原生Android项目集成Flutter Module踩坑记录

按照官网的手动集成步骤一步一步完成,但是在运行打开 FlutterActivity 时出现了了如下错误:

E/flutter: [ERROR:flutter/runtime/dart_vm_data.cc(18)] VM snapshot invalid and could not be inferred from settings.
E/flutter: [ERROR:flutter/runtime/dart_vm.cc(250)] Could not setup VM data to bootstrap the VM from.
E/flutter: [ERROR:flutter/runtime/dart_vm_lifecycle.cc(84)] Could not create Dart VM instance.
A/flutter: [FATAL:flutter/shell/common/shell.cc(274)] Check failed: vm. Must be able to initialize the VM.
    

百度该错误定位到的问题解释有两种:

  • Flutter 集成到Android项目中遇到的坑
  • Flutter与Android混合编码配置笔记

即:1、因为app模块名称不是 app ;2、存在变体;

我的项目中模块名称是app,故排除 1 。我的项目中存在多构建模式与多变体,便尝试方案2,但是结果很残酷并不能正常运行,依旧报该错误。

在尝试的过程中我发现,当构建模式在debug模式下,是可以正常拉起 Flutter 页面的,在我自定义的构建模式下才会报错,思考该问题的出现可能是 flutter 模块不支持我们自定义的构建模式。

参考方案1的解决思路,找到 $flutterRoot/packages/flutter_tools/gradle/flutter.gradle,搜索 buildTypes,定位到代码块:

project.android.buildTypes {
            // Add profile build type.
            profile {
                initWith debug
                if (it.hasProperty("matchingFallbacks")) {
                    matchingFallbacks = ["debug", "release"]
                }
            }

            release {
                // Enables code shrinking, obfuscation, and optimization for only
                // your project's release build type.
                minifyEnabled true
                // Enables resource shrinking, which is performed by the
                // Android Gradle plugin.
                // NOTE: The resource shrinker can't be used for libraries.
                shrinkResources isBuiltAsApp(project)
                // Fallback to `android/app/proguard-rules.pro`.
                // This way, custom Proguard rules can be configured as needed.
                proguardFiles project.android.getDefaultProguardFile("proguard-android.txt"), flutterProguardRules, "proguard-rules.pro"
            }
        }

可以看到,果不其然,在 flutter 中,默认只支持三种构建模式,分别是debugprofilerelease

考虑到我们的原生Android 项目是基于多构建模式实现的多衍生版本,显然从多构建模式重构到只使用变体实现多衍生需要做大量的工作。是否我们可以采用一种取巧的方式来实现在当前原生项目中集成Flutter,那就是直接修改 flutter.gradle 文件,手动的添加我们当前项目使用的 buildtypes 。

原生Android项目集成Flutter Module踩坑记录_第1张图片

我们参考 profile 的写法,添加自己项目中使用的构建模式到该代码块中,在回到原生项目,选择 Invalidata cache / Restart ,重新打开后,将flutter模块的构建模式选择成与项目一直即可。

提示:在测试过程中,使用initWith debug ,这样可以享受Flutter的热加载等功能,在发布时使用 initWith release 提升性能、减小包体积。
原生Android项目集成Flutter Module踩坑记录_第2张图片
此时如果没有别的情况,你可以正常的使用flutter模块了,但是你还有可能遇到另一个问题,报错日志:

Transform's input file does not exist: /Users/houwenjun/dev/my_flutter/.android/Flutter/build/intermediates/flutter/qaRelease/libs.jar

这种情况请尝试修改 gradle 版本为 5.6.2com.android.tools.build:gradle 版本修改为 3.5.0,我在这样修改后解决了该问题。

总结:

在使用多构建模式+多变体的原生项目中,如果需要集成 Flutter Module,需要修改 Flutter SDK 中的 flutter.gradle 文件,添加等同配置,否则无法使用。


关联另一个报错日志,便于遇到该问题的同学搜索到这篇答案,解决问题的方法都是一样的:

Could not determine the dependencies of task ':app:compileDevCeshiJavaWithJavac'.
> Could not resolve all task dependencies for configuration ':app:devCeshiCompileClasspath'.
   > Could not resolve project :flutter.
     Required by:
         project :app
      > Unable to find a matching variant of project :flutter:
          - Variant 'debugApiElements' capability com.example.my_flutter:flutter:1.0:
              - Incompatible attribute:
                  - Required com.android.build.api.attributes.BuildTypeAttr 'ceshi' and found incompatible value 'debug'.
              - Other attributes:
                  - Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
                  - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                  - Required environment 'dev' but no value provided.
                  - Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
                  - Required org.jetbrains.kotlin.platform.type 'androidJvm' but no value provided.
          - Variant 'debugRuntimeElements' capability com.example.my_flutter:flutter:1.0:
              - Incompatible attribute:
                  - Required com.android.build.api.attributes.BuildTypeAttr 'ceshi' and found incompatible value 'debug'.
              - Other attributes:
                  - Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
                  - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                  - Required environment 'dev' but no value provided.
                  - Required org.gradle.usage 'java-api' and found compatible value 'java-runtime'.
                  - Required org.jetbrains.kotlin.platform.type 'androidJvm' but no value provided.
          - Variant 'profileApiElements' capability com.example.my_flutter:flutter:1.0:
              - Incompatible attribute:
                  - Required com.android.build.api.attributes.BuildTypeAttr 'ceshi' and found incompatible value 'profile'.
              - Other attributes:
                  - Found com.android.build.api.attributes.VariantAttr 'profile' but wasn't required.
                  - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                  - Required environment 'dev' but no value provided.
                  - Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
                  - Required org.jetbrains.kotlin.platform.type 'androidJvm' but no value provided.
          - Variant 'profileRuntimeElements' capability com.example.my_flutter:flutter:1.0:
              - Incompatible attribute:
                  - Required com.android.build.api.attributes.BuildTypeAttr 'ceshi' and found incompatible value 'profile'.
              - Other attributes:
                  - Found com.android.build.api.attributes.VariantAttr 'profile' but wasn't required.
                  - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                  - Required environment 'dev' but no value provided.
                  - Required org.gradle.usage 'java-api' and found compatible value 'java-runtime'.
                  - Required org.jetbrains.kotlin.platform.type 'androidJvm' but no value provided.
          - Variant 'releaseApiElements' capability com.example.my_flutter:flutter:1.0:
              - Incompatible attribute:
                  - Required com.android.build.api.attributes.BuildTypeAttr 'ceshi' and found incompatible value 'release'.
              - Other attributes:
                  - Found com.android.build.api.attributes.VariantAttr 'release' but wasn't required.
                  - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                  - Required environment 'dev' but no value provided.
                  - Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
                  - Required org.jetbrains.kotlin.platform.type 'androidJvm' but no value provided.
          - Variant 'releaseRuntimeElements' capability com.example.my_flutter:flutter:1.0:
              - Incompatible attribute:
                  - Required com.android.build.api.attributes.BuildTypeAttr 'ceshi' and found incompatible value 'release'.
              - Other attributes:
                  - Found com.android.build.api.attributes.VariantAttr 'release' but wasn't required.
                  - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                  - Required environment 'dev' but no value provided.
                  - Required org.gradle.usage 'java-api' and found compatible value 'java-runtime'.
                  - Required org.jetbrains.kotlin.platform.type 'androidJvm' but no value provided.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 801ms

你可能感兴趣的:(Flutter,android,flutter)