android集成咸鱼的FlutterBoost
早就看到咸鱼发布的flutterboost的文章,能够在连续的flutter页面公用一个引擎达到节省内存和统一管理路由这些有点.但一直没有时间去试用它,趁着这两天app发版做了一下尝试.在接入过程中折腾了小半天,理解后觉得并没有想象中的那么复杂,所以写下来给后来者,节省不必要的时间.
下面开始说说的接入之旅
1.在原有项目执行flutter命令新建一个modue
flutter create -t module test_flutter
此时会生成一个flutter module
2.在app项目的根目录下的setting.gradle中加入以下代码
setBinding(new Binding([gradle: this]))
evaluate(new File(
settingsDir.parentFile,
'你的工程目录/test_flutter/.android/include_flutter.groovy' // 比如我的原有工程明叫MixFlutterTest 刚才创建的flutter module叫test_flutter 那么这个地方应该是MixFlutterTest/test_flutter/.android/include_flutter.groovy
))
3.在原有项目的app目录下的build.gradle中增加对flutter的依赖 'app/build.gradle'
dependencies {
implementation project(':flutter')
}
4.在第一步中新增的flutter module下的pubspec.yaml中新增对咸鱼flutter插件的依赖 'test_flutter/pubspec.yaml'
name: test_boost
description: A new flutter module project.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
xservice_kit: ^0.0.18
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
dev_dependencies:
flutter_test:
sdk: flutter
flutter_boost:
path: ../../../flutter_boost-master
flutter:
uses-material-design: true
module:
androidPackage: com.example.test_boost
iosBundleIdentifier: com.example.testBoost
注意
dev_dependencies:
flutter_test:
sdk: flutter
flutter_boost:
path: ../../../flutter_boost-master // 这个地方是从github下载下来的本地flutter_boost的项目地址 并且从此flutter module目录开始去找flutter_boost-master的地址 ..是上一层目录 你需要根据你的flutter_boost-master地址来定义这个路径
test_flutter/pubspec.yaml中新增了两个地方
dependencies:
xservice_kit: ^0.0.18
flutter_boost:
path: ../../../flutter_boost-master
注意看上面的pubspec.yaml文件配置
5.在app工程中引入flutter_boost依赖代码 在第三部的地方 'app/build.gradle'
dependencies {
implementation project(':flutter')
implementation project(':flutter_boost') // 新增
}
在build工程时会报错,需要在工程中加入
android {
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}
完整的build文件 app/build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "cn.mf.mixfluttertest"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(':flutter')
implementation project(':flutter_boost')
}
接入完成后 build后工程目录是这样
注意标注 1 和 2的地方
build工程时可能还会报错你需要修改下1和2的build.gradle配置 改成和你相对应的build版本,里面的配置项尽可能的和app/build.gradle里的编译选项保持一致
标注1的地方
group 'com.taobao.idlefish.flutterboost'
version '1.0-SNAPSHOT'
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2' // 注意
}
}
rootProject.allprojects {
repositories {
google()
jcenter()
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 27 // 注意
buildToolsVersion "27.0.3" // 注意 编译版本
defaultConfig {
minSdkVersion 16 // 注意 最小sdk版本
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
lintOptions {
disable 'InvalidPackage'
}
}
dependencies {
implementation 'com.alibaba:fastjson:1.2.41' // 注意
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:appcompat-v7:27.1.1'
compileOnly rootProject.findProject(":xservice_kit") // 注意可能提示换成compileOnly 按照提示换成compileOnly就行
}
ext {
groupId = 'com.taobao.fleamarket'
artifactId = "FlutterBoost"
}
标注2的地方
group 'com.taobao.idlefish.xservice_kit'
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
}
}
rootProject.allprojects {
repositories {
google()
jcenter()
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 27 // 注意
buildToolsVersion "27.0.3" // 注意
defaultConfig {
minSdkVersion 16 // 注意
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
lintOptions {
disable 'InvalidPackage'
}
}
6.最后可能还会提示app里的 android:allowBackup="true" 不允许
找到标注1中 src/main/java/AndroidManifest.xml 把android:allowBackup="true"改为android:allowBackup="false"
编译时可能还会提示BoostFlutterNativeView中36有错误
com/taobao/idlefish/flutterboost/BoostFlutterNativeView.java:36: 错误: 方法不会覆盖或实现超类型的方法
@override
找到这个文件 删除调这个方法就行 变成这样
import android.content.Context;
import io.flutter.view.FlutterNativeView;
public class BoostFlutterNativeView extends FlutterNativeView {
public BoostFlutterNativeView(Context context) {
super(context);
}
@Override
public void destroy() {
//do nothing...
}
public void boostDestroy() {
super.destroy();
}
}
ok所有步骤都完成了 祝你好运