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

问题描述 投票:5回答:2

我在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).

有没有办法将这些实现语句组合在一起,所以我只需要编写一个exclude语句?

编辑

我的具体解决方案基于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'
}
android gradle android-gradle build.gradle android-gradle-3.1.0
2个回答
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
        }
    }
}

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。这基本上做的是强制一切使用特定版本,因此它们不会有任何冲突。

© www.soinside.com 2019 - 2024. All rights reserved.