简体   繁体   English

Build.gradle问题与实现版本

[英]Build.gradle issue with implementation versions

I receive this warning: 我收到这个警告:

All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). 所有com.android.support库必须使用完全相同的版本规范(混合版本可能导致运行时崩溃)。 Found versions 27.1.1, 27.1.0. 找到的版本27.1.1,27.1.0。 Examples include com.android.support:animated-vector-drawable:27.1.1 and com.android.support:exifinterface:27.1. 示例包括com.android.support:animated-vector-drawable:27.1.1和com.android.support:exifinterface:27.1。

I understand this, but why is this warning shown to me even though all the com.android.support versions are the same? 我理解这一点,但为什么即使所有的com.android.support版本都相同,这个警告也会显示给我? (27.1.1) (27.1.1)

this is the content of my file: 这是我的文件的内容:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.app.sin.retrolist"
        minSdkVersion 18
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    } }

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation 'com.android.support:cardview-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.jakewharton:butterknife:8.8.1'
    implementation 'com.android.volley:volley:1.1.1'
    implementation 'com.squareup.picasso:picasso:2.71828' }

To see why a specific dependency is included in your dependency tree, you can (assuming that your main module is named app and you are on macOS) run a simple: 要查看为什么特定依赖项包含在依赖关系树中,您可以(假设您的主模块名为app并且您在macOS上)运行一个简单的:

./gradlew app:dependencies

In the console output, you will see the full dependency tree and how library versions are resolved. 在控制台输出中,您将看到完整的依赖关系树以及如何解析库版本。

The proper way to deal with dependencies conflicts like this is to explicity define a resolution strategy: 像这样处理依赖关系冲突的正确方法是明确定义解决策略:

configurations.all {
    resolutionStrategy {
        force 'com.android.support:exifinterface:27.1.1'
    }
}

This must be placed in the root of your build.gradle file in the app module. 这必须放在app模块中build.gradle文件的根目录中。

Overriding the conflicting dependency with: 用以下内容覆盖冲突的依赖项:

dependencies {
    ...
    implementation 'com.android.support:exifinterface:27.1.1'
}

is less correct, as your module doesn't directly depends on this library so, if for example future versions of your dependencies won't depend on it anymore, it will still be included in the list of resolved dependencies even if you don't need it. 不太正确,因为你的模块不直接依赖于这个库,所以,如果你的依赖关系的未来版本不再依赖它,它仍将被包含在已解析的依赖关系列表中,即使你不这样做需要它。

Even better, you could define: 更好的是,您可以定义:

ext {
    supportLibVersion = "27.1.1"
}

in your main build.gradle file, and then use it wherever you need it: 在主build.gradle文件中,然后在需要的地方使用它:

configurations.all {
    resolutionStrategy {
        force "com.android.support:exifinterface:$rootProject.supportLibVersion"
    }
}

dependencies {
    ...
    implementation "com.android.support:appcompat-v7:$rootProject.supportLibVersion"
    implementation "com.android.support:recyclerview-v7:$rootProject.supportLibVersion"
    implementation "com.android.support:cardview-v7:$rootProject.supportLibVersion"
    ...
}

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

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