android - 从gradle中的多个依赖项中排除同一组?

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

我在我的Android项目的app模块的build.gradle中有如下代码

implementation('com.google.firebase:firebase-core:16.0.1', {
    exclude group: 'com.android.support'
})
implementation('com.google.firebase:firebase-database:16.0.1', {
    exclude group: 'com.android.support'
})
implementation('com.google.firebase:firebase-auth:16.0.1', {
    exclude group: 'com.android.support'
})
implementation('com.google.firebase:firebase-crash:16.0.1',  {
    exclude group: 'com.android.support'
})

firebase 库都包含我正在使用的 android 支持库的冲突版本,因此我需要排除它以防止构建警告

All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 27.1.1, 26.1.0. Examples include com.android.support:animated-vector-drawable:27.1.1 and com.android.support:support-media-compat:26.1.0 less... (Ctrl+F1) 
There are some combinations of libraries, or tools and libraries, that are incompatible, or can lead to bugs. One such incompatibility is compiling with a version of the Android support libraries that is not the latest version (or in particular, a version lower than your targetSdkVersion).

有没有一种方法可以将这些实现语句组合在一起,这样我只需要编写一个排除语句?

编辑

我基于 Cris 的回答的具体解决方案

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.group == 'com.android.support') {
            details.useVersion '27.1.1'
        }
    }
}

dependencies {
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.google.firebase:firebase-core:16.0.1'
    implementation 'com.google.firebase:firebase-database:16.0.1'
    implementation 'com.google.firebase:firebase-auth:16.0.1'
    implementation 'com.google.firebase:firebase-crash:16.0.1'
}

最佳答案

如官方所述gradle documentation ,您可以按如下方式实现:

configurations {
    implementation {
        exclude group: 'javax.jms', module: 'jms'
        exclude group: 'com.sun.jdmk', module: 'jmxtools'
        exclude group: 'com.sun.jmx', module: 'jmxri'
    }
}

另一种选择是强制特定版本的库组,在这种情况下支持。 official documentation 也涵盖了这一点

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.group == 'org.gradle') {
            details.useVersion '1.4'
            details.because 'API breakage in higher versions' 
            //note that details.because requires Gradle version 4.6 or higher
        }
    }
}

关于android - 从gradle中的多个依赖项中排除同一组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52165198/

相关文章:

Android Studio 2.3 AVD 管理器变灰了吗?

java - 使 Sprite 从边缘反弹

groovy - Gradle createPom 似乎没有读取 gradle.properties

android - 如何在 Android Studio 中查看项目的当前依赖项列表?

android - Build.gradle : Failed to resolve: com. android.support

java - 如何将List的内容传递给另一个Android Activity

android - ABIs : armeabi-v7a in the android version detail mean? 是什么,请提供如何使用它的内部细节?

jenkins - 致命 : The Gradle wrapper has not been found in these directories

android - 更新至Android Studio 3.0和gradle版本4.1:debug/manifest.xml中的未知元素<action>。真的不知道该怎么办?

android - 如何设置 gradle 和 android studio 来进行发布构建?