Win10下Android Studio生成APK报错:DexArchiveMergerException无法合并dex
Hey there, let's work through this dex merging issue you're hitting—this is a super common problem, but we can get it sorted with a few targeted steps. First, let's recap the key errors you're seeing:
java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
Configuration 'compile' in project ':app' is deprecated. Use 'implementation' instead
Let's start with the low-hanging fruit first, then dive into dependency conflicts and other fixes.
1. Replace Deprecated compile with implementation/api
That deprecation warning isn't just noise—it's likely contributing to your dependency mess. The compile configuration was replaced with implementation (for internal dependencies) and api (for dependencies you need to expose to other modules) a while back.
- Open your Module-level build.gradle file
- Find every instance of
compileand replace it:- Use
implementationfor most dependencies (this keeps them internal to your app module, reducing conflict risks) - Use
apionly if another module in your project needs to access that dependency
- Use
- Sync your project with Gradle files after making these changes.
2. Enable MultiDex (If Needed)
If your app targets a minSdkVersion lower than 21, Android can't handle more than 65k method references by default. Enabling MultiDex fixes this:
- In your Module-level build.gradle, add
multiDexEnabled trueto thedefaultConfigblock:android { defaultConfig { // ... other configs multiDexEnabled true } } - Add the MultiDex dependency (use the AndroidX version if you're on AndroidX):
dependencies { // For AndroidX implementation 'androidx.multidex:multidex:2.0.1' // For legacy Support Library // implementation 'com.android.support:multidex:1.0.3' } - Update your Application class:
- Either extend
MultiDexApplicationinstead ofApplication:public class MyApp extends MultiDexApplication { // ... your app code } - Or if you can't extend
MultiDexApplication, overrideattachBaseContext:@Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); }
- Either extend
3. Dig Deep Into Dependency Conflicts
You ran gradlew -q dependencies, but let's get more specific to find duplicate or conflicting libraries:
- Run this command in your project root directory (using Android Studio's Terminal or Command Prompt):
gradlew app:dependencies --configuration debugRuntimeClasspath - Look for libraries that appear multiple times with different versions (e.g., different versions of
androidx.appcompatorcom.google.guava). - To fix conflicts, you can either:
- Exclude the duplicate dependency from a specific library. For example, if a third-party library includes an old version of appcompat:
implementation('com.example:some-library:1.0.0') { exclude group: 'androidx.appcompat', module: 'appcompat' } - Force a single version for conflicting libraries. Add this to your Module-level build.gradle inside the
androidblock:configurations.all { resolutionStrategy { // Replace with the version you want to use consistently force 'androidx.appcompat:appcompat:1.4.1' force 'com.google.android.material:material:1.5.0' } }
- Exclude the duplicate dependency from a specific library. For example, if a third-party library includes an old version of appcompat:
4. Clean and Rebuild Properly
You tried cleaning and restarting, but let's do a more thorough reset:
- Close Android Studio
- Delete the
.gradlefolder in your project root directory - Delete the
.gradlefolder in your user directory (usuallyC:\Users\[YourUsername]\.gradle) - Reopen Android Studio, click File > Sync Project with Gradle Files
- Run Build > Clean Project, then Build > Rebuild Project
5. Verify Android Studio and Gradle Versions
Outdated versions can cause unexpected build issues. Make sure:
- You're using the latest stable version of Android Studio (check Help > Check for Updates)
- Your Gradle plugin version matches your Gradle distribution version. You can check this in your Project-level build.gradle:
The Gradle distribution version is set inbuildscript { dependencies { classpath 'com.android.tools.build:gradle:7.4.2' // Use a stable, up-to-date version } }gradle/wrapper/gradle-wrapper.properties:
(Stick to stable releases and ensure plugin/distribution versions are compatible.)distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
After working through these steps, your dex merging error should be resolved. If you're still hitting issues, feel free to share your updated build.gradle files and the full dependency tree output!
内容的提问来源于stack exchange,提问作者dzboot02

