Android项目打包APK时出现重复类依赖冲突无法生成安装包
问题根因
你遇到的是依赖打包冲突,核心原因是你把仅测试用的appium java-client依赖配置成了implementation,该类型的依赖会被强制打入主APK包体,而该依赖本身的传递依赖(selenium、commons-logging)和其他依赖的传递依赖出现了类重复。
单独运行测试方法不会触发全量APK打包的类重复校验,因此可以正常执行。
解决方案
方案1:修正依赖作用域(优先推荐)
appium java-client属于测试工具依赖,不需要打入主APK,直接将依赖的作用域修改为测试专用即可:
- 若你的测试用例为运行在设备上的 instrumentation 测试,修改为:
androidTestImplementation group: 'io.appium', name: 'java-client', version: '7.5.1'
- 若你的测试用例为运行在本地JVM的单元测试,修改为:
testImplementation group: 'io.appium', name: 'java-client', version: '7.5.1'
修改完成后重新执行打包命令即可解决90%以上的同类冲突。
方案2:手动排除冲突依赖(特殊场景使用)
如果你确实需要将该依赖打入主APK,可手动排除重复的传递依赖,修改build.gradle如下:
首先在android块外层添加全局排除规则:
configurations { // 全局排除commons-logging,使用spring-jcl的实现避免冲突 all*.exclude group: 'commons-logging', module: 'commons-logging' }
再修改dependencies中的appium依赖配置,排除重复的selenium-api:
dependencies { implementation('io.appium:java-client:7.5.1') { exclude group: 'org.seleniumhq.selenium', module: 'selenium-api' } // 统一声明selenium-api版本 implementation 'org.seleniumhq.selenium:selenium-api:3.141.59' // 其余原有依赖保持不变 testImplementation group: 'org.testng', name: 'testng', version: '7.4.0' implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.3.0' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' }
修改完成后执行./gradlew assembleDebug即可正常打包。
内容的提问来源于stack exchange,提问作者maitre
相关产品推荐
相关产品推荐

