使用android studio,可能在建立一个空白工程的时候就会编译报错,尤其是3.x版本上线以后,此类错误层出不穷。
报错代码:
Error:Execution failed for task ':app:preDebugAndroidTestBuild'.
> Conflict with dependency 'com.android.support:support-annotations' in project ':app'. Resolved versions for app (26.1.0) and test app (27.1.1) differ.
See https://d.android.com/r/tools/test-apk-dependency-conflicts.html for details.
大致意思是:com.android.support:support-annotations这个依赖包在实际的app中的版本是26.1.0,但是在测试app中的版本是27.1.1 。为了解决这个冲突,我们需要做的就是把这里面的27.1.1的com.android.support:support-annotations换成26.1.0的版本。
网上搜了很多方法都没有用。
现存的网上的方法:在dependencies中添加
androidTestCompile 'com.android.support:support-annotations:26.1.0'
实际使用后没有解决问题。
根据实际情况分析,冲突是出现在
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
解决问题:在app下的gradle文件中增加以下代码
configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:26.1.0'
}
强制将annotation版本变成26.1.0.
亲测有效。