与依赖关系 'com.android.support:support-annotations' 冲突。应用程序(23.1.0)和测试应用程序(23.0.1)的已解决版本不同。

119

构建时我遇到以下错误:

Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (23.1.0) and test app (23.0.1) differ.

这是我的Gradle依赖项

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:support-v4:23.1.0'
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:design:23.1.0'
    compile 'com.android.support:cardview-v7:23.1.0'
    compile 'com.android.support:recyclerview-v7:23.1.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.squareup:otto:1.3.8'
    compile 'com.snappydb:snappydb-lib:0.5.2'
    compile 'com.esotericsoftware.kryo:kryo:2.24.0'
    compile 'com.google.dagger:dagger:2.0.1'
    apt 'com.google.dagger:dagger-compiler:2.0.1'
    compile 'javax.annotation:javax.annotation-api:1.2'
    compile 'io.reactivex:rxandroid:1.0.1'
    compile 'io.reactivex:rxjava:1.0.14'
    compile 'com.google.android.gms:play-services-location:8.1.0'
    compile 'com.google.android.gms:play-services-gcm:8.1.0'
    compile 'org.apache.commons:commons-lang3:3.4'
    testCompile 'junit:junit:4.12'
    testCompile 'org.hamcrest:hamcrest-library:1.3'
    testCompile 'org.mockito:mockito-core:1.10.19'
    androidTestCompile 'com.android.support.test:runner:0.4'
    androidTestCompile 'com.android.support.test:rules:0.4'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.1'
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
}

我该怎么修复这个问题?


我能告诉你,23.1.0 依赖于 appcompat lib,因为它包括 annotations lib。但是我不知道 23.0.1 是什么。 - Tim
如果我将appcompat切换回23.0.1,它仍然无法工作。 - barq
我刚刚在依赖项中将23.1.0替换为23.0.1,对我有效。 - Shahzad Afridi
添加与您版本错误相关的注释依赖项。https://readyandroid.wordpress.com/errorexecution-failed-for-task-apppredebugandroidtestbuild-conflict-with-dependency-com-android-supportsupport-annotations-android/ - Ready Android
或许这可以帮助您:https://readyandroid.wordpress.com/conflict-with-dependency-com-android-supportsupport-annotations-resolved-versions-for-app-23-1-0-and-test-app-23-0-1-differ/ - Ready Android
9个回答

208

您可以在测试中使用以下方式强制使用注释库:

androidTestCompile 'com.android.support:support-annotations:23.1.0'

类似这样:

  // Force usage of support annotations in the test app, since it is internally used by the runner module.
  androidTestCompile 'com.android.support:support-annotations:23.1.0'
  androidTestCompile 'com.android.support.test:runner:0.4.1'
  androidTestCompile 'com.android.support.test:rules:0.4.1'
  androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
  androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
  androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.1'

另一种解决方案是在顶层文件中使用此方法:

configurations.all {
    resolutionStrategy.force 'com.android.support:support-annotations:23.1.0'
}

8
这行代码是解决方案:androidTestCompile 'com.android.support:support-annotations:23.1.0'。 - barq
4
使用"configurations.all"设置对我有效,但不是在项目级别的文件中,这是我最初从上面的回复中理解为"顶层文件"。它在模块级别的build.gradle文件中。 - OYRM
这个冲突最初是由Espresso引起的吗? - IgorGanapolsky
resolutionStrategy.force 'com.android.support:support-annotations:23.4.0' 对我没有帮助.. 问题在于DatePicker、DrawerActions、RecyclerView等的EspressoContribution.. ('com.android.support.test.espresso:espresso-contrib:2.2.2'){ exclude module: 'support-annotations' exclude module: 'support-v4' } - Ewoks
3
需要在应用程序模块的 build.gradle 文件中添加 configurations.all { resolutionStrategy.force 'com.android.support:support-annotations:23.1.0' } 配置,以解决问题。请注意,这是一个重要的细节,需要明确指出。 - AADProgramming
显示剩余3条评论

69

项目重建解决了我的问题。

在Android Studio工具栏中... 依次选择“构建” > “重建项目”。


25

来源: CodePath - 使用Espresso进行UI测试

  1. 最后,我们需要引入Espresso依赖并在我们的应用程序build.gradle中设置测试运行器:
// build.gradle
...
android {
    ...
    defaultConfig {
        ...
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    ...
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') {
        // Necessary if your app targets Marshmallow (since Espresso
        // hasn't moved to Marshmallow yet)
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test:runner:0.5') {
        // Necessary if your app targets Marshmallow (since the test runner
        // hasn't moved to Marshmallow yet)
        exclude group: 'com.android.support', module: 'support-annotations'
    }
}

我已将此添加到我的gradle文件中,警告消失了。

另外,如果您发现其他依赖项出现冲突,例如support-annotations,请尝试从androidTestCompile依赖项中排除它。


1
不包括单独的androidtestcompile在内,这对我来说可行。 - Nantha kumar

13

你可以尝试使用

  androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})

代替

androidTestCompile 'com.android.support.test:runner:0.4.1'

androidTestCompile 'com.android.support.test:rules:0.4.1'

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'

6

我遇到了这个错误

错误:执行任务“:app:preDebugAndroidTestBuild”失败。在项目“:app”中依赖项“com.android.support:support-annotations”存在冲突。应用程序(26.1.0)和测试应用程序(27.1.1)的解析版本不同。请参见https://d.android.com/r/tools/test-apk-dependency-conflicts.html获取详细信息。

我在 Gradle Scripts 下的 build.gradle 文件中有以下依赖项

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.android.support:support-vector-drawable: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'
}

因此,我通过注释以下依赖项来解决了它。
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'

所以我的依赖关系看起来像这样:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.android.support:support-vector-drawable: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'
}

希望这能有所帮助!

1
那么你移除了测试库?如果你需要这些库,这并不是很有帮助。 - Pants

4
我今天也遇到了同样的错误:
错误:执行任务':app:preDebugAndroidTestBuild'失败。项目':app'中的依赖项'com.android.support:support-annotations'存在冲突。应用程序(版本号为26.1.0)和测试应用程序(版本号为27.1.1)的解析版本不同。
我所做的是:
我简单地更新了所有的依赖项,将它们从26.1.0更新到了27.1.1;
我还将之前的compileSdkVersion 27和targetSdkVersion 27更新为27;
这样,com.android.support:support-annotations的错误就消失了!
参考:
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    implementation 'com.android.support:design:27.1.1'
    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'
}

1
在我的情况下,我在应用程序级别的build.gradle的依赖项中添加了以下代码。
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})

之后,我清理了项目并重新构建。我的问题得到解决。


0

试试这个:

apply plugin: 'com.android.application'

android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.example.yourpackagename"
    minSdkVersion 15
    targetSdkVersion 27
    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:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
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'
}

0

修改你的应用程序级别的 build.gradle 文件:

implementation 'com.android.support:appcompat-v7:23.1.0'

 implementation 'com.android.support:appcompat-v7:23.0.1'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接