在Android Studio构建时,构建老是会莫名其妙的报错,报错信息如下:
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.
这个错误的大概意思是:我的名为app的module里,com.android.support:support-annotations这个依赖冲突了,app里的版本是26.1.0,但是Test app的版本里是27.1.1。
看一下我叫做app的module里的build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "wzt.mytest"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
这么看来,问题是出在了
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
个人理解是这两个依赖里面都自带了com.android.support:support-annotations依赖,并且版本是27.1.1的,因此我们需要做的就是把这里面的27.1.1的com.android.support:support-annotations换成26.1.0的版本
因此把build.gradle文件修改如下
apply plugin: 'com.android.application'
configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:26.1.0'
}
android {
compileSdkVersion 26
defaultConfig {
applicationId "wzt.mytest"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
即新增了如下代码即可
configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:26.1.0'
}
问题解决参考了下面的网址,不过其他的如exclude方法我自己试了没有效果
https://stackoverflow.com/questions/33317555/conflict-with-dependency-com-android-supportsupport-annotations-resolved-ver