最佳答案
我在 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;
只有当我添加了足够多的依赖项时才会出现这种情况,这是可以预料到的。我已经按照说明(https://developer.android.com/studio/build/multidex.html)启用了 multidex,并在 Android 项目 build.gradle
文件中添加了 multidex 依赖,但是不确定对于 Flutter App 来说“为 multidex 配置你的应用程序”中的步骤2该怎么做,甚至不确定这一步的遗漏是否是问题所在。
File/New/New Flutter Project
向 pubspec.yaml
中的依赖项添加以下内容:
dependencies:
flutter_google_place_picker: "^0.0.1"
location: "^1.2.0"
Packages Get
in Android Studio or run flutter packages get
in the project directoryModify android/app/build.gradle
to add the following sections in the appropriate places:
dependencies {
compile 'com.android.support:multidex:1.0.1'
}
android {
defaultConfig {
multiDexEnabled true
}
}
Run/Run
from the toolbarReplacing the "compile" dependency in build.gradle
with each of the following:
compile 'com.android.support:multidex:1.0.3'
implementation 'com.android.support:multidex:1.0.1'
implementation 'com.android.support:multidex:1.0.3'
Following the multidex steps for each of my dependencies; i.e. modifying their build.gradle
files, enabling multidex and adding the multidex dependency.
build.gradle
files for my project and its dependencies, and enabling multidex for them.location: "^1.2.0"
with geolocation: "^0.2.1"
Not enabling multidex at all (i.e. skipping step 7 of recreating). This results in the following error:
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!