应用程序(22.0.0)和测试应用程序(21.0.3)的已解决版本不同。

180

升级到 API 22 和支持库修订版 22 后,我收到了以下警告:

警告:依赖项 'com.android.support:support-annotations' 冲突。应用程序 (22.0.0) 和测试应用程序 (21.0.3) 的解析版本不同。

Gradle 自身更加宽容,但 Android Studio 不是很宽容。

我没有声明任何依赖项为 21.0.3 …… 是否其中一些依赖库使用了 21.0.3,而 Google 忘记将其与其他批次一起更新?

我的 build.gradle 文件,省略了额外的部分。

android {
  compileSdkVersion 22
  buildToolsVersion '22'

  defaultConfig {
    applicationId "com.REDACTED.android"
    minSdkVersion 14
    targetSdkVersion 22
    renderscriptSupportModeEnabled true
    versionName '1.0.0'
    versionCode 100
  }

  buildTypes {
    release {
      minifyEnabled true
      zipAlignEnabled true
      signingConfig signingConfigs.release
    }

    debug {
      minifyEnabled false
      zipAlignEnabled true
      signingConfig signingConfigs.debug
    }
  }

  dependencies {
    provided 'org.projectlombok:lombok:1.16.2'
    googleCompile 'com.google.android.gms:play-services-base:6.5.87'
    compile 'com.android.support:support-v4:22.0.0'
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'com.android.support:support-v13:22.0.0'
    compile 'com.android.support:cardview-v7:22.0.0'
    compile 'com.android.support:palette-v7:22.0.0'
    compile 'com.android.support:support-annotations:22.0.0'
    compile 'com.github.chrisbanes.photoview:library:1.2.3'
    compile 'org.apache.commons:commons-lang3:3.3.2'
    compile 'commons-io:commons-io:2.4'
    compile 'commons-codec:commons-codec:1.10'
    compile 'com.jakewharton:butterknife:6.1.0'
    compile 'com.jakewharton:disklrucache:2.0.2'
    compile 'com.squareup:otto:1.3.6'
    compile 'com.squareup.picasso:picasso:2.5.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.squareup.okhttp:okhttp:2.2.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
    compile 'com.squareup.okio:okio:1.2.0'
    compile 'com.flaviofaria:kenburnsview:1.0.6'
    compile 'com.edmodo:cropper:1.0.1'
    compile 'com.getbase:floatingactionbutton:1.8.0'
    compile 'com.nispok:snackbar:2.10.2'
    compile 'com.github.ksoichiro:android-observablescrollview:1.5.0'
    compile 'in.srain.cube:grid-view-with-header-footer:1.0.9'
    compile 'de.hdodenhof:circleimageview:1.2.2'
    compile fileTree(dir: 'libs', include: '*.jar')
    // Test Only Dependencies
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
  }

更新:(感谢Mark)

看起来是espresso-contrib。

+--- com.android.support.test:testing-support-lib:0.1 (*)
\--- com.android.support.test.espresso:espresso-contrib:2.0
     +--- com.android.support:recyclerview-v7:21.0.3
     |    +--- com.android.support:support-annotations:21.0.3
     |    \--- com.android.support:support-v4:21.0.3
     |         \--- com.android.support:support-annotations:21.0.3
     +--- com.android.support:support-v4:21.0.3 (*)
     \--- com.android.support.test.espresso:espresso-core:2.0 (*)

5
Gradleзҡ„dependenciesд»»еҠЎеҸҜд»Ҙеё®еҠ©дҪ жүҫеҮәй—®йўҳжүҖеңЁпјҡhttps://gradle.org/docs/current/userguide/tutorial_gradle_command_line.html#para:commandline_dependency_report - CommonsWare
1
谢谢Mark。看起来是espresso-contrib引起的问题... - copolii
2
你可以尝试使用“exclude”来阻止“recyclerview-v7”的21.0.3版本,自己引入22.0.0版本,并祈祷它们足够兼容“espresso-contrib”的需求。就我个人而言,我很惊讶你能够在那么长的依赖列表中保持理智... :-) - CommonsWare
1
没错,这就是解决方法。如果你把它作为答案发布,我会给你点赞的 :) androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.0') { exclude module: 'support-annotations' } - copolii
1
我的同样的问题只需要简单地重启Android Studio就解决了 :) - Lakhwinder Singh Dhillon
显示剩余3条评论
10个回答

287

处理这种情况的第一步是熟悉命令行Gradle。

第二步是运行Gradle依赖关系报告(例如,在项目根目录下运行gradle -q app:dependencies)。这将提供如问题更新中所示的ASCII树,它应该有助于确定是哪个正在请求冲突的构件版本。

第三步是决定需要替换什么。您选择只替换冲突项(support-annotations)。就我个人而言,我会选择错误版本树的根节点(recyclerview-v7),但我不知道在这种情况下是否这样做是最好的选择。

第四步是添加exclude指令以阻止您在第三步中选择的内容:

androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.0') {
    exclude module: 'support-annotations'
}

第五步是对这个更改进行全面测试。你所做的是告诉 espresso-contrib 必须处理 22.0.0 版本的support-annotations。这可能有效,也可能无效。这取决于冲突的向后兼容性。在这种情况下,support-annotations 应该可以胜任。

第六步是享用你选择的饮料,应根据你所在地区和时间选择合适的饮料。


4
我使用了 exclude group: 'com.android.support', module: 'support-annotations',因为你的版本在 gradle 中引发了“无法推断参数类型”的警告。 - appoll
2
可以尝试将此应用于com.android.support.test:runner:0.3、com.android.support.test:rules:0.3和com.android.support.test.espresso:espresso-core:2.2。 - uudashr
1
@OYRM: "你在哪里添加了那个 androidTestCompile 指令?" -- 在 dependencies 中。参考 Chiu-Ki Chan 的 这个示例 - CommonsWare
我一直在Module gradule文件中添加“dependencies”,但可能引入了一个拼写错误。在三次检查语法后,错误已经被清除,androidTestCompile方法及其“exclude”参数已经按照您的描述正常工作。 - OYRM
16
第六步在这里非常关键。 - Odaym
显示剩余8条评论

146
我解决它的方法是在我的build.gradle脚本中添加以下行:

我解决它的方法是在我的build.gradle脚本中添加以下行:

androidTestCompile 'com.android.support:support-annotations:xx.x.x'

xx.x.x替换为您的应用程序正在使用的support-annotations版本 - 这将显示在您的依赖项或Gradle同步消息中,如有同步问题,应该会显示:已解析应用程序的版本(xx.x.x)


哇,我这么做是为了升级Junit,但完全忘记我也可以为其他依赖项这样做。 - Jared Burrows
10
这确实是Google样例的做法(参见链接),注释写着“强制在测试应用程序中使用支持注释,因为它在运行器模块内部使用”。 - desseim
这对我来说是最好的答案。注意:必须更新到“23.1.1”: androidTestCompile 'com.android.support:support-annotations:23.1.1' - David Manpearl
谢谢,在我的情况下,这行代码已经存在,但是明确声明了一个冲突的版本,更新为所需的版本后,一切都像魔术般地运行。 - RAM237
如何将此添加到 build.gradle? - ubuntu_noob

58

实际上,这是Espresso Contrib新版本的一个错误,您可以参考这个解决方法:android-testing/build.gradle

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

2
这是我的问题。票在这里 - theblang
3
这会创建一个警告,@RenanNery 的解决方案更好。 - Diego Palomar
那不起作用。我仍然因为androidTestCompile 'com.android.support.test:runner:0.5'而得到冲突错误。 - IgorGanapolsky
1
@bony - 链接已损坏 - Mushtaq Jameel
这对我有用。我把这个代码块放在“android { testOptions { <here> } }”中。 - jwehrle

23

1
今天(2018年11月10日)这也帮助了我,但是使用版本26.1.0,以避免与版本27.1.1发生冲突。 - J. B. Rainsberger

8

我也遇到了这个问题,以及涉及到appcompat的其他冲突。解决办法是添加测试编译并将它们设置为当前使用的 sdk 版本。在我的情况下,sdk 的版本是 25,代码如下:

androidTestCompile 'com.android.support:support-annotations:25.+'
androidTestCompile 'com.android.support:appcompat-v7:25.+'
androidTestCompile 'com.android.support:support-v4:25.+'
androidTestCompile 'com.android.support:recyclerview-v7:25.+'
androidTestCompile 'com.android.support:design:25.+'

此外,您可以看到我添加了与Android Material Design相关的设计依赖项。
希望对您有所帮助 =)

8

针对SDK版本25

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

3

你可以在Windows上运行gradlew,在Mac/Linux上运行./gradlew,这将在需要时下载依赖项。

您可以检查一个库是否具有对支持注释或任何以错误命名的库的依赖关系,并尝试在gradle中排除它,例如以下示例:

compile("org.apache.maven:maven-ant-tasks:${mavenAntTaskVer}‌​") { exclude group: 'junit' }

错误:与依赖关系“junit:junit”存在冲突。 应用程序(3.8.1)和测试应用程序(4.12)的已解析版本不同。 有关详细信息,请参见g.co/androidstudio/app-test-app-conflict。

这就是我遇到的错误,所以我使用上述gradle命令来解决问题。


这似乎是解决这类问题的一种非常好的方法。此外,请尝试在项目根目录下运行此命令行 _./gradlew :app:dependencies_,它将生成有关项目依赖项的树形结构,并进行仔细检查,然后您将找到冲突所在的位置。 - frank jorsn

2

只需在您的build.gradle文件中删除这些行:

androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

它不是那样工作的。这会给项目带来更多的错误。 - user7691120

0

这是仪器化测试中常见的问题,如此处所述,可以通过在build.gradle中添加androidTestCompile依赖项来简单解决。在我的情况下,出现了appcompat、recyclerview和design依赖项的冲突。通过添加以下行进行解决

    androidTestCompile 'com.android.support:appcompat-v7:23.4.0'
    androidTestCompile 'com.android.support:recyclerview-v7:23.4.0'
    androidTestCompile 'com.android.support:design:23.4.0'

0

我也遇到了26.0.0和27.1.1的同样问题,事实上只需将前者升级到后者即可解决问题。


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