更新Google Ads依赖至最新版后Android Studio构建出现重复类错误
问题根因
该重复类错误由play-services-ads:20.4.0的传递依赖版本不一致导致:20.4.0版本的广告SDK会同时拉取18.0.1和18.0.3两个版本的play-services-measurement系列组件,相同类存在于不同版本的Jar包中,触发构建校验失败。
解决方案
你可以选择以下任意一种方案解决冲突:
方案1:统一指定measurement组件版本
在App模块的build.gradle文件的dependencies代码块中,显式声明所有冲突的measurement组件为同一版本(推荐使用更高的18.0.3版本),Gradle会自动统一全局依赖版本:
implementation 'com.google.android.gms:play-services-measurement:18.0.3' implementation 'com.google.android.gms:play-services-measurement-base:18.0.3' implementation 'com.google.android.gms:play-services-measurement-impl:18.0.3' implementation 'com.google.android.gms:play-services-measurement-sdk-api:18.0.3'
方案2:排除广告依赖中的measurement组件
如果你的应用未使用Google统计相关的measurement功能,可以直接在广告依赖中排除所有measurement相关的传递依赖,从根源避免版本冲突:
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' }
构建前缓存清理
修改依赖配置后,先执行缓存清理操作再构建,避免旧缓存影响:
- 点击Android Studio顶部菜单栏
File > Invalidate Caches... - 选择
Invalidate and Restart重启IDE - 重启后重新执行项目构建即可
内容的提问来源于stack exchange,提问作者Milan.Tejani
相关产品推荐
相关产品推荐

