Android Studio生成APK报错求助:DexArchiveMergerException无法合并dex
Hey there, let's break down how to resolve this common dex merging issue you encountered. First, let's restate the error you ran into for clarity:
Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. > java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
This error nearly always traces back to duplicate dependencies, conflicting library versions, or hitting the Android dex method limit. Since you mentioned you already fixed it quickly, here are the most likely actionable fixes that resolve this issue:
Enable MultiDex in your project
If your app exceeds the 64k method limit (a common cause for this error), enabling MultiDex is essential. Add these configurations to your app-levelbuild.gradle:android { defaultConfig { ... multiDexEnabled true } } dependencies { implementation 'com.android.support:multidex:1.0.3' }If you use a custom
Applicationclass, make it extendMultiDexApplication, or add this override to handle MultiDex initialization:@Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); }Resolve duplicate or conflicting dependencies
Run the command./gradlew app:dependencies(macOS/Linux) orgradlew.bat app:dependencies(Windows) in your project root to list all dependencies. Look for libraries that appear multiple times with different versions. For example, if you see conflicting versions ofcom.android.support:appcompat-v7, force a single compatible version with a resolution strategy:android { ... configurations.all { resolutionStrategy.force 'com.android.support:appcompat-v7:26.1.0' // Match your target SDK version } }Clean and rebuild your project
Cached dex files often cause unexpected conflicts. Go toBuild > Clean Projectin Android Studio, thenBuild > Rebuild Project. You can also manually delete the.gradle,build, andapp/buildfolders to clear all cached files, then sync your project with Gradle.Update tools (if feasible)
While you're using Android Studio 3.0.1 and Gradle 4.1, newer versions include fixes for dex merging bugs. If possible, update to a more recent stable Android Studio version and match the Gradle plugin version in your project-levelbuild.gradle(e.g., Android Studio Arctic Fox pairs with Gradle plugin 7.0.2 and Gradle 7.0.2).
Since you resolved this quickly, it's almost certainly one of these steps—most likely enabling MultiDex or fixing conflicting dependencies—that did the trick.
内容的提问来源于stack exchange,提问作者NoNaMe

