Android Studio集成AdMob SDK添加play-services-vision依赖出现重复类报错
问题原因
你遇到的是Google Play Services套件的版本依赖冲突:com.google.android.gms:play-services-vision:11.8.0 属于非常老旧的Play Services版本,它依赖的底层公共库play-services-base、play-services-basement都是11.8.0版本;而你引入的com.google.android.gms:play-services-ads:20.1.0属于较新的版本,依赖的play-services-basement是17.6.0,两个大版本跨度的底层库存在同名类,编译时就会报重复类错误。
解决方案
方案1:升级play-services-vision到匹配的新版本(优先推荐)
你可以将旧版vision依赖升级到和play-services-ads 20.1.0兼容的版本,修改依赖如下即可:
dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation 'androidx.appcompat:appcompat:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' // 替换为适配新版Play Services的vision版本 implementation 'com.google.android.gms:play-services-vision:20.1.3' implementation 'com.google.android.gms:play-services-ads:20.1.0' }
修改后同步Gradle即可解决版本冲突。
方案2:强制指定所有Play Services底层库的版本(如果必须使用11.8.0版本的vision)
如果你的业务逻辑必须依赖11.8.0版本的vision,你可以在模块级build.gradle的android块下方添加强制版本统一配置:
configurations.all { resolutionStrategy { // 强制所有play-services相关依赖都使用17.6.0版本的底层公共库 force 'com.google.android.gms:play-services-basement:17.6.0' force 'com.google.android.gms:play-services-base:17.6.0' } } dependencies { // 原有依赖保持不变 implementation fileTree(dir: "libs", include: ["*.jar"]) implementation 'androidx.appcompat:appcompat:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'com.google.android.gms:play-services-vision:11.8.0' implementation 'com.google.android.gms:play-services-ads:20.1.0' }
注意:该方案属于向下兼容适配,有可能出现旧版vision调用的API在新版底层库中被移除的异常,需要自行测试功能可用性
方案3:排除ads依赖中的basement库
你也可以手动排除play-services-ads引入的高版本basement库,让全局使用旧版本的底层库:
dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation 'androidx.appcompat:appcompat:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'com.google.android.gms:play-services-vision:11.8.0' implementation ('com.google.android.gms:play-services-ads:20.1.0') { exclude group: 'com.google.android.gms', module: 'play-services-basement' } }
注意:该方案同样有可能出现新版ads依赖旧版底层库时的兼容性问题,需要测试广告功能是否正常
内容的提问来源于stack exchange,提问作者user15260712
相关产品推荐
相关产品推荐

