在项目“:app”中与依赖项'com.android.support:support-annotations'存在冲突。应用程序(26.1.0)和测试应用程序(27.1.1)的解决版本不同。

107
我是Android应用程序开发新手。当我尝试创建新项目Android Project时,出现以下错误信息:
Error:Execution failed for task ':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
这是我的项目截图 点击此处查看我收到的错误截图 我还尝试将以下代码添加到我的依赖项中:
androidTestCompile 'com.android.support:support-annotations:23.3.0'
但这并没有起作用。我也尝试了27.1.1和26.1.0,但也不起作用。

请查看此问题:https://stackoverflow.com/q/43817004/9611523。希望能有所帮助。 - user9611523
1
更新Android Studio(以及之后的模拟器和其他一些东西)解决了我的问题。 - user3510955
问题是由于注释 gradle 在 Android Studio 新项目创建中不是默认的。也许这可以帮助你:https://readyandroid.wordpress.com/conflict-with-dependency-com-android-supportsupport-annotations-in-project-app-resolved-versions-for-app-26-1-0-and-test-app-27-1-1-differ/ - Ready Android
15个回答

182

根据您的截图,我找到了两个可行的解决方案:

第一个方案:将以下代码添加到gradle模块的依赖项中


第一个方案:将以下代码添加到gradle模块的依赖项中。
compile 'com.android.support:support-annotations:27.1.1'

并同步您的项目

注意: 如果您使用的是Android Studio 3+,请将compile更改为implementation

第二种解决方案: 在文档https://developer.android.com/studio/build/gradle-tips.html#configure-project-wide-properties中找到配置项目范围属性。

在项目级别gradle文件中添加以下行:

// This block encapsulates custom properties and makes them available to all
// modules in the project.
ext {
    // The following are only a few examples of the types of properties you can define.
    compileSdkVersion = 26
    // You can also use this to specify versions for dependencies. Having consistent
    // versions between modules can avoid behavior conflicts.
    supportLibVersion = "27.1.1"
}

然后要访问此部分,请将compileSdkVersion行更改为:

compileSdkVersion rootProject.ext.compileSdkVersion

dependencies部分,将导入的库更改为以下内容:

compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"

并同步您的项目

注意:如果您正在使用Android Studio 3+,请将compile更改为implementation

关于compileimplementation的区别,请参考此页面: What's the difference between implementation and compile in gradle


4
我建议使用“实现(implementation)”而不是“编译(compile)”。 - Sanved

61
在依赖块之前在您的app.gradle文件中添加以下行。
configurations.all {
    resolutionStrategy {
        force 'com.android.support:support-annotations:26.1.0'
    }
}

下面还有一张截图,以便更好地理解。

Configurations.all block

  1. 如果您想将目标SDK设置为26,则configurations.all块会非常有用。 如果将其更改为27,则可以在不添加app.gradle文件中的配置块的情况下消除错误。

  2. 还有一种方式是,如果您从app.gradle文件中删除所有的test implementation,它也会解决这个错误,并且在这种情况下,您不需要添加configuration块,也不需要更改targetSdk版本。


谢谢,节省了我的时间。 - Manikandan K
1
@ManikandanK 很高兴能帮忙! - Harsh Jain
感谢您提供完整的说明!(在这种主题上的许多答案只是像“添加此:'force'com.android.support:support-annotations:26.1.0'”一样,缺乏上下文!)(不幸的是,我仍然遇到相同的构建错误,在我的情况下是“找不到com.android.support:support-annotations:27.0.0。 要求: trainingiq:bugsnag-react-native:未指定> com.bugsnag:bugsnag-android:4.3.1“) - Dronz

35
如果您使用的是版本26,则依赖项版本应为1.0.13.0.1,即如下所示。
  androidTestImplementation 'com.android.support.test:runner:1.0.1'
  androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
如果您使用版本27,则依赖项版本应该为1.0.23.0.2,即如下所示。
  androidTestImplementation 'com.android.support.test:runner:1.0.2'
  androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

4
愿上帝保佑你,愿Android支持库版本控制的问题不再存在! - Muhammad Naderi
那么Android Studio向导会创建带错误的项目吗? ¬¬ - Sebastian Breit
28怎么样? - alizulfuqar

26

如果你使用的是 Android Studio 3.1 或以上版本

只需将以下内容添加到你的 gradle 依赖项中:

implementation 'com.android.support:support-annotations:27.1.1'

总体来说就是这样:

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.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'

    implementation 'com.android.support:support-annotations:27.1.1'
}

谢谢伙计。许多爱。 - amilab

23
这是由于版本冲突引起的问题,要解决它,只需强制更新支持注释版本,在你的模块中添加以下行:应用gradle implementation ('com.android.support:support-annotations:27.1.1') 希望这能解决你的问题 ;)
编辑
几乎忘记了,你可以声明一个单独的额外属性(https://docs.gradle.org/current/userguide/writing_build_scripts.html#sec:extra_properties)为版本,在你的项目(或顶级)gradle文件中,声明你的支持,或只是为了这个例子,注释版本var ext.annotation_version = "27.1.1" 然后在你的模块gradle中替换它: implementation ("com.android.support:support-annotations:$annotation_version") 这非常类似于@emadabel的解决方案,这是一个很好的替代方法,但没有块或rootproject前缀。

1
感谢Desgraci,以上解决方法帮助了我。 - BharathRao
在添加了这行代码后,现在我遇到了一个错误:“Error:执行任务‘:app:processDebugManifest’时失败。
清单合并器出现多个错误,请参见日志。” 请帮我解决这个问题。
- Basant
@Basant 我需要看一下问题出在哪里,由于执行gradle时出现了问题,最好是为此打开一个问题。 - desgraci
抱歉回复晚了 @desgraci,我通过升级 IDE 解决了这个问题。 - Basant
@Basant 很高兴听到这个消息! - desgraci

9

以下是我的解决方案,将以下代码添加到build.gradle(模块应用程序)中:
compile 'com.android.support:support-annotations:27.1.1'


7
不要担心,这很简单:
进入“项目”目录结构,在其中转到“Gradle Scripts”,然后进入其中的“build.gradle(Module:app)”,双击它。
现在- 向下滚动程序,在其中进入依赖项部分: 像下面这样
依赖项 {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.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'

现在删除最后两行代码并重新构建应用程序,现在它将起作用。

依赖项应为:


依赖项 {

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'

重新构建应用程序,它就能正常工作了!


你没有解释你删除/添加了什么。如果这里需要测试库怎么办? - TheRealChx101

3
请前往您项目中的build.gradle(Module App)文件:

enter image description here

按照图片所示,更改这些版本号:
compileSdkVersion: 27
targetSdkVersion: 27

如果您使用的是Android Studio 2版本: 请将以下行更改为这一行:
compile 'com.android.support:appcompat-v7:27.1.1'

否则,将该行替换为以下行:

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

希望您能解决技术问题并消除bug。

2

Add this to your gradle file.

implementation 'com.android.support:support-annotations:27.1.1'

1

重要更新

前往项目级别的build.gradle文件,定义全局变量

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlinVersion = '1.2.61'

    ext.global_minSdkVersion = 16
    ext.global_targetSdkVersion = 28
    ext.global_buildToolsVersion = '28.0.1'
    ext.global_supportLibVersion = '27.1.1'
}

进入应用级别的build.gradle文件,并使用全局变量

应用级别的build.gradle文件

android {
    compileSdkVersion global_targetSdkVersion
    buildToolsVersion global_buildToolsVersion
    defaultConfig {
        minSdkVersion global_minSdkVersion
        targetSdkVersion global_targetSdkVersion
}
...

dependencies {
    implementation "com.android.support:appcompat-v7:$global_supportLibVersion"
    implementation "com.android.support:recyclerview-v7:$global_supportLibVersion"
    // and so on...
}

一些库的 build.gradle 文件
android {
    compileSdkVersion global_targetSdkVersion
    buildToolsVersion global_buildToolsVersion
    defaultConfig {
        minSdkVersion global_minSdkVersion
        targetSdkVersion global_targetSdkVersion
}
...

dependencies {
    implementation "com.android.support:appcompat-v7:$global_supportLibVersion"
    implementation "com.android.support:recyclerview-v7:$global_supportLibVersion"
    // and so on...
}

解决方法是使所有模块的版本相同,这样就不会有冲突了。
重要提示:
我发现当我更新了 gradle、sdks、库等所有东西的版本时,我遇到的错误就会更少。因为开发人员正在努力让在 Android Studio 上进行开发变得更加容易。
始终使用最新但稳定的版本。不稳定版本包括 alpha、beta 和 rc,在开发中应该忽略它们。
我已经更新了我的项目中的所有内容,我再也没有遇到这些错误了。

愉快编程!:)


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