依赖项 'com.android.support:support-annotations' 冲突。应用程序(23.3.0)和测试应用程序(23.1.1)的已解决版本不同。

21

在添加 Espresso 到 Android 项目时,我遇到了这个异常。我已经尝试了伴随异常提示的链接。

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

同时,根据我发现的其他线程,我还添加了以下代码行:

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

但问题仍然存在。我正在使用以下配置:

buildToolsVersion "23.0.2"

androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'

有什么想法,谢谢。


1
你尝试过运行 ./gradlew -q app:dependencies 命令来查看哪个库引起了冲突吗? - IgorGanapolsky
请访问以下链接:https://dev59.com/EFoV5IYBdhLWcg3wAqzy#36835268 这个链接将解决你的问题。 - Sanjay Mallur
6个回答

39

这对我解决了问题“应用程序(24.0.0-beta1)和测试应用程序(23.0.1)的已解决版本不同”。

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

如果你想运行AndroidTest,请不要忘记添加以下代码。

 defaultConfig {
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

1
这是仅有对我有效的答案。需要将configurations.all放在dependencies块的底部。 - IgorGanapolsky

15
dependencies {
    //...

    // Solves "versions for app (23.3.0) and test app (23.1.1) differ"
    androidTestCompile 'com.android.support:support-annotations:23.3.0'

    // Android JUnit Runner
    androidTestCompile 'com.android.support.test:runner:0.5'
    // JUnit4 Rules
    androidTestCompile 'com.android.support.test:rules:0.5'
}

可能是关于另一个库的问题?请将所有com.android.support...库添加到androidTestCompile中。 - Frank
1
感谢您的询问...androidTestCompile 'com.android.support:support-annotations:XX.XX.XX' 您的应用程序版本 - Pierry
Gradle构建正常,但在不同版本的支持库中会显示以下错误: androidTestCompile 'com.android.support.test:rules:0.5' - Nilay Dani

8

现在,在Android Studio上创建新项目时,默认情况下会添加以下依赖项:

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

exclude 部分很可能是为了避免问题中提到的冲突。当我尝试添加 runner:0.5rules:0.5 依赖时,我也遇到了这个问题。我的解决方案是对它们应用与上面相同的代码片段:

androidTestCompile ('com.android.support.test:runner:0.5', {
    exclude group: 'com.android.support', module: 'support-annotations'
})

androidTestCompile ('com.android.support.test:rules:0.5', {
    exclude group: 'com.android.support', module: 'support-annotations'
})

这个对我有用。希望能帮到你。


4

注解库被所有三个依赖项规则:0.5'、runner:05和espresso-core:2.2.2使用,所以以下内容对我有效:

androidTestCompile 'com.android.support.test:runner:0.5', {
    exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile 'com.android.support.test:rules:0.5', {
    exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
}

1
重新构建项目解决了问题。
在Android Studio的工具栏中,选择“构建”>“重新构建项目”。

0
сompile 'com.android.support:support-annotations:23.3.0'
androidTestCompile ("com.android.support.test:runner:0.5"){
   exclude group: 'com.android.support'
}
androidTestCompile ('com.android.support.test:rules:0.5'){
   exclude group: 'com.android.support'
}

这是一个解决方案


为什么这会解决问题?我不明白。我尝试了一下,它并没有解决任何问题。 - IgorGanapolsky
我的依赖版本:<!-- language: gradle --> androidGradleToolsVersion = '2.1.0' androidBuildToolsVersion = '23.0.2' androidSDKVersion = 23 androidSupportLibVersion = '23.3.0'<!-- language: lang-none --> 尝试更改它们。或者尝试使用最新版本。但我认为你仍需要使用上面的代码片段。 - Michael Gaev

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