(二)Flutter插件之Android插件开发

1、打开Android插件项目

2、Android插件报红处理

在build.gradle 中加入外在最外层即可

bash 复制代码
//获取local.properties配置文件
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}
//获取flutter的sdk路径
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

dependencies {
    compileOnly files("$flutterRoot/bin/cache/artifacts/engine/android-arm/flutter.jar")
    testImplementation files("$flutterRoot/bin/cache/artifacts/engine/android-arm/flutter.jar")
}

3、Android插件 如何开发与验证(把Android插件改成独立的项目)

Android的单元测试是满足不了的

Flutter 插件(android/ 只是插件模块),现在希望让它变成 独立的 Android 应用,可以直接运行和调试。

将 apply plugin: 'com.android.library' 改成:apply plugin: 'com.android.application'

bash 复制代码
    apply plugin: 'com.android.application'
    defaultConfig {
        applicationId = "com.example.my_plugin_app"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"
    }
bash 复制代码
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.plugin.my_flutter_plugin">
    <application
        android:label="MyPluginApp"
        android:icon="@mipmap/ic_launcher">
        <activity android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>
bash 复制代码
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="Hello from converted plugin!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
bash 复制代码
package com.example.plugin.my_flutter_plugin
import android.app.Activity
import android.os.Bundle
import com.example.plugin.my_flutter_plugin.bean.JsMsgBean
import com.example.plugin.my_flutter_plugin.dispatcher.JsActionDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        CoroutineScope(Dispatchers.Default).launch {
            if (JsActionDispatcher.hasStrategy("canOpenUrl")) {
                JsActionDispatcher.dispatch(this@MainActivity, this,
                    JsMsgBean("canOpenUrl", {})
                ) {
                    runOnUiThread {
                        // 回调结果处理
                        println("插件调用结果: $it")
                    }
                }
            }
        }
    }
}

gradle.properties 支持androidx

bash 复制代码
android.useAndroidX=true
android.experimental.DEX_ALIGNMENT_16KB_PAGE=true

4、写插件中的主要方法

5、第四部无误之后,把对应代码复制到插件当中

相关推荐
zhlx283511 小时前
免费开源跨平台听歌自由!自定义音乐源 + 桌面歌词!LX Music 落雪音乐开源版
android·windows·macos
程序员码歌20 小时前
短思考第263天,每天复盘10分钟,胜过盲目努力一整年
android·前端·后端
安卓兼职framework应用工程师20 小时前
Android 10.0 按键智能机按键连续响两次的异常处理
android·audio·audioservice·按键音·按键声音
studyForMokey21 小时前
【Android 项目】个人学习demo随笔
android
吃喝不愁霸王餐APP开发者21 小时前
利用责任链模式解耦多平台(美团/饿了么)霸王餐接口的适配逻辑
android·责任链模式
百***787521 小时前
Step-Audio-2 轻量化接入全流程详解
android·java·gpt·php·llama
武玄天宗1 天前
第四章、用flutter创建登录界面
flutter
搬砖的kk1 天前
基于Flutter开发应用如何快速适配HarmonyOS
flutter·华为·harmonyos
昼-枕1 天前
Flutter深度解析:如何构建高性能、跨平台的移动应用
flutter
yangpipi-1 天前
《C++并发编程实战》第5章 C++内存模型和原子操作
android·java·c++