根据 https://flutter.dev/docs/development/add-to-app 中的步骤,在现有的应用中添加flutter模块

准备工作:

Android studio要升级到3.6,flutter插件要是42以上的

  1. File-new module 选择flutter module,这样就可以生成一个flutter模块

  2. 向应用中添加并启动flutter页面

    1. 先初始化flutter在application中
      FlutterMain.startInitialization(this);

    2. 启动默认页面

      startActivity(FlutterActivity.createDefaultIntent(activity!!))

    3. 添加依赖 implementation 'android.arch.lifecycle:common-java8:1.1.1'

  3. 错误记录

    1. 错误: 程序包android.support.annotation不存在

      修正:将对 android.support.annotation.NonNull; 的改为Androidx androidx.fragment.app.Fragment;

      androidx.annotation.NonNull;
      import androidx.lifecycle.Lifecycle;
      import androidx.lifecycle.LifecycleObserver;
      import androidx.lifecycle.OnLifecycleEvent;

    2. 错误:未初始化flutter

      1
      Unable to start activity ComponentInfo{com.magefitness.app/io.flutter.embedding.android.FlutterActivity}: java.lang.IllegalStateException: ensureInitializationComplete must be called after startInitialization

      修正flutter未初始化,在application中FlutterMain.startInitialization(this);

  1. 未添加 implementation 'android.arch.lifecycle:common-java8:1.1.1'

    1
    Failed resolution of: Lio/flutter/embedding/engine/FlutterEngineAndroidLifecycle$1;