(二)Flutter插件之Android插件开发
1、打开Android插件项目
2、Android插件报红处理
在build.gradle 中加入外在最外层即可
//获取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’
apply plugin: 'com.android.application'defaultConfig {applicationId = "com.example.my_plugin_app"minSdk = 24targetSdk = 34versionCode = 1versionName = "1.0"}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.plugin.my_flutter_plugin"><applicationandroid: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>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:gravity="center"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:text="Hello from converted plugin!"android:layout_width="wrap_content"android:layout_height="wrap_content"/>
</LinearLayout>
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.launchclass 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
android.useAndroidX=true
android.experimental.DEX_ALIGNMENT_16KB_PAGE=true