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

更新Firebase与Google Play服务后Android Studio遇Unable to MergeDex问题

解决Android Dex合并失败问题(Unable to merge dex)

你的错误信息

Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. 
java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

你的build.gradle配置片段

apply plugin: 'com.android.application' 
android { 
    compileSdkVersion 27 
    defaultConfig { 
        applicationId "app.monmos" 
        minSdkVersion 21 
        targetSdkVersion 27 
        versionCode 1 
        versionName "1.0" 
        testInstrumentationRunner "android.support.test.runner.AndroidJUn...
    }
}

这是Android开发里非常常见的依赖冲突或Dex数量超限问题,结合你的配置,给你几个针对性的解决步骤:

  • 启用MultiDex支持:虽然你的minSdkVersion是21(系统默认支持MultiDex),但显式开启能解决大部分Dex合并问题。在defaultConfig块里添加multiDexEnabled true,然后在dependencies中加入依赖:

    dependencies {
        implementation 'com.android.support:multidex:1.0.3'
    }
    

    另外,如果你的Application类继承了自定义类,需要让它继承MultiDexApplication,或者在attachBaseContext方法中调用MultiDex.install(this)。

  • 排查重复依赖:很多时候是不同第三方库引用了相同的类导致冲突。执行Gradle命令查看依赖树:

    ./gradlew app:dependencies
    

    找到重复的依赖项,比如两个不同版本的support库,用exclude排除其中一个,示例:

    implementation('some.library:xxx:1.0') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
    
  • 清理构建缓存:缓存损坏也可能导致这个问题。执行以下操作:

    1. Android Studio菜单栏选择Build -> Clean Project
    2. 然后选择Build -> Rebuild Project
    3. 或者直接用命令行:./gradlew clean assembleDebug
  • 更新Gradle插件和构建工具:旧版本的Gradle插件可能存在Dex合并的bug。在项目根目录的build.gradle中,更新插件版本到对应compileSdk的稳定版,比如:

    buildscript {
        dependencies {
            classpath 'com.android.tools.build:gradle:3.2.1' // 对应compileSdk 27的合适版本
        }
    }
    
  • 检查第三方库SDK版本:确保所有第三方库的compileSdk版本和你的项目保持一致(27),如果有库用了更高版本,要么升级你的compileSdk,要么排除该库的冲突依赖。

内容的提问来源于stack exchange,提问作者Adnan Ahmed

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 03:39:15