【问题标题】:Android dependency has different version for the compile and runtimeAndroid 依赖在编译和运行时有不同的版本
【发布时间】:2017-11-23 00:13:59
【问题描述】:

将 Android Studio 从 Canary 3 更新到 Canary 4 后,在构建时会抛出以下错误。

Android 依赖 'com.android.support:support-support-v4' 对于编译 (25.2.0) 和运行时 (26.0.0-beta2) 类路径有不同的版本。您应该通过 DependencyResolution 手动设置相同的版本。

我在整个项目中进行了完整的搜索,但 25.1.0 的版本没有使用。

App-build.gradle

android {
compileSdkVersion 26
buildToolsVersion '26.0.0'


defaultConfig {
    applicationId "com.xxx.xxxx"
    minSdkVersion 14
    targetSdkVersion
    versionCode 1
    versionName "1.0"
    multiDexEnabled true

}


buildTypes {
    debug {
        debuggable true
    }
    release {
        debuggable false
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }

    lintOptions {
        abortOnError false
    }

}}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
implementation project(':core')
implementation com.google.android.gms:play-services-gcm:9.0.0'

implementation('com.crashlytics.sdk.android:crashlytics:2.6.5@aar') {
    transitive = true
}
implementation 'com.android.support:multidex:1.0.1'
implementation 'com.flurry.android:analytics:7.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
implementation 'com.jakewharton:butterknife:8.6.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
}

库构建.gradle:

apply plugin: 'com.android.library'
android {
compileSdkVersion 26
buildToolsVersion '26.0.0'

defaultConfig {
    minSdkVersion 14
    targetSdkVersion
    versionCode 1
    versionName "1.0"
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation files('libs/model.jar')
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:percent:26.0.0-beta2'
implementation 'com.android.support:appcompat-v7:26.0.0-beta2'
implementation 'com.android.support:support-core-utils:26.0.0-beta2'

implementation 'com.squareup.retrofit2:retrofit:2.0.2'
implementation 'com.squareup.picasso:picasso:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
implementation 'com.squareup.okhttp3:logging-interceptor:3.2.0'
implementation 'uk.co.chrisjenx:calligraphy:2.2.0'
implementation 'com.google.code.gson:gson:2.2.4'
implementation 'com.android.support:design:26.0.0-beta2'
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.1'

}

注意:项目在 Canary 3 中构建良好

【问题讨论】:

  • 我也遇到了同样的问题,因为我在我的应用程序中使用了两个模块,所以请确保您对所有 gradle 文件使用相同的版本代码。

标签: android android-gradle-plugin build.gradle


【解决方案1】:

您应该能够通过为您的项目运行正确的gradle -q dependencies 命令,准确地看到哪个依赖项将奇数版本作为传递依赖项拉入,如下所述:

https://docs.gradle.org/current/userguide/userguide_single.html#sec:listing_dependencies

一旦你追踪到是什么把它拉进来,你可以在你的 gradle 文件中添加一个排除到特定依赖项的内容,例如:

implementation("XXXXX") {
    exclude group: 'com.android.support', module: 'support-compat'
}

【讨论】:

  • 嗨,我运行了 gradle 依赖命令,并将屏幕截图 url 发布在这里,imgur.com/dL35BaN。我从不在我的项目中使用 Firebase。在 gcm 中添加了该行,但它不起作用
  • @DroidLearner 我可能遗漏了一些东西,但我在您发布的屏幕截图中没有看到对 com.android.support:support-compat 的任何引用。该屏幕截图中还有一个警告,它指的是“编译”配置,我在 OP 的 gradle 文件中看不到该配置。它很可能来自 :core 子模块。你能从那里发布相关的gradle信息吗?
  • 嗨,在这里发布带有依赖树的整个 gradle 文件.. app gradle 文件 -> gist.github.com/anonymous/93affc0d75eb96b59f9fde51332b9716 核心 gradle 文件 ->gist.github.com/anonymous/5c85031f26ff766109061ab1f00b833d 依赖树 ->gist.github.com/anonymous/71dd33b6fa4dc63dd357889e8aff01ee 希望这会有所帮助。
  • 看起来旧版本的库正在被 firebase 引入,而这又是 gms 的传递依赖。您可以通过在任何其他依赖项之前添加: implementation 'com.android.support:support-v4:26.0.0-beta2' 来实现此功能。长期关闭传递依赖以支持显式或使用 resolutionStrategy (docs.gradle.org/current/dsl/…) 可能是更好的方法。
  • 谢谢。不知何故设法修复了传递依赖。 Gradle 构建成功。在运行时它显示所有库类的错误。错误:包retrofit2 不存在错误:包android.support.v7.app 不存在错误:包com.google.gson 不存在。在编译时它没有显示任何错误。
【解决方案2】:

我有同样的错误,解决我的问题是什么。在我的 library 中,我不使用编译或实现,而是使用“api”。所以最后我的依赖:

dependencies {
api fileTree(dir: 'libs', include: ['*.jar'])
api files('libs/model.jar')
testApi 'junit:junit:4.12'
api 'com.android.support:percent:26.0.0-beta2'
api 'com.android.support:appcompat-v7:26.0.0-beta2'
api 'com.android.support:support-core-utils:26.0.0-beta2'

api 'com.squareup.retrofit2:retrofit:2.0.2'
api 'com.squareup.picasso:picasso:2.4.0'
api 'com.squareup.retrofit2:converter-gson:2.0.2'
api 'com.squareup.okhttp3:logging-interceptor:3.2.0'
api 'uk.co.chrisjenx:calligraphy:2.2.0'
api 'com.google.code.gson:gson:2.2.4'
api 'com.android.support:design:26.0.0-beta2'
api 'com.github.PhilJay:MPAndroidChart:v3.0.1'
}

您可以在此链接https://*.com/a/44493379/3479489中找到有关“api”、“实施”的更多信息

【讨论】:

  • 所有 android studio 都推荐使用实现。而且这个另类的解决方案有效。安卓工作室的谷歌工程师需要一个教训来向世界学习。多么令人沮丧的工具
  • 它没有解决问题,消息说:“Android依赖'......'有不同的编译版本”
  • 为我工作。谢谢
  • @KeithLoughnane 这不是坏习惯,是正确的做法和文档推荐的方式
  • @YayoArellano 实施是在绝对需要时明智地使用 api 的建议。这是把所有东西都扔到墙上,看看有什么坚持编程。其中一些可能需要api,但不是全部。你暴露得太多了。
【解决方案3】:

在您的构建脚本中使用此代码(build.gradle 根目录):

subprojects {
  project.configurations.all {
     resolutionStrategy.eachDependency { details ->
        if (details.requested.group == 'com.android.support'
              && !details.requested.name.contains('multidex') ) {
           details.useVersion "version which should be used - in your case 26.0.0-beta2"
        }
     }
  }
}

【讨论】:

  • 它对我有用,记得将 details.useVersion 更改为版本号,如果你只是复制粘贴它会失败
  • 最好的,适用于 rn 0.55,gradle 4.1,构建 gradle tools 3.0.1
  • 确保包含!details.requested.name.contains('multidex') 真的帮助了我。
  • 我试过这个,但它只适用于 com.android.support 冲突。它不适用于 com.google.firebase:firebase-analytics 冲突。 “包含”似乎匹配了太多的包。我在this post 中使用了更简单的解决方案来解决所有冲突,并且效果很好。
  • @user3908686 解决了这个问题,但请解释一下,为什么我们需要添加这个??
【解决方案4】:

将硬编码版本替换为 + 示例:

implementation 'com.google.android.gms:play-services-base:+'
implementation 'com.google.android.gms:play-services-maps:+'

【讨论】:

  • 不完全是最佳实践,因为这可能会导致库有点远离你。前任。他们改变了在新版本中完成工作的方式,突然之间你的代码就不起作用了。虽然您应该努力使用最新版本,但您应该先测试后手动设置版本号,以防止不必要的崩溃和错误。
  • 不是一个好习惯。除了@JonahStarling 说的,如果关闭离线功能也会影响Gradle 构建性能。
【解决方案5】:

我按照上面 Eddi 提到的方法解决了,

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

【讨论】:

    【解决方案6】:

    花了很多时间并从一个比我更了解 android 的朋友那里得到帮助: 应用程序/build.gradle

    android {
        compileSdkVersion 27
    
        // org.gradle.caching = true
    
        defaultConfig {
            applicationId "com.cryptoviewer"
            minSdkVersion 16
            targetSdkVersion 23
            versionCode 196
            versionName "16.83"
            // ndk {
            //     abiFilters "armeabi-v7a", "x86"
            // }
        }
    

    和依赖关系

    dependencies {
        implementation project(':react-native-camera')
       //...
        implementation "com.android.support:appcompat-v7:26.1.0" // <= YOU CARE ABOUT THIS
        implementation "com.facebook.react:react-native:+"  // From node_modules
    }
    

    在 build.gradle 中

    allprojects {
       //...
        configurations.all {
            resolutionStrategy.force "com.android.support:support-v4:26.1.0"
        }
    

    在 gradle.properties 中

    android.useDeprecatedNdk=true
    android.enableAapt2=false
    org.gradle.jvmargs=-Xmx4608M
    

    【讨论】:

    • resolutionStrategy.force 是唯一对我有用的东西。谢谢!
    【解决方案7】:

    这对我有用:

    在依赖部分的app/build.gradle 中添加以下行:

    implementation "com.android.support:appcompat-v7:27.1.0"
    

    在我的情况下是:27.1.1

    【讨论】:

      【解决方案8】:

      我注释掉 //api 'com.google.android.gms:play-services-ads:15.0.1' 同步后它对我有用

      【讨论】:

        【解决方案9】:

        我的答案是将其添加到我的 build.gradle 文件中:

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

        就我而言,将解决策略包含在configurations.all { .. } 块中是必要的。我将configurations.all 块直接放入我的app/build.gradle 文件中(即configurations.all 没有嵌套在其他任何东西中)

        【讨论】:

          【解决方案10】:

          将我的冲突依赖项从实现切换到 api 就可以解决问题。这是 mindorks 的一篇很好的文章,解释了不同之处。

          https://medium.com/mindorks/implementation-vs-api-in-gradle-3-0-494c817a6fa

          编辑:

          这也是我的依赖解决方案

           subprojects {
                  project.configurations.all {
                      resolutionStrategy.eachDependency { details ->
                          if (details.requested.group == 'com.android.support'
                                  && !details.requested.name.contains('multidex')) {
                              details.useVersion "28.0.0"
                          }
                          if (details.requested.group == 'com.google.android.gms'
                                  && details.requested.name.contains('play-services-base')) {
                              details.useVersion "15.0.1"
                          }
                          if (details.requested.group == 'com.google.android.gms'
                                  && details.requested.name.contains('play-services-tasks')) {
                              details.useVersion "15.0.1"
                          }
                      }
                  }
              }
          

          【讨论】:

            【解决方案11】:

            在您的库项目中查看使 compileSdkVersiontargetSdkVersion 版本与您的应用程序相同

            android {
                compileSdkVersion 28
            
                defaultConfig {
                    consumerProguardFiles 'proguard-rules.txt'
                    minSdkVersion 14
                    targetSdkVersion 28
                }
            }
            

            也将所有依赖项设为同一级别

            【讨论】:

              【解决方案12】:

              只需在你的 build.gradle 文件中添加这些行

              resolutionStrategy.force "com.android.support:support-media-compat:26.0.0-beta2"

              resolutionStrategy.force "com.android.support:support-v4:26.0.0-beta2"

              【讨论】:

                【解决方案13】:

                如果有人在 2019 年遇到此依赖问题,请将 Android Studio 更新到 3.4 或更高版本

                【讨论】:

                  【解决方案14】:

                  我通过在 android/build.gradle 文件中升级我的 gradle 依赖项解决了这个问题:classpath 'com.android.tools.build:gradle:3.3.1'(我之前使用的是 3.2 版。

                  【讨论】:

                    【解决方案15】:

                    将此代码添加到您的项目级 build.gradle 文件中。

                    subprojects {
                        project.configurations.all {
                            resolutionStrategy.eachDependency { details ->
                                if (details.requested.group == 'com.android.support'
                                        && !details.requested.name.contains('multidex') ) {
                                    details.useVersion "version which should be used - in your case 28.0.0-beta2"
                                }
                            }
                        }
                    }
                    

                    示例代码:

                    // Top-level build file where you can add configuration options common to all sub-projects/modules.
                    
                    buildscript {
                    
                        repositories {
                            google()
                            jcenter()
                            maven { url 'https://maven.fabric.io/public' }
                        }
                        dependencies {
                            classpath 'com.android.tools.build:gradle:3.2.0'
                            classpath 'io.fabric.tools:gradle:1.31.0'
                    
                            // 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
                    }
                    
                    
                    subprojects {
                        project.configurations.all {
                            resolutionStrategy.eachDependency { details ->
                                if (details.requested.group == 'com.android.support'
                                        && !details.requested.name.contains('multidex') ) {
                                    details.useVersion "28.0.0"
                                }
                            }
                        }
                    }
                    

                    【讨论】:

                      【解决方案16】:

                      在我的情况下,我在两个不同的模块中有以下实现的两个不同版本,所以我将两个实现都更改为版本,即:6.0.2 并且它工作。您可能还需要编写依赖解析,请参阅接受的答案。

                      应用模块

                      implementation 'com.karumi:dexter:5.0.0'
                      

                      公共模块

                      implementation 'com.karumi:dexter:6.0.2'
                      

                      【讨论】:

                        【解决方案17】:

                            configurations.all {
                                resolutionStrategy.force
                                //"com.android.support:appcompat-v7:25.3.1"
                                //here put the library that made the error with the version you want to use
                            }

                        将此添加到 allprojects 内的 gradle(项目)

                        【讨论】:

                          最近更新 更多