Android 与gradle常见相关问题及解答(2)

本文介绍了解决Android项目中Gradle依赖冲突的方法。通过调整依赖版本并使用Gradle的解析策略,成功解决了main APK和test APK之间的依赖冲突问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

4.gradle隐藏包冲突问题解决


在编译一个android项目时遇到如下问题。

Warning:Conflict with dependency 'com.android.support:support-annotations' in project ':app'. Resolved versions for app (26.0.0) and test app (25.4.0) differ. See http://g.co/androidstudio/app-test-app-conflict for detail s.

build.gradle依赖部分如下
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:26.0.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test:runner:1.0.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'

这就奇怪了,哪里都找不到25.4.0 这个数字,那么编译时的冲突时怎么产生的

我们进入提示的网页 http://g.co/androidstudio/app-test-app-conflict 

Resolving conflicts between main and test APK

When instrumentation tests are run, both the main APK and test APK share the same classpath. Gradle build will fail if the main APK and the test APK use the same library (e.g. Guava) but in different versions. If gradle didn't catch that, your app could behave differently during tests and during normal run (including crashing in one of the cases).
To make the build succeed, just make sure both APKs use the same version. If the error is about an indirect dependency (a library you didn't mention in your build.gradle) , just add a dependency for the newer version to the configuration ("compile" or "androidTestCompile") that needs it. You can also use  Gradle's resolution strategy mechanism You can inspect the dependency tree by running  ./gradlew :app:dependencies  and  ./gradlew :app:androidDependencies.
看来问题找到了,runner:1.0.0 引用了25.4.0导致的。。。
按照上面英文说明修改如下
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:26.0.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    androidTestCompile 'com.android.support.test:runner:1.0.0'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
    androidTestCompile 'com.android.support:support-annotations:26.0.0'
}

加上最后一行的红色代码后,问题解决。

总结:这种问题知道原因后很简单,但是如果没有思路的话,有时候就会很困惑:到底哪里冒出来了个25.4.0?










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值