android.support libraries not compatible together

2019-07-10 00:06发布

问题:

I just updated my gradle plugin and then got warnings telling me to update some of my implementations. I just upgraded both

com.android.support:appcompat

and

com.android.support:design

to 27.1.1. But now I am getting a warning saying

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:customtabs:26.1.0
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).

Here is the dependencies in the app module of gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "aheschl.studyup"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 9
        versionName '3.1.1'
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.google.firebase:firebase-database:15.0.0'
    implementation 'com.google.firebase:firebase-auth:15.0.0'
    implementation 'com.google.android.gms:play-services-ads:15.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.android.support:design:27.1.1'
}

I understand one of the libraries needs the version to be upgraded but I can not find out which ones. I have the option just to suppress the warning but my app is in production and I don't want to do that. Thanks for any help

回答1:

Put something like this in your root project:

Map<String,String> versionOverrides = [
        'com.android.support:support-v7' : '27.1.1'
        ...
]

def overrideDependencies = { Project project ->
    project.configurations.all {
        // See https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html
        resolutionStrategy {
            eachDependency { DependencyResolveDetails details ->
                String overrideVersion = versionOverrides.get(details.requested.group + ":" + details.requested.name)
                if (overrideVersion != null && details.requested.version != overrideVersion) {
                    logger.debug("Overriding dependency ${details.requested.group}:${details.requested.name} " +
                            "version ${details.requested.version} --> $overrideVersion")
                    details.useVersion overrideVersion
                }
            }
        }
    }
}

subprojects { project ->
    overrideDependencies(project)
}


回答2:

The conflicted dependencies probably comes from Firebase and Google play service library. You need to check it with:

./gradlew app:dependencies

then you can override them by adding the support libraries to your dependencies block. You don't need to use exclude