Flutter 嵌入到Android原生项目中的几种实现方式
环境:Mac + Flutter 1.20.0
一、在原来Android Module基础上添加
1. 点击File---->New Module--->Flutter Module
添加完成Flutter Module之后会自动添加对Flutter的依赖
至此就完成了对Flutter 部分的依赖可以进行调试
FlutterMain.startInitialization(this);
Intent intent1 = new Intent(MainActivity.this,FlutterMainActivity.class);
startActivity(intent1);
二、 AAR 依赖形式
第一种会产生原来项目结构变化,同时Flutter 和原生的代码没有完全分离。此时可以考虑把Flutter 相关部分打包成 AAR,AAR 形式又主要分为两种:1. maven 依赖 2. aar,jar 组合
1. maven依赖
先创建一个Flutter Project---->Flutter Module
创建完成之后可以在该目录下执行:flutter build aar
命令执行完成之后会给出提示:
1. Open <host>/app/build.gradle
2. Ensure you have the repositories configured, otherwise add them:
String storageUrl = System.env.FLUTTER_STORAGE_BASE_URL ?: "https://storage.googleapis.com"
repositories {
maven {
url '/Users/lang/test/flutter_module_test/build/host/outputs/repo'
}
maven {
url '$storageUrl/download.flutter.io'
}
}
3. Make the host app depend on the Flutter module:
dependencies {
debugImplementation 'com.example.flutter_module_test:flutter_debug:1.0'
profileImplementation 'com.example.flutter_module_test:flutter_profile:1.0'
releaseImplementation 'com.example.flutter_module_test:flutter_release:1.0'
}
4. Add the `profile` build type:
android {
buildTypes {
profile {
initWith debug
}
}
}
上面4步就是谷歌官方推荐的,但是第二步中的 url '$storageUrl/download.flutter.io'
应该是 url 'http://download.flutter.io' ,同时第二步设置在host的build.gradle文件中,非Project
以 release 为例引用的即如下目标的相关文件
设置完成即引入Flutter,添加Flutter测试入口,编译可以测试
上面依赖的是本地的仓库,后期再出一片生成远程maven仓库并引用的文章。
2. 不依赖本地仓库
step1: 生成aar过程和上面一样,讲上图中的 flutter_release-1.0.aar复制到android项目下
step2:
找到flutter根目录下的build/host/outputs/repo, 将flutter_release-1.0.pom,里面有如下flutter基础库的地址
<groupId>io.flutter</groupId>
<artifactId>flutter_embedding_release</artifactId>
<version>1.0.0-c8e3b9485386425213e2973126d6f57e7ed83c54</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.flutter</groupId>
<artifactId>armeabi_v7a_release</artifactId>
<version>1.0.0-c8e3b9485386425213e2973126d6f57e7ed83c54</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.flutter</groupId>
<artifactId>arm64_v8a_release</artifactId>
<version>1.0.0-c8e3b9485386425213e2973126d6f57e7ed83c54</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.flutter</groupId>
<artifactId>x86_64_release</artifactId>
<version>1.0.0-c8e3b9485386425213e2973126d6f57e7ed83c54</version>
<scope>compile</scope>
</dependency>
step3: 在/app/build.gradle下添加
repositories {
maven {
url 'http://download.flutter.io'
}
}
dependencies {
implementation(name: 'flutter_release-1.0', ext: 'aar')
implementation 'io.flutter:flutter_embedding_release:1.0.0-c8e3b9485386425213e2973126d6f57e7ed83c54'
implementation 'io.flutter:armeabi_v7a_release:1.0.0-c8e3b9485386425213e2973126d6f57e7ed83c54'
implementation 'io.flutter:arm64_v8a_release:1.0.0-c8e3b9485386425213e2973126d6f57e7ed83c54'
}
注意:如果设置了 abiFilters,上面的依赖arm 依赖一定要对应上,每次flutter clean,再 flutter build aar 之后的 1.0.0-c8e3b9485386425213e2973126d6f57e7ed83c54 是不变的
arm 方式可以把对应的so库直接放大libs,主要是 libflutter.so ,这个库可以从正常运行的包中解压然后放到对应的arm系列文件夹下。
本文地址:https://blog.csdn.net/jia635/article/details/107859544