应用程序(版本 26.1.0)和测试应用程序(版本 27.1.1)的解决版本不同。

20

这是完整的错误信息 -

Error:Execution failed for task ':app:preDebugAndroidTestBuild'.
> Conflict with dependency 'com.android.support:support-annotations' in project ':app'. Resolved versions for app (26.1.0) and test app (27.1.1) differ. See https://d.android.com/r/tools/test-apk-dependency-conflicts.html for details.

我知道这种问题有很多解决方案,但我是Android Studio的绝对初学者,我无法理解像命令行界面与gradle交互这样的解决方案...

如果有简单的解决方案,请告诉我。非常感谢!


请附上您的应用程序 build.gradle 文件。 - ישו אוהב אותך
10个回答

27

最近我又遇到了这个错误......我只需进入 "Build" -> "Rebuild Project",每次都可以解决问题。


每次我点击“构建项目”时,它都会提示我相同的错误。有什么解决方法吗? - meyasir

26

修改您的build.gradle应用程序中的所有实现,例如:

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

将所有内容更改为最新版本(27.1.1)并同步项目。


这些更改应该在哪里进行?例如,在build.gradle文件中,我应该添加这些代码吗? - ubuntu_noob
1
@Dev_123 在 build.gradle (Apps) 文件中只需更改 version 即可。这些代码来自我的项目,因此您不需要它们。您只需要更改版本号,就可以正常工作了。 - Kopi Bryant

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

7

修改gradle文件中的以下行。

//From 
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//To
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

3

这种错误让我非常不喜欢Android Studio。

但是,对于我来说,解决方案非常简单:编辑app.iml文件并更改com.android.support:support-annotations版本。在这种情况下,很容易找到,只需使用ctrl+F查找并输入“27.1.1”(它应该是唯一的匹配项),然后更改为您的版本“26.1.0”。

换句话说,将其从

<orderEntry type="library" scope="TEST" name="com.android.support:support-annotations:27.1.1@jar" level="project" />

to

<orderEntry type="library" scope="TEST" name="com.android.support:support-annotations:26.1.0@jar" level="project" />

之后,重新构建项目。(仅仅重新构建并不起作用,直到我编辑了app.iml文件。)


1

前往 build.gradle(Module:app) 文件,在 dependencies 中添加 compile 'com.android.support:support-annotations:27.1.1',然后再次点击“立即同步”。


1

只需要几个步骤... 1. 进入module.app文件。 2. 将目标SDK版本和编译SDK版本更改为最新版本(这里是27)。 3. 然后将appcompact版本更改为27.1.1。 4. 同步Gradle文件。


同时,您需要将appcompat版本更改为最新版本,例如:implementation 'com.android.support:appcompat-v7:27.1.1'。 - George Vrynios

0
使用以下代码:

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

代替

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

并将compileSdkVersion从26更改为27

-1

你的根 gradle 文件应包含以下内容:

buildscript {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
    }
}

allprojects {
    repositories {
        jcenter()
        google ()
    }
}

而且模块的 gradle 文件应该包含像这样的库:

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
}
apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    buildToolsVersion "27.0.3"

    defaultConfig {
        applicationId "your.package.name"
        minSdkVersion 17
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }
}


repositories {
    jcenter()
    mavenCentral()
    maven { url 'https://maven.google.com' } // necessary for Android API 26
    maven { url 'https://maven.fabric.io/public' }
    maven { url "https://jitpack.io" }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:cardview-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation 'com.android.support:support-v4:27.1.1'

    implementation 'com.google.firebase:firebase-ads:15.0.0'

    implementation 'com.google.android.gms:play-services-location:15.0.0'
    implementation 'com.google.android.gms:play-services-maps:15.0.0'
}

这只是一个典型的简单Gradle配置示例。根据需要使用。


-1

使用最新稳定版本的支持库

在此跟踪最新版本 https://mvnrepository.com/artifact/com.android.support

截至2018年9月28日,最新版本为28.0.0

  implementation 'com.android.support:appcompat-v7:28.0.0'
  implementation 'com.android.support:support-annotations:28.0.0'

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