集成流程
- android studio 命令行,输入如下命令
flutter create -t module my_flutter
相当于创建了一个flutter的moudle
- 需要满足jdk要求,添加如下片段(app gradle)
android {
compileOptions {//flutter 需要这样声明
sourceCompatibility 1.8
targetCompatibility 1.8
}
}
- 因为flutter是一个moudle,所以需要在主工程添加依赖
1、在原应用的setting.gradle下添加如下代码
setBinding(new Binding([gradle: this]))
evaluate(new File(
settingsDir, //若做为原应用的子mudule,则为settingsDir
'my_flutter/.android/include_flutter.groovy'
))
2、添加依赖
implementation project(':flutter')
- 添加混淆配置
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.** { *; }
-keep class io.flutter.util.** { *; }
-keep class io.flutter.view.** { *; }
-keep class io.flutter.** { *; }
-keep class io.flutter.plugins.** { *; }
遇到的问题
查看flutter.gradleflutter.gradle文件,得知默认main module name是app,而当前demo是sample,所以需要在配置文件中,指定mainModuleName
ext {
mainModuleName = 'sample'
}
相关flutter逻辑(include_flutter.groovy)
gradle.getGradle().projectsLoaded { g ->
g.rootProject.beforeEvaluate { p ->
_mainModuleName = binding.variables['mainModuleName']
if (_mainModuleName != null && !_mainModuleName.empty) {
p.ext.mainModuleName = _mainModuleName
}
}
g.rootProject.afterEvaluate { p ->
p.subprojects { sp ->
if (sp.name != 'flutter') {
sp.evaluationDependsOn(':flutter')
}
}
}
}
对比包大小
- 集成flutter之前包的大小:14.2
- 集成flutter之后包的大小:19.1m
结论:大了约5m