与依赖项“ 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库,因为它包括注释库。我不知道的23.0.1
Tim

如果切换程序兼容性回23.0.1它也不管用
BARQ

我只是用23.0.1替换了23.1.0,在任何依赖项下,对我来说都是有效的。
Shahzad Afridi

添加与您的版本错误相关的注释依赖项。readyandroid.wordpress.com/...
就绪的Android

或者,也许这可以帮助您readyandroid.wordpress.com/...
就绪的Android

Answers:


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'c​​om.android.support:support
annotations

4
使用configurations.all设置对我有用,但不适用于项目级别的文件,该文件最初是我根据上述响应解释为“顶级文件”。它在模块级别的build.gradle文件中
OYRM,2015年

此冲突最初是由Espresso引起的吗?
IgorGanapolsky '16

resolutionStrategy.force'com.android.support:support-annotations:23.4.0'对我没有帮助。.问题是DatePicker,DrawerActions,RecyclerView等的EspressoContribution。('com.android.support.test.espresso :espresso-contrib:2.2.2'){排除模块:'support-annotations'排除模块:'support-v4'}
Ewoks

3
请注意,我们需要build.gradle模块(app)内添加configurations.all {resolutionStrategy.force'com.android.support:support-annotations:23.1.0'} 来解决此问题。
AADProgramming

69

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

在Android Studio的工具栏中。Build> Rebuild Project。


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文件中,警告消失了。

另外,如果您收到列出为冲突的其他任何依赖项,例如支持注释,请尝试将其也从androidTestCompile依赖项中排除。


1
排除了对我
有用

12

你可以尝试使用

  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脚本下的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
因此,您删除了测试库?如果您需要这些库,这不是很有帮助。
裤子

4

我今天遇到了同样的错误:

错误:任务':app:preDebugAndroidTestBuild'的执行失败。>与项目':app'中的依赖项'com.android.support:support-annotations'发生冲突。应用(26.1.0)和测试应用(27.1.1)的已解决版本不同。

我做了什么:

  • 我只是将所有依赖项更新为 27.1.126.1.0
  • 此外,更新了我compileSdkVersion 27targetSdkVersion 27它是26较早

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

更改您的应用程序级别的build.gradle文件:

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

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

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'
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.