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

来源:互联网 发布:windows调整字体大小 编辑:程序博客网 时间:2024/06/05 10:24

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 details.

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 mechanismYou 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?






原创粉丝点击