在现有应用中加入Flutter支持以及flutter_boost的引入

在现有项目中导入Flutter

参考:Google官方文档 Add-Flutter-to-existing-apps

  • Android项目引入flutter module
  1. 在项目根目录通过命令行执行
flutter create -t module my_flutter
  1. 进入my_flutter文件夹
$ cd .android/
$ gradlew flutter:assembleDebug
  1. 在app下的build.gradle添加如下配置
compileOptions {
  sourceCompatibility 1.8
  targetCompatibility 1.8
}
  1. 在setting.gradle里配置
//根目录
include ':app'                                     // assumed existing content
setBinding(new Binding([gradle: this]))                                 // new
evaluate(new File(                                                      // new
  settingsDir,                                               // new
  'my_flutter/.android/include_flutter.groovy'                          // new
))                                                                      // new
//同级目录
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
  1. 在app下的build.gradle文件添加依赖项目。
    ps:此处必须是flutter,因为项目引入的是在.\flutter_module.android下的Flutter库
implementation project(':flutter')
  • 在原生使用flutter
 private void createFlutterView() {
        View flutterView = Flutter.createView(MainActivity.this,getLifecycle(),"rount1");
        FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addContentView(flutterView, layout);
    }

flutter_boost的使用

  • 在flutter的项目pubspec.yaml中添加依赖
flutter_boost: ^0.0.415
  • Flutter代码集成
void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State {
  @override
  void initState() {
    super.initState();

    ///register page widget builders,the key is pageName
    FlutterBoost.singleton.registerPageBuilders({
      'sample://firstPage': (pageName, params, _) => FirstRouteWidget(),
      'sample://secondPage': (pageName, params, _) => SecondRouteWidget(),
    });

    ///query current top page and load it
    FlutterBoost.handleOnStartPage();
  }

  @override
  Widget build(BuildContext context) => MaterialApp(
      title: 'Flutter Boost example',
      builder: FlutterBoost.init(), ///init container manager
      home: Container());
}
  • Android代码集成

在Application的onCreate初始化

引入flutter_boost

    implementation project(path: ':flutter_boost')
public class MyApplication extends FlutterApplication {
    @Override
    public void onCreate() {
        super.onCreate();
        FlutterBoostPlugin.init(new IPlatform() {
            @Override
            public Application getApplication() {
                return MyApplication.this;
            }

            /**
             * get the main activity, this activity should always at the bottom of task stack.
             */
            @Override
            public Activity getMainActivity() {
                return MainActivity.sRef.get();
            }

            @Override
            public boolean isDebug() {
                return false;
            }

            /**
             * start a new activity from flutter page, you may need a activity router.
             */
            @Override
            public boolean startActivity(Context context, String url, int requestCode) {
                return PageRouter.openPageByUrl(context,url,requestCode);
            }

            @Override
            public Map getSettings() {
                return null;
            }
        });
    }

踩坑

//flutter只提供v7a的so,所以只能配置
ndk {
            abiFilters "armeabi-v7a"
        }

引入报错Must be able to initialize the VM.

尝试在flutter的执行gradle编译 gradlew assemble

https://github.com/flutter/flutter/issues/19818

你可能感兴趣的:(在现有应用中加入Flutter支持以及flutter_boost的引入)