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

相关推荐
javaGHui5 分钟前
安卓传感器横竖屏切换
android·经验分享·笔记
用户69371750013847 分钟前
21.Kotlin 接口:接口 (Interface):抽象方法、属性与默认实现
android·后端·kotlin
QING61817 分钟前
Jetpack Compose 中 Flow 收集详解 —— 新手指南
android·kotlin·android jetpack
AskHarries19 分钟前
Flutter + Supabase 接入 Google 登录
flutter
泓博19 分钟前
Android截屏汇报问题
android
晚霞的不甘21 分钟前
架构演进与生态共建:构建面向 OpenHarmony 的 Flutter 原生开发范式
flutter·架构
_李小白23 分钟前
【Android FrameWork】第二十一天:AudioFlinger
android
向葭奔赴♡27 分钟前
Android SharedPreferences实战指南
android·java·开发语言
愤怒的代码28 分钟前
一个使用 AI 开发的 Android Launcher
android
Ya-Jun33 分钟前
架构设计模式:依赖注入最佳实践
flutter