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

如何解决Android Studio升级play-services-ads后出现的Duplicate class错误

问题原因

重复类报错是因为com.google.android.gms:play-services-ads:20.4.0依赖的play-services-measurement系列组件存在版本冲突,同时引入了18.0.2和18.0.3两个版本,不同版本里存在同路径的类导致编译失败。

修复方案

方案1:强制统一依赖版本(推荐)

在app模块的build.gradle文件中,添加全局版本强制规则,将所有冲突的测量组件统一指定为18.0.3版本:

configurations.all {
    resolutionStrategy {
        force 'com.google.android.gms:play-services-measurement:18.0.3'
        force 'com.google.android.gms:play-services-measurement-base:18.0.3'
        force 'com.google.android.gms:play-services-measurement-impl:18.0.3'
        force 'com.google.android.gms:play-services-measurement-sdk-api:18.0.3'
    }
}

该方案不需要修改原有依赖声明,适用性最强,不会影响其他功能。

方案2:从广告依赖中排除冲突组件

如果项目中完全没有使用Google测量、Firebase分析相关的功能,可以直接在广告依赖声明时排除掉冲突的测量组件:

implementation ('com.google.android.gms:play-services-ads:20.4.0') {
    exclude group: 'com.google.android.gms', module: 'play-services-measurement'
    exclude group: 'com.google.android.gms', module: 'play-services-measurement-base'
    exclude group: 'com.google.android.gms', module: 'play-services-measurement-impl'
    exclude group: 'com.google.android.gms', module: 'play-services-measurement-sdk-api'
}

注意:如果项目其他地方依赖了上述测量组件,该方案可能会导致运行时异常,请谨慎使用。

方案3:升级所有谷歌服务依赖到兼容版本

如果项目同时使用了Firebase、其他Play Services组件,可以将所有谷歌相关服务依赖升级到对应兼容的最新版本,也能解决版本不一致导致的冲突问题。

后续操作

修改配置后,执行以下操作确保修改生效:

  • 点击Android Studio的File > Invalidate Caches / Restart清理缓存
  • 执行Build > Clean Project清理旧的构建产物
  • 重新执行Build > Make Project编译项目

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 20:15:03