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


105

我是Android App开发的新手。当我尝试创建一个新项目Android Project时,弹出以下消息。

错误:任务':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。信息:分级任务[:app:generateDebugSources,:app:generateDebugAndroidTestSources,:app:mockableAndroidJar]

这是我的项目的屏幕截图, 单击此处以查看我得到的错误的屏幕截图

我也尝试将此代码添加到我的依赖项中。androidTestCompile'c​​om.android.support:support-annotations:23.3.0'这没有解决。我也尝试了27.1.1和26.1.0 ..都没有解决。


检查以下问题:stackoverflow.com/q/43817004/9611523。希望这会有所帮助。

1
更新Android Studio(以及之后的模拟器和其他一些东西)为我解决了这个问题。

问题是由于注解gradle在Android Studio新项目创建中默认情况下不存在。可能是这个帮助你:readyandroid.wordpress.com/...
就绪的Android

Answers:


181

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

第一个解决方案:将gradle模块的依赖项添加到此行

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

并同步您的项目

注意:如果您使用的是Android Studio 3+,请更改compileimplementation

第二种解决方案:配置文档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+,请更改compileimplementation

对于之间的区别compileimplementation请看一下 。gradle中的实现和编译之间有什么区别


4
我建议使用“实现”而不是编译
SanVed 2018年

60

将以下行添加到您的app.gradle文件中,然后再关闭依赖关系。

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

下面还有屏幕截图,以使您更好地理解。

Configurations.all块

  1. 仅当您希望目标sdk为26时,configurations.all块才有用。如果可以将其更改为27,则无需在app.gradle文件中添加配置块,错误就会消失。

  2. 如果您要从app.gradle文件中删除所有测试实现,则还有另一种方法,它可以解决错误,在这种情况下,您也不需要添加配置块,也不需要更改targetdk版本。

希望能有所帮助。


谢谢,节省我的时间
Manikandan K

1
@ManikandanK乐于助人!
哈什·in那

感谢您提供完整的指示!(关于此类主题的这么多答案就像“添加此内容:“强制'com.android.support:support-annotations:26.1.0'”一样!)(几乎没有上下文!生成错误,在我的情况下为“找不到com.android.support:support-annotations:27.0.0。要求提供者:trainingiq:bugsnag-react-native:unspecified> com.bugsnag:bugsnag-android:4.3.1”
德隆兹18-10-24

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'

2
上帝保佑您,愿android支持库版本控制在地狱烂掉!
穆罕默德·纳德里

那么Android Studio向导会从头开始创建一个有错误的项目吗?¬–
塞巴斯蒂安·布赖特

什么28
alizulfuqar

27

如果您使用的是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

这是由于版本冲突造成的,要解决此问题,只需强制更新支持注释版本,然后在模块上添加以下行即可:app 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,上述解决方案对我
有所

添加此行后,我收到错误消息“错误:任务':app:processDebugManifest'的执行失败。>清单合并失败并出现多个错误,请参阅日志”,请帮助我如何解决
Basant

@Basant我需要查看这是问题所在,因为执行gradle时遇到问题,因此最好为此提一个问题
desgraci

抱歉,@ desgraci会延迟答复,我通过升级IDE缩短了此问题
Basant

@Basant很高兴听到这个消息!
desgraci


7

不用担心,这很简单:

转到“项目”目录结构,然后转到“ Gradle脚本”,然后在其中进入“ 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 '18

3

转到项目中的build.gradle(Module App):

在此处输入图片说明

跟随图片并更改那些版本:

compileSdkVersion: 27
targetSdkVersion: 27

如果是android studio版本2,请使用以下内容更改该行:

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

else用以下行更改该行:

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

希望您会解决您的错误。


2

将此添加到您的gradle文件。

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

1

解决此问题的另一种简单方法是编辑您的build.gradle(应用程序):

  1. 转到android标签并更改compileSdkVersion 26compileSdkVersion 27
  2. 转到defaultConfig标签并更改targetSdkVersion 26targetSdkVersion 27
  3. 该杀dependencies标签和更改implementation 'com.android.support:appcompat-v7:26.1.0',以implementation 'com.android.support:appcompat-v7:27.1.1'

1

我有同样的问题,在build.gradle(Module:app)中在依赖项中添加以下代码行:

dependencies 
{
   ...
   compile '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上进行开发。

总是有最新的,但稳定的版本不稳定的版本alphabeta并且rc,忽略它们在发展。

我在项目中更新了以下所有内容,并且不再遇到这些错误。

编码愉快!:)


0

如果更改目标sdk版本无济于事,则如果您对版本有任何依赖关系,3.0.2请将其更改为3.0.1

例如改变

androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'

androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.1'

0

官方的解释中说明了一个更好的解决方案。我把我以前给出的答案留在水平线以下。

根据那里的解决方案:

使用外部标记,并在顶级build.gradle文件中写下以下代码。您将版本更改为变量而不是静态版本号。

ext {
    compileSdkVersion = 26
    supportLibVersion = "27.1.1"
}

在您的应用程序级别build.gradle文件中更改静态版本号,该版本号已经(Module: app)接近。

android {
    compileSdkVersion rootProject.ext.compileSdkVersion // It was 26 for example
    // the below lines will stay
}

// here there are some other stuff maybe

dependencies {
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    // the below lines will stay
}

同步您的项目,不会有任何错误。


您无需向Gradle脚本添加任何内容。安装必要的SDK,即可解决问题。

根据您的情况,请从“偏好设置”>“ Android SDK”或“ 工具”>“ Android”>“ SDK Manager”中安装以下库

在此处输入图片说明


0

在gradle文件中的依赖项下添加此行

compile 'com.android.support:support-annotations:27.1.1'
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.