简体   繁体   English

如何使用 Gradle resolutionStrategy 强制依赖版本?

[英]How to use Gradle resolutionStrategy to force dependency version?

I am using Android support library com.android.support:appcompat-v7:28.0.0 .我正在使用 Android 支持库com.android.support:appcompat-v7:28.0.0 However, I am also using com.google.android.gms:play-services-nearby:16.0.0 , which depends on an earlier version of the support library.但是,我也在使用com.google.android.gms:play-services-nearby:16.0.0 ,这取决于支持库的早期版本。

To fix this, I used a resolutionStrategy to force the support library to be version 28.0.0, but it isn't working.为了解决这个问题,我使用了一个resolutionStrategy来强制支持库版本为 28.0.0,但它不起作用。 Android Studio is still complaining about conflicting dependencies. Android Studio 仍在抱怨依赖项冲突。 Why isn't my resolution strategy working?为什么我的解决策略不起作用?

apply plugin: 'com.android.application'


apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "rstudio.vedantroy.swarm"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    configurations.all {
        resolutionStrategy.force 'com.android.support:appcompat-v7:28.0.0'
    }
}

dependencies {

    //Developer-added dependencies
    implementation 'com.google.android.gms:play-services-nearby:16.0.0'
    implementation 'com.airbnb.android:mvrx:0.7.0'


    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

You can use this configuration for all support library version to be unified:您可以使用此配置将所有支持库版本统一:

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

This code snippet will find each dependency who has group name com.android.support and forcefully apply the version 28.0.0 , even to the internal dependencies.此代码段将查找具有组名称com.android.support每个依赖项,并强制将版本28.0.0应用到内部依赖项。 This way, you can also specify other dependencies projectwide to use explicit version, eg picasso or glide.这样,您还可以指定项目范围内的其他依赖项以使用显式版本,例如 picasso 或 glide。 You just need to specify its group and specify its specific version in details.useVersion .您只需要指定其组并在details.useVersion指定其特定版本。

Hope this helps :)希望这可以帮助 :)

If anyone need, the script for Gradle Kotlin DSL is the following way:如果有人需要,Gradle Kotlin DSL 的脚本如下:

configurations.all {
    resolutionStrategy {
        eachDependency {
            when (requested.module.toString()) {
                "androidx.fragment:fragment" -> useVersion("1.3.0")
                // ...etc
            }
        }
    }
}

Also you may want to take a look at theofficial docs .此外,您可能想查看官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM