(二)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、第四部无误之后,把对应代码复制到插件当中

相关推荐
SHEN_ZIYUAN39 分钟前
Android 主线程性能优化实战:从 90% 降至 13%
android·cpu优化
曹绍华43 分钟前
android 线程loop
android·java·开发语言
雨白43 分钟前
Hilt 入门指南:从 DI 原理到核心用法
android·android jetpack
介一安全1 小时前
【Frida Android】实战篇3:基于 OkHttp 库的 Hook 抓包
android·okhttp·网络安全·frida
sTone873751 小时前
Android Room部件协同使用
android·前端
我命由我123451 小时前
Android 开发 - Android JNI 开发关键要点
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
千码君20161 小时前
Android Emulator hypervisor driver is not installed on this machine
android
lichong9511 小时前
Android studio release 包打包配置 build.gradle
android·前端·ide·flutter·android studio·大前端·大前端++
傲世(C/C++,Linux)2 小时前
Linux系统编程——进程通信之有名管道
android·linux·运维