一、创建Flutter Module
    在已有Android工程中集成flutter,可以使用AndroidStudio的new Flutter Module实现,方便快捷。

    image.png
    找到FlutterModule,一连串的next操作即可
    image.png
    创建完成后我们的项目中会多一个叫做flutter的module
    image.png
    并且在项目的setting.gradle文件中会自动添加如下几行代码,

    1. setBinding(new Binding([gradle:this]))
    2. evaluate(
    3. new File(
    4. settingsDir,
    5. 'flutter_module/.android/include_flutter.groovy'
    6. )
    7. )
    8. include ':flutter_module'

    最后我们只需在app的build.gradle中引用这个flutter模块即可

    1. implementation project(path: ':flutter')

    二、使用Flutter
    两种使用方式,FlutterView和FlutterFragment。
    1、使用FlutterFragment
    新版本的FlutterSDK不再支持已经没有Flutter类,不再支持诸如Flutter.createView()、Flutter.createFragment()等用法,通过阅读其源码我们可以找到FlutterFragment的新用法,代码如下:

    1. public class MyFlutterActivity extends FragmentActivity {
    2. @Override
    3. protected void onCreate(@Nullable Bundle savedInstanceState) {
    4. super.onCreate(savedInstanceState);
    5. setContentView(R.layout.flutter_layout);
    6. //FlutterFragment fragment=new FlutterFragment();
    7. FlutterFragment fragment=FlutterFragment.withNewEngine().initialRoute("home").build();
    8. getSupportFragmentManager().beginTransaction().add(R.id.flutter_container, fragment).commit();
    9. }
    10. }

    代码中FlutterFragment的创建也可以直接使用默认的构造方法,这样加载main.dart中的默认路由,即’’/‘’,给出的示例代码中我们通过调用FlutterFragment.withNewEngine().initialRoute("home").build()创建了新的NewEngineFragmentBuilder来构建指定路由的FlutterFragment。
    2、使用FlutterView
    FlutterView的创建同样不能使用老版本的方法了,新版本的使用方法代码如下:

    1. public class MyFlutterActivity extends FragmentActivity {
    2. @Override
    3. protected void onCreate(@Nullable Bundle savedInstanceState) {
    4. super.onCreate(savedInstanceState);
    5. setContentView(R.layout.flutter_layout);
    6. FrameLayout frameLayout=findViewById(R.id.flutter_container);
    7. ProgressBar progress=findViewById(R.id.progress);
    8. //创建FlutterView
    9. FlutterView flutterView=new FlutterView(this);
    10. //创建FlutterView首帧渲染完成监听
    11. flutterView.addFirstFrameListener(new FlutterView.FirstFrameListener() {
    12. @Override
    13. public void onFirstFrame() {
    14. //隐藏进度条,显示FlutterView
    15. progress.setVisibility(View.GONE);
    16. frameLayout.setVisibility(View.VISIBLE);
    17. }
    18. });
    19. //创建dart代码执行器
    20. DartExecutor executor=flutterView.getDartExecutor();
    21. //执行main.dart中的main函数
    22. executor.executeDartEntrypoint(
    23. new DartExecutor.DartEntrypoint(FlutterMain.findAppBundlePath(),
    24. "main"));
    25. //将FlutterView添加到布局中
    26. ViewGroup.LayoutParams layoutParams=new LinearLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
    27. FrameLayout.LayoutParams.MATCH_PARENT);
    28. frameLayout.addView(flutterView);
    29. }
    30. }

    布局文件如下:

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent">
    5. <FrameLayout
    6. android:id="@+id/flutter_container"
    7. android:visibility="invisible"
    8. android:layout_width="match_parent"
    9. android:layout_height="match_parent">
    10. </FrameLayout>
    11. <ProgressBar
    12. android:id="@+id/progress"
    13. android:layout_width="45dp"
    14. android:layout_height="45dp"
    15. android:layout_centerVertical="true"
    16. android:layout_centerHorizontal="true"
    17. android:visibility="visible"
    18. >
    19. </ProgressBar>
    20. </RelativeLayout>

    效果如下图
    WeChat_20200121151718.gif