更新到23.3.0后出现的Android支持库错误

48

我一直在使用android支持库v4 23.1.1,最近试图将其更新到23.3.0(在提问时是最新版本),但是我遇到了以下错误:

错误:与依赖项'com.android.support:support-annotations'冲突。
应用程序(23.3.0)和测试应用程序(23.1.1)的已解析版本不同。
有关详细信息,请参见http://g.co/androidstudio/app-test-app-conflict

到目前为止,我已经找到了这个网址https://code.google.com/p/android/issues/detail?id=206137

我访问了两个链接,但都无法解决我的问题,我该如何解决这个问题?

编辑:

这是我的依赖项

compile 'com.android.support:support-v4:23.3.0'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:recyclerview-v7:23.3.0'
compile 'com.android.support:cardview-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
之前的所有版本都是23.1.1,更新后出现了错误。
编辑:
Gradle Version 2.10
Gradle Plugin Version 2.0.0
buildToolsVersion "23.0.3"
13个回答

102

对于那些仍然遇到这个问题的人,只需将这一行添加到您的依赖项中即可。

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

它解决了我的问题。

更新:

如果您现在遇到此错误,您只需像错误描述中所述那样将新版本代码(在本例中为23.3.0,或者在2018年5月份是 27.1.1)插入到上述解决方案中即可。


1
尝试了你的解决方案,但仍然面对错误:Error:Conflict with dependency 'com.android.support:support-v4'. Resolved versions for app (23.4.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.Gradle 依赖项:androidTestCompile 'com.android.support:support-annotations:23.4.0'你能否提供建议? - Jaydev
3
尝试添加 androidTestCompile 'com.android.support:support-v4:23.4.0'。 - Nongthonbam Tonthoi
仍然出现“Error:Conflict with dependency 'com.android.support:appcompat-v7'。应用程序(23.4.0)和测试应用程序(23.1.1)的已解决版本不同。有关详细信息,请参见http://g.co/androidstudio/app-test-app-conflict。”但是添加`androidTestCompile 'com.android.support:appcompat-v7:23.4.0'`会导致values.xml中的错误,如“Error:(72) Error retrieving parent for item: No resource found that matches the given name 'TextAppearance.AppCompat.Display1'. ...” - Jaydev
3
SO - Stack Overflow - Nongthonbam Tonthoi
由于我升级到Android Studio 2.3,我不得不做同样的事情来构建我的项目。谢谢。 - Jérémy

50

似乎也修复了我的问题,在AS 2.2预览版3中。 - Lara Ruffle Coles

20

只是举例说明Akshayraj的答案

原始的Gradle文件:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    [...]

    compile 'com.android.support:support-annotations:25.3.0'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'

}

收到错误:

错误:在项目“:app”中依赖项“com.android.support:support-annotations”发生冲突。
应用程序(25.1.0)和测试应用程序(23.1.1)的已解析版本不同。
有关详细信息,请参见http://g.co/androidstudio/app-test-app-conflict

修复方法:当我添加以下内容时已经解决了问题:

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

最终文件:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    [...]

    compile 'com.android.support:support-annotations:25.3.0'

    androidTestCompile 'com.android.support:support-annotations:25.3.0'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}

10

我的原始app.gradle文件中包含:

dependencies {
    // App dependencies
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0' 

    // Testing-only dependencies
    androidTestCompile 'com.android.support.test:runner:0.3'
    androidTestCompile 'com.android.support.test:rules:0.3'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
}

导致以下错误:
错误:与依赖项 'com.android.support:support-annotations' 冲突。应用程序 (23.4.0) 和测试应用程序 (22.2.0) 的已解析版本不同。有关详细信息,请参见 http://g.co/androidstudio/app-test-app-conflict

在阅读错误中建议的链接后,我发现了以下内容:

当运行仪器测试时,主 APK 和测试 APK 共享相同的类路径。如果主 APK 和测试 APK 使用不同版本的同一库(例如 Guava),Gradle 构建将失败。如果 Gradle 没有捕获到这一点,则您的应用在测试期间和正常运行期间可能会表现出不同的行为(包括在某些情况下崩溃)。

所以我修改了我的 app.gradle 依赖项如下:

dependencies {
    // App dependencies
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'

    // Testing-only dependencies
    androidTestCompile 'com.android.support:support-annotations:23.3.0'
    androidTestCompile 'com.android.support.test:runner:0.3'
    androidTestCompile 'com.android.support.test:rules:0.3'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
}
尽管进行了上述更改,但Gradle仍然感到不满意 :-(:
错误:依赖项'com.android.support:support-annotations'冲突。应用程序(23.4.0)和测试应用程序(23.3.0)的已解析版本不同。有关详细信息,请参见http://g.co/androidstudio/app-test-app-conflict

测试apk版本的更改是不同的!所以我修改了下面粘贴的版本字符串,这对我起作用了:

(涅槃)

dependencies {
    // App dependencies
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0' // main APK

    // Testing-only dependencies
    androidTestCompile 'com.android.support:support-annotations:23.4.0' //test APK
    androidTestCompile 'com.android.support.test:runner:0.3'
    androidTestCompile 'com.android.support.test:rules:0.3'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
}

一个关于Android Gradle使用的不错示例 --> https://github.com/JakeWharton/u2020/blob/master/build.gradle - MANN
很遗憾对我没用,但我看到你的答案是如何工作的。+1 为解释加分。 - rgv

4
需要翻译的内容如下:

花了我一些时间才摆脱这个错误。但是以下方法适用于我,请尝试:

注意: 我使用的编译版本为26

在build.gradle(Module: app)中的依赖块中,我删掉了androidTestImplementation 'com.android.support.test:runner:1.0.2' 和androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'。所以最后我的代码只剩下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.date.brian.cradletest"
        minSdkVersion 15
        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'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    compile 'com.android.support:design:26.1.0'
    compile 'com.android.support:cardview-v7:26.1.0'
    compile 'com.android.support:recyclerview-v7:26.1.0'
    compile 'com.getbase:floatingactionbutton:1.9.0'
    compile 'de.hdodenhof:circleimageview:2.1.0'
    testImplementation 'junit:junit:4.12'
}

我希望这对您有所帮助!


3

您需要在应用和androidTest APK中使用相同的版本。为此,请将与您的应用程序相同的版本指定为:

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

其中24.1.1是您的应用程序中使用的依赖版本号


2
compile 'com.android.support:design:24.1.1'

1
对我而言,“构建工具版本必须与依赖版本相一致”。比如说,如果构建工具版本是“26.1.0”,Gradle 依赖版本就必须符合它。
最简单的方法是创建一个版本变量并使用它。请参考下面的示例。
ext {
    buildVersion = '26.1.0'
}

dependencies {
    compile "com.android.support:appcompat-v7:${buildVersion}"
}

1

只需排除“注释”。不会有任何损害

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

0

从build.gradle文件中删除测试依赖项以解决问题


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