我在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配置您的应用程序”,或者是否忽略了该步骤是问题所在。
build.gradle
File/New/New Flutter Project
pubspec.yaml
dependencies: flutter_google_place_picker: "^0.0.1" location: "^1.2.0"
Packages Get
flutter packages get
android/app/build.gradle
dependencies { compile 'com.android.support:multidex:1.0.1' } android { defaultConfig { multiDexEnabled true } }
Run/Run
compile 'com.android.support:multidex:1.0.3' implementation 'com.android.support:multidex:1.0.1' implementation 'com.android.support:multidex:1.0.3'
按照我的每个依赖项的multidex步骤进行操作;即修改其build.gradle文件,启用multidex并添加multidex依赖项。
build.gradle针对我的项目及其依赖项,将minSdkVersion修改为每个文件中的21和27,并为其启用multidex。
location: "^1.2.0"
geolocation: "^0.2.1"
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!
您的两个软件包似乎在它们的传递依存关系上意见不一致。一个想要11.6。+,另一个想要一些Play-service依赖性的11. +。由于11.6.2和11.8.0都存在,所以最终将导致冲突。
如果./gradlew androidDependencies在android/文件夹中运行,则会列出依赖关系解析的结果,其中包括以下内容:
./gradlew androidDependencies
android/
+--- :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' } }