【问题标题】:Conflict with dependency after creating new project in android studio在android studio中创建新项目后与依赖冲突
【发布时间】:2018-11-07 15:17:29
【问题描述】:

我正在使用 android studio 3.0.1 在我创建新项目后它会给出以下错误:

错误:任务 ':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 为 详情。

这是我的应用级 build.gradle 文件

apply plugin: 'com.android.application'

    android {
        compileSdkVersion 26
        defaultConfig {

            minSdkVersion 19
            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:27.1.1'
        implementation 'com.android.support.constraint:constraint-layout:1.1.0'
        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'
    }

这是我的顶级 build.gradle 文件

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

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

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

task clean(type: Delete) {
    delete rootProject.buildDir
}

根据这篇文章: Error While creating new project with android studio 3.0.1

我改变了我的

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

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

但是因为我正在使用

targetSdkVersion 26

我不应该使用支持库 27.1.1。

我该如何解决这个问题

接受任何帮助。 谢谢你:)

【问题讨论】:

  • 这里有多个相同的依赖问题,尝试排除一个。
  • targetSdkVersion 与您的支持库版本无关。支持库版本与compileSdkVersion 相关。如果compileSdkVersion26,那么支持库版本应该是26.x.x,如果是27,那么支持库版本应该是27.x.x

标签: android android-studio build


【解决方案1】:

如果您使用的支持库版本为 27。

com.android.support:appcompat-v7:27.1.1

改成

targetSdkVersion 27
compileSdkVersion 27

我想它会解决你的答案

【讨论】:

    【解决方案2】:

    尝试将其添加到build.gradle 的底部(应用级别一)

    configurations.all {
        resolutionStrategy.eachDependency { details ->
            def requested = details.requested
            if (requested.group == 'com.android.support') {
                if (!requested.name.startsWith("multidex")) {
                    details.useVersion '26.1.0'
                }
            }
        }
    
    }
    

    感谢Eugen Pechanec

    【讨论】:

      【解决方案3】:

      您需要确保 compileSdkVersion 为 27。

      您需要从 espresso-Core 中排除支持库,如下所示:

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

      【讨论】:

        【解决方案4】:

        这是由于您添加的多个库发生冲突而发生的 Gradle 文件。如果您的目标版本是 26,那么您必须使用 26 API 版本的库。只需进入 项目结构 -> .idea -> 库 -> 删除文件夹。重启 android studio 或 Rebuild 并清理项目。这样做会解决问题。

        【讨论】:

        • @Learning Always 如果对您有帮助,请接受答案。因此其他用户可以按照此步骤操作。谢谢
        【解决方案5】:

        这完全取决于您的 targetSdkVersion 版本。如果它是 26,你应该使用实现 'com.android.support:appcompat-v7:26.1.0',如果它是 27,你应该使用实现 'com.android.support:appcompat-v7:27.1.1'。之后同步项目,如果不工作,请尝试清理并重建项目。

        【讨论】:

          猜你喜欢
          • 2018-05-18
          • 1970-01-01
          • 2018-01-28
          • 1970-01-01
          • 2021-09-21
          • 2018-01-16
          • 1970-01-01
          • 2016-11-05
          • 2019-08-08
          相关资源
          最近更新 更多