You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Win10下Android Studio生成APK报错:DexArchiveMergerException无法合并dex

Fixing "Unable to merge dex" Error in Android Studio (Windows 10)

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 compile and replace it:
    • Use implementation for most dependencies (this keeps them internal to your app module, reducing conflict risks)
    • Use api only if another module in your project needs to access that dependency
  • 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:

  1. In your Module-level build.gradle, add multiDexEnabled true to the defaultConfig block:
    android {
        defaultConfig {
            // ... other configs
            multiDexEnabled true
        }
    }
    
  2. 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'
    }
    
  3. Update your Application class:
    • Either extend MultiDexApplication instead of Application:
      public class MyApp extends MultiDexApplication {
          // ... your app code
      }
      
    • Or if you can't extend MultiDexApplication, override attachBaseContext:
      @Override
      protected void attachBaseContext(Context base) {
          super.attachBaseContext(base);
          MultiDex.install(this);
      }
      

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.appcompat or com.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 android block:
      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'
          }
      }
      

4. Clean and Rebuild Properly

You tried cleaning and restarting, but let's do a more thorough reset:

  1. Close Android Studio
  2. Delete the .gradle folder in your project root directory
  3. Delete the .gradle folder in your user directory (usually C:\Users\[YourUsername]\.gradle)
  4. Reopen Android Studio, click File > Sync Project with Gradle Files
  5. 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:
    buildscript {
        dependencies {
            classpath 'com.android.tools.build:gradle:7.4.2' // Use a stable, up-to-date version
        }
    }
    
    The Gradle distribution version is set in gradle/wrapper/gradle-wrapper.properties:
    distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
    
    (Stick to stable releases and ensure plugin/distribution versions are compatible.)

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:49:33