1. 手动整合Flutter到Android项目
- Flutter支持
x86_64
,armeabi-v7a
,arm64-v8a
android { //... defaultConfig { ndk { // Filter for architectures supported by Flutter. // 如果使用Android模拟器测试请添加'x86'平台 abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86_64' } } }
- java8支持
android { //... compileOptions { sourceCompatibility 1.8 targetCompatibility 1.8 } }
- 在现有Android项目的同级目录执行创建Flutter项目的命令
flutter create -t module --org cn.it200 my_flutter
- 配置
settings.gradle
// Include the host app project. include ':app' // assumed existing content setBinding(new Binding([gradle: this])) // new evaluate(new File( // new settingsDir.parentFile, // new 'my_flutter/.android/include_flutter.groovy' // new )) // new
- 为Android项目依赖Flutter模块
dependencies { implementation project(':flutter') }
- 在Java代码中可以正常导入Flutter相关包,编译项目无异常
2. 在Activity中启动Flutter页面
- 注册FlutterActivity到清单文件
<activity android:name="io.flutter.embedding.android.FlutterActivity" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize" />
- 启动FlutterActivity
//打开首页 startActivity( FlutterActivity.createDefaultIntent(currentActivity) ); //根据路由打开指定页面 startActivity( FlutterActivity .withNewEngine() .initialRoute("/other") .build(MainActivity.this) );
- 引入FlutterEngine加快启动
//初始化FlutterEngine并进行缓存 FlutterEngine flutterEngine = new FlutterEngine(this); flutterEngine.getDartExecutor().executeDartEntrypoint( DartExecutor.DartEntrypoint.createDefault() ); FlutterEngineCache.getInstance().put("my_engine_id", flutterEngine);
//改变启动方式 startActivity( FlutterActivity.withCachedEngine("my_engine_id").build(MainActivity.this); );
- 引入FlutterEngine时初始化路由
在创建flutterEngine对象后插入如下内容: flutterEngine.getNavigationChannel().setInitialRoute("/other");
3. 在Activity中加载Flutter页面
- 布局文件添加代码
<FrameLayout android:layout_weight="1" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" />
- 在Activity中添加代码
// Define a tag String to represent the FlutterFragment within this // Activity's FragmentManager. This value can be whatever you'd like. private static final String TAG_FLUTTER_FRAGMENT = "flutter_fragment"; // Declare a local variable to reference the FlutterFragment so that you // can forward calls to it later. private FlutterFragment flutterFragment;
// Get a reference to the Activity's FragmentManager to add a new // FlutterFragment, or find an existing one. FragmentManager fragmentManager = getSupportFragmentManager(); // Attempt to find an existing FlutterFragment, // in case this is not the first time that onCreate() was run. flutterFragment = (FlutterFragment) fragmentManager .findFragmentByTag(TAG_FLUTTER_FRAGMENT); // Create and attach a FlutterFragment if one does not exist. if (flutterFragment == null) { flutterFragment = FlutterFragment.createDefault(); fragmentManager .beginTransaction() .add( R.id.fragment_container, flutterFragment, TAG_FLUTTER_FRAGMENT ) .commit(); }
@Override public void onPostResume() { super.onPostResume(); flutterFragment.onPostResume(); } @Override protected void onNewIntent(@NonNull Intent intent) { super.onNewIntent(intent); flutterFragment.onNewIntent(intent); } @Override public void onBackPressed() { flutterFragment.onBackPressed(); } @Override public void onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults ) { flutterFragment.onRequestPermissionsResult( requestCode, permissions, grantResults ); } @Override public void onUserLeaveHint() { flutterFragment.onUserLeaveHint(); } @Override public void onTrimMemory(int level) { super.onTrimMemory(level); flutterFragment.onTrimMemory(level); }
- 使用
FlutterFragment.withCachedEngine("my_engine_id").build();
加速启动
if (flutterFragment == null) { flutterFragment = FlutterFragment.withCachedEngine("my_engine_id").build(); fragmentManager .beginTransaction() .add( R.id.fragment_container, flutterFragment, TAG_FLUTTER_FRAGMENT ) .commit(); }