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

相关推荐
enbug21 小时前
编译安卓内核:以坚果R1、魔趣MK100(Android 10)系统为例
android·linux
、BeYourself1 天前
应用专属文件与应用偏好设置(SharedPreferences)
android
2501_948120151 天前
基于模糊数学的风险评估模型
android
MengFly_1 天前
Compose 脚手架 Scaffold 完全指南
android·java·数据库
灰灰勇闯IT1 天前
Flutter for OpenHarmony:深色模式下的 UI 优化技巧 —— 构建舒适、可读、无障碍的夜间体验
flutter·ui
浩辉_1 天前
Dart - 认识Sealed
flutter·dart
·云扬·1 天前
MySQL Binlog三种记录格式详解
android·数据库·mysql
2501_940007891 天前
Flutter for OpenHarmony三国杀攻略App实战 - 鸿蒙适配与打包发布
前端·flutter
月明泉清1 天前
Android中对于点击事件的深度梳理(二)
android
一起养小猫1 天前
Flutter for OpenHarmony 进阶:数据统计与排序算法深度解析
flutter·harmonyos