一尘不染

Flutter的Multidex问题

flutter

我在Android Studio中使用Flutter使用gradle编译时遇到以下错误:

Dex: Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/internal/zzcew;
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/internal/zzcew;

[... stacktrace omitted for brevity ...]

* What went wrong:
Execution failed for task ':app:transformDexArchiveWithDexMergerForDebug'.
> com.android.build.api.transform.TransformException: com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/internal/zzcew;

短版

仅在我添加了足够的依赖项时(如预期的那样),才会发生这种情况。我已经build.gradle按照说明(https://developer.android.com/studio/build/multidex.html)启用了multidex并在Android项目文件中添加了multidex依赖关系,但是不确定如何处理其中的步骤2为Flutter应用程序“为multidex配置您的应用程序”,或者是否忽略了该步骤是问题所在。

重新创建步骤:

  1. File/New/New Flutter Project从工具栏中选择
  2. 选择“ Flutter Application”
  3. 包括Kotlin和Swift支持
  4. 检查应用编译并运行
  5. 在依赖项中添加以下内容pubspec.yaml
        dependencies:
      flutter_google_place_picker: "^0.0.1"
      location: "^1.2.0"
  1. Packages Get在Android Studio中点击或flutter packages get在项目目录中运行
  2. 修改android/app/build.gradle以在适当的位置添加以下部分:
        dependencies {
      compile 'com.android.support:multidex:1.0.1'
    }
    android {
        defaultConfig {
            multiDexEnabled true
        }
    }
  1. Run/Run从工具栏中选择

我尝试过的其他事情

  1. build.gradle以下每个替换“ compile”依赖项:
        compile 'com.android.support:multidex:1.0.3'
    implementation 'com.android.support:multidex:1.0.1'
    implementation 'com.android.support:multidex:1.0.3'
  1. 按照我的每个依赖项的multidex步骤进行操作;即修改其build.gradle文件,启用multidex并添加multidex依赖项。

  2. build.gradle针对我的项目及其依赖项,将minSdkVersion修改为每个文件中的21和27,并为其启用multidex。

  3. 为我的项目启用缩小功能。
  4. 替换location: "^1.2.0"geolocation: "^0.2.1"
  5. 完全不启用multidex(即跳过重新创建的步骤7)。这将导致以下错误:
        FAILURE: Build failed with an exception.

    * What went wrong:
    Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
    > java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

颤振医生输出

    $ flutter doctor -v
    [√] Flutter (Channel beta, v0.2.8, on Microsoft Windows [Version 10.0.16299.371], locale en-GB)
        • Flutter version 0.2.8 at D:\flutter
        • Framework revision b397406561 (2 weeks ago), 2018-04-02 13:53:20 -0700
        • Engine revision c903c217a1
        • Dart version 2.0.0-dev.43.0.flutter-52afcba357

    [√] Android toolchain - develop for Android devices (Android SDK 27.0.3)
        • Android SDK at C:\Users\Dave\AppData\Local\Android\sdk
        • Android NDK location not configured (optional; useful for native profiling support)
        • Platform android-27, build-tools 27.0.3
        • Java binary at: D:\AndroidDev\jre\bin\java
        • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02)
        • All Android licenses accepted.

    [√] Android Studio (version 3.1)
        • Android Studio at D:\AndroidDev
        • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02)

    [√] Connected devices (1 available)
        • Android SDK built for x86 64 • emulator-5554 • android-x64 • Android 5.1.1 (API 22) (emulator)

    • No issues found!

阅读 1984

收藏
2020-08-13

共1个答案

一尘不染

您的两个软件包似乎在它们的传递依存关系上意见不一致。一个想要11.6。+,另一个想要一些Play-service依赖性的11.
+。由于11.6.2和11.8.0都存在,所以最终将导致冲突。

如果./gradlew androidDependenciesandroid/文件夹中运行,则会列出依赖关系解析的结果,其中包括以下内容:

+--- :flutter_google_place_picker (variant: release)
+--- com.google.android.gms:play-services-location:11.8.0@aar
+--- com.google.android.gms:play-services-places:11.6.2@aar
+--- com.google.android.gms:play-services-maps:11.6.2@aar
+--- com.google.android.gms:play-services-base:11.8.0@aar
+--- com.google.android.gms:play-services-tasks:11.8.0@aar
+--- com.google.android.gms:play-services-basement:11.8.0@aar

这些11.6.2和11.8.0软件包不能一起使用。要解决此问题,您需要修补依赖项以使其彼此一致,或者将依赖项覆盖添加到android/app/build.gradle文件的最高级别并希望达到最佳效果:

configurations.all {
    resolutionStrategy {
        force 'com.google.android.gms:play-services-places:11.8.0'
        force 'com.google.android.gms:play-services-location:11.8.0'
    }
}
2020-08-13