1、创建fluttermodule,创建好的fluttermodule和Android项目是在同一级目录下
//当前android项目的上级目录
cd ../
//创建fluttermodule
//不支持Androidx
flutter create -t module flutter_module
//支持Androidx
flutter create --androidx -t module flutter_module
2、在settings.gradle文件中配置一下代码
rootProject.name='NativeApp'
include ':app'
setBinding(new Binding([gradle: this]))
evaluate(new File(
settingsDir.parentFile,
'flutter_module/.android/include_flutter.groovy'
))
include ':flutter_module'
project(':flutter_module').projectDir = new File('../flutter_module')
3、app目录下build.gradle中
//最小改为16
minSdkVersion 19
4、app目录下build.gradle中添加依赖
android {
....
//jdk1.8编辑,否则会报错
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
...
//添加依赖
implementation project(':flutter')
...
}
5、加载flutter页面
//在清单配置文件中注册activity
<activity
android:name="io.flutter.embedding.android.FlutterActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"
/>
//跳转到flutter页面
startActivity(
FlutterActivity
.withNewEngine()
.initialRoute("route1")
.build(MainActivity.this)
);
6、flutter中接受初始化参数
import 'dart:ui';
@override
void initState() {
// TODO: implement initState
super.initState();
//获取初始化的参数
window.defaultRouteName;
}