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

时间:2018-09-04 11:23:07

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

我的Android项目的应用模块中的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'
}

2 个答案:

答案 0 :(得分:6)

如官方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
        }
    }
}

答案 1 :(得分:0)

我通常通过将其放在gradle文件中来解决此错误:

// use default version for all support repositories
configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion 'PUT_THE_VERSION_YOU_WANT' //latest would be 28.0.0-rc02
            }
        }
    }
}

您可能必须在multiDexEnabled内添加android true。基本上,这是强制所有内容使用特定版本,因此它们不会有任何冲突。