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

Android Studio运行应用时出现Multiple dex files定义冲突构建错误求助

解决Multiple dex files define Lcom/google/android/gms/internal/measurement/zzabn; 构建错误

这个问题我帮很多开发者排查过,核心原因就是项目中重复引入了不同版本的Google Play Services测量模块,导致Dex合并时类定义冲突。下面是几个一步步就能搞定的解决方案:

1. 强制统一所有GMS库版本

项目里的第三方依赖、自己引入的GMS组件,很可能各自用了不同版本的测量模块,我们可以在根目录的build.gradle里强制指定统一版本,让Gradle自动对齐:

allprojects {
    repositories {
        // 保留你原本的仓库配置,比如google()、jcenter()等
    }
    configurations.all {
        resolutionStrategy {
            // 替换成你项目中正在使用的GMS最新兼容版本,比如17.6.0(可根据实际情况调整)
            force 'com.google.android.gms:play-services-measurement-base:17.6.0'
            force 'com.google.android.gms:play-services-analytics:17.6.0'
        }
    }
}

注意:如果你的项目用到了Firebase,一定要保证Firebase和GMS版本匹配,否则还会触发其他冲突。

2. 定位并排除重复依赖

如果强制版本后还是有问题,我们可以先找出到底是哪个依赖引入了重复的模块。在终端执行Gradle依赖树命令:

./gradlew app:dependencies

搜索输出里的com.google.android.gms:play-services-measurement-*相关条目,找到重复引入的来源,然后在对应的依赖里排除掉冲突模块。比如某个第三方库带了旧版本,就这么写:

implementation('com.example:third-party-lib:1.0.0') {
    exclude group: 'com.google.android.gms', module: 'play-services-measurement'
}

3. 开启MultiDex(如果项目方法数超标)

如果你的项目方法数确实超过了65536的限制,单纯解决重复依赖还不够,得开启MultiDex支持:
在app模块的build.gradle里配置:

android {
    defaultConfig {
        // 其他配置...
        multiDexEnabled true
    }
}

dependencies {
    // 如果你用的是AndroidX,换成androidx.multidex:multidex:2.0.1
    implementation 'com.android.support:multidex:1.0.3'
}

然后修改你的Application类:

  • 如果是自定义Application,让它继承MultiDexApplication;
  • 如果不能继承,就在attachBaseContext方法里添加:MultiDex.install(this);

4. 清理重建项目

做完上面的配置后,一定要清理掉旧的构建缓存,再重新构建:
在终端执行:

./gradlew clean
./gradlew assembleDebug

或者直接在Android Studio里点击Build -> Clean Project,然后Build -> Rebuild Project。

内容的提问来源于stack exchange,提问作者Josip Valčić

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:35:48