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

相关推荐
2501_916007475 小时前
iOS 上架技术支持全流程解析,从签名配置到使用 开心上架 的实战经验分享
android·macos·ios·小程序·uni-app·cocoa·iphone
明月与玄武6 小时前
Melos 使用指南:Flutter / Dart 多包管理工具!
flutter·melos 使用指南·dart 多包管理工具
sakoba6 小时前
MySQL的json处理相关方法
android·学习·mysql·json
神仙别闹6 小时前
Android 端 2D 横屏动作冒险类闯关游戏
android·游戏
坏小虎6 小时前
Android App Startup 库使用说明文档,初始化不再用Application了...
android
lichong95113 小时前
Android studio 修改包名
android·java·前端·ide·android studio·大前端·大前端++
shr007_15 小时前
flutter 鸿蒙
flutter·华为·harmonyos
Bryce李小白15 小时前
Flutter 与原生混合编程
flutter
爱学习的大牛12316 小时前
MVVM 架构 android
android·mvvm