Flutter混合环境搭建踩坑

首先贴一下Flutter开发环境,由于目前的项目还没有迁移到Androidx,所以我是以support包为例:

[✓] Flutter (Channel unknown, v1.9.1+hotfix.6, on Mac OS X 10.15.3 19D76, locale
    zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[!] Xcode - develop for iOS and macOS (Xcode 11.3.1)
    ✗ CocoaPods installed but not working.
        You appear to have CocoaPods installed but it is not working.
        This can happen if the version of Ruby that CocoaPods was installed with
        is different from the one being used to invoke it.
        This can usually be fixed by re-installing CocoaPods. For more info, see
        https://github.com/flutter/flutter/issues/14293.
      To re-install CocoaPods, run:
        sudo gem install cocoapods
[✓] Android Studio (version 3.5)
[✓] Connected device (1 available)

1. 现在开始接入,主项目以MyApp为例

  • MyApp/app/build.gradle 添加so库相关过滤
android {
  //...
  defaultConfig {
    ndk {
      // Filter for architectures supported by Flutter.
      abiFilters 'armeabi-v7a', 'arm64-v8a'
    }
  }
}
  • 进入到MyApp的父级文件夹下,创建my_flutter module
cd some/path/
flutter create -t module  my_flutter

  • MyApp/app/build.gradle 文件中增加java8支持
android {
  //...
  compileOptions {
    sourceCompatibility 1.8
    targetCompatibility 1.8
  }
}
  • MyApp/settings.gradle中,添加以下依赖
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
  • MyApp/app/build.gradle中,依赖flutter 模块
  implementation project(':flutter')

2. 下面开始测试从native跳转flutter页面

  • 在AndroidManifest.xml中注册FlutterActivity


  • 任意页面添加跳转代码,以在TestActivity中为例
myButton.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    startActivity(
      FlutterActivity.createDefaultIntent(TestActivity.this)
    );
  }
});

以上步骤全是按照官方文档来的,按道理配置完成应该正常跳转,但是还是报错了:

   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.snbc.honey_app/io.flutter.embedding.android.FlutterActivity}: java.lang.IllegalStateException: ensureInitializationComplete must be called after startInitialization
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6806)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

看报错应该是初始化的问题,但是官方文档没有提到任何初始化步骤相关的代码,这个时候不要慌,查查相关issue,有人也碰到这种问题,表示要加一行初始化代码:

  • 在MyApplication的onCreate方法中添加
public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        FlutterMain.startInitialization(this);

    }
}

你以为这样就好了吗,再运行一次,还会报错:

java.lang.NoClassDefFoundError: Failed resolution of: Landroid/arch/lifecycle/DefaultLifecycleObserver;
        at io.flutter.embedding.engine.FlutterEngine.(FlutterEngine.java:152)
        at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.setupFlutterEngine(FlutterActivityAndFragmentDelegate.java:221)
        at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onAttach(FlutterActivityAndFragmentDelegate.java:145)
        at io.flutter.embedding.android.FlutterActivity.onCreate(FlutterActivity.java:399)
        at android.app.Activity.performCreate(Activity.java:7224)
        at android.app.Activity.performCreate(Activity.java:7213)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6806)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.arch.lifecycle.DefaultLifecycleObserver" on path: DexPathList[[zip file "/data/app/com.example.myapplication-kZH0dnJ-qI1ow1NqGOB2ug==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.myapplication-kZH0dnJ-qI1ow1NqGOB2ug==/lib/arm64, /data/app/com.example.myapplication-kZH0dnJ-qI1ow1NqGOB2ug==/base.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]

只能继续google,这个时候还要在app/build.gradle里增加这个依赖:

   implementation 'android.arch.lifecycle:common-java8:1.1.0'

好了,再次运行,终于能够正常跳转了。

比较坑爹的是,官方文档没有任何这样的相关操作提示,我自己又摸索了好久,最终找到一种更加简单的解决办法,直接 将flutter 版本升级到1.12以上 (友情提示,升级的时候请配置好国内镜像 https://www.jianshu.com/p/1c31218bffc3

flutter version v1.12.13+hotfix.6

升级一下flutter版本,再按照官方文档来是行得通的,我怀疑文档是基于高版本写的

还有一个小问题,native跳转flutter的时候会有黑屏,这个影响不太大,打release包不会出现,当然也是可以解决的,可以自行google。

你可能感兴趣的:(Flutter混合环境搭建踩坑)