如何解决 gradle 插件造成的依赖冲突?

How to resolve dependency conflict created by gradle plugin?

我正在尝试为 Android O 更新我的应用程序,这样做需要更新以支持库 26。

我的compileSdkVersion是26,targetSdkVersion是26,buildToolsVersion是26.0.1,支持库版本是26.0.1,玩services/firebase消息是11.0.4。我正在使用 Android Studio 3.0 b2 和 android gradle 插件 3.0.0-beta2。看来这个版本对支持库冲突敏感多了。

自从播放服务和 firebase link 针对旧版本的支持库以来,我不得不手动排除支持依赖项以解决冲突:

implementation("com.google.android.gms:play-services-auth:$playServicesVersion",{
    exclude group: 'com.android.support'
})

implementation("com.google.android.gms:play-services-identity:$playServicesVersion",{
    exclude group: 'com.android.support'
})

implementation("com.google.android.gms:play-services-base:$playServicesVersion",{
    exclude group: 'com.android.support'
})

implementation("com.google.android.gms:play-services-analytics:$playServicesVersion",{
    exclude group: 'com.android.support'
})

implementation("com.google.firebase:firebase-messaging:$playServicesVersion",{
    exclude group: 'com.android.support'
})

implementation("com.google.firebase:firebase-analytics:$playServicesVersion",{
    exclude group: 'com.android.support'
})

这解决了我所有的依赖冲突,除了一个。

Google Play Services/Firebase 需要使用 google-services gradle 插件来解析生成的 .json 文件并包含必要的 keys/secrets 在应用程序中。我的 build.grade 有一个像这样的构建脚本块:

buildscript {
    repositories {
        maven { url "https://maven.google.com" }
        jcenter()
    }

    dependencies {
        classpath "com.google.gms:google-services:3.1.0"
    }
}

在脚本的末尾,我应用了插件。注释掉 apply 和 classpath 后,一切都可以正确编译。然而,google-services 3.1.0 似乎添加了对支持库版本 25.2.0 的依赖,我一直无法弄清楚如何覆盖它。消息是:

  • What went wrong: Execution failed for task ':app:preDevelopmentDebugBuild'.

    Android dependency 'com.android.support:support-v4' has different version for the compile (25.2.0) and runtime (26.0.1) classpath. You should manually set the same version via DependencyResolution

请注意,我什至无法降级以支持库 25.4.0,因为我遇到了同样的错误(只需将上面消息中的 26.0.1 替换为 25.4.0)。唯一有效的版本是 25.2.0

运行 ./gradlew app:dependencies 包含类路径依赖项,给我这个:

compile - Compile dependencies for 'main' sources (deprecated: use 'implementation' instead).
\--- com.google.firebase:firebase-core:11.0.4
     \--- com.google.firebase:firebase-analytics:[11.0.4] -> 11.0.4
          +--- com.google.firebase:firebase-analytics-impl:[11.0.4] -> 11.0.4
          |    +--- com.google.android.gms:play-services-basement:[11.0.4] -> 11.0.4
          |    |    \--- com.android.support:support-v4:25.2.0
          |    |         +--- com.android.support:support-compat:25.2.0
          |    |         |    \--- com.android.support:support-annotations:25.2.0
          |    |         +--- com.android.support:support-media-compat:25.2.0
          |    |         |    +--- com.android.support:support-annotations:25.2.0
          |    |         |    \--- com.android.support:support-compat:25.2.0 (*)
          |    |         +--- com.android.support:support-core-utils:25.2.0
          |    |         |    +--- com.android.support:support-annotations:25.2.0
          |    |         |    \--- com.android.support:support-compat:25.2.0 (*)
          |    |         +--- com.android.support:support-core-ui:25.2.0
          |    |         |    +--- com.android.support:support-annotations:25.2.0
          |    |         |    \--- com.android.support:support-compat:25.2.0 (*)
          |    |         \--- com.android.support:support-fragment:25.2.0
          |    |              +--- com.android.support:support-compat:25.2.0 (*)
          |    |              +--- com.android.support:support-media-compat:25.2.0 (*)
          |    |              +--- com.android.support:support-core-ui:25.2.0 (*)
          |    |              \--- com.android.support:support-core-utils:25.2.0 (*)
          |    +--- com.google.firebase:firebase-iid:[11.0.4] -> 11.0.4
          |    |    +--- com.google.android.gms:play-services-basement:[11.0.4] -> 11.0.4 (*)
          |    |    \--- com.google.firebase:firebase-common:[11.0.4] -> 11.0.4
          |    |         +--- com.google.android.gms:play-services-basement:[11.0.4] -> 11.0.4 (*)
          |    |         \--- com.google.android.gms:play-services-tasks:[11.0.4] -> 11.0.4
          |    |              \--- com.google.android.gms:play-services-basement:[11.0.4] -> 11.0.4 (*)
          |    +--- com.google.firebase:firebase-common:[11.0.4] -> 11.0.4 (*)
          |    \--- com.google.android.gms:play-services-tasks:[11.0.4] -> 11.0.4 (*)
          +--- com.google.android.gms:play-services-basement:[11.0.4] -> 11.0.4 (*)
          \--- com.google.firebase:firebase-common:[11.0.4] -> 11.0.4 (*)

看来问题实际上与通过子模块包含的依赖项有关。支持库是通过一个公共子模块包含的,例如:

implementation project(':common')

我也尝试过:

implementation(project(':common'),{transitive = true})

但这并没有什么不同。

唯一可行的方法是直接将支持库包含在我的顶级 build.gradle

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

像这样,您的支持库应该都使用相同的版本编译。只需将模块级别 Gradle 构建脚本中的代码段添加到依赖项。