在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;
简短版
只有当我添加足够的依赖项时才会发生这种情况,这是可以预料的。我已经按照说明在Android项目的build.gradle
文件中启用了multidex并添加了multidex依赖项(https://developer.android.com/studio/build/multidex.html),但不确定如何处理"为您的应用程序配置multidex"中的步骤2以用于Flutter应用,甚至是否省略了该步骤是问题的原因。
重现步骤:
- 从工具栏中选择
文件/新建/新的Flutter项目
- 选择"Flutter应用程序"
- 包括Kotlin和Swift支持
- 检查应用程序能够编译和运行
将以下内容添加到
pubspec.yaml
文件中的依赖项中:dependencies: flutter_google_place_picker: "^0.0.1" location: "^1.2.0"
- 在Android Studio中点击
Packages Get
或者在项目目录下运行flutter packages get
修改
android/app/build.gradle
文件,在适当的位置添加以下部分:dependencies { compile 'com.android.support:multidex:1.0.1' } android { defaultConfig { multiDexEnabled true } }
- 从工具栏中选择
运行/运行
我尝试过的其他方法
在
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'
按照多dex步骤处理我的每个依赖项;即修改它们的
build.gradle
文件,启用multidex并添加multidex依赖。- 修改项目及其依赖项中每个
build.gradle
文件的minSdkVersion为21和27,并为它们启用multidex。 - 为我的项目启用代码混淆。
- 将
location: "^1.2.0"
替换为geolocation: "^0.2.1"
完全不启用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 输出
$ 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!