Android dependency '..' has different vers

2019-02-05 19:36发布

问题:

I migrated to Android Studio 3 and Gradle 4. Then I changed compile to implementation in my build.gradle files. But I get the error:

Error:Execution failed for task ':app:preDebugBuild'.
> Android dependency 'com.google.firebase:firebase-core' has different version for the compile (9.0.0) and runtime (11.6.0) classpath. You should manually set the same version via DependencyResolution

When I change implementation to api the error disappear. But this is not the solution. I have the app module and one library module. App build.gradle has only one dependency:

implementation project(':common-lib')

The apply plugin: 'com.google.gms.google-services' is correctly placed at the bottom of the app build.gradle file (the project worked before migration to Gradle 4)

Dependencies from gradlew app:dependencies (clipping of text):

Compile

debugAndroidTestCompileClasspath - Resolved configuration for compilation for variant: debugAndroidTest
+--- com.google.firebase:firebase-core:9.0.0
|    \--- com.google.firebase:firebase-analytics:9.0.0
|         +--- com.google.android.gms:play-services-basement:9.0.0
|         |    \--- com.android.support:support-v4:23.0.0
|         |         \--- com.android.support:support-annotations:23.0.0
|         +--- com.google.firebase:firebase-common:9.0.0
|         |    +--- com.google.android.gms:play-services-basement:9.0.0 (*)
|         |    \--- com.google.android.gms:play-services-tasks:9.0.0
|         |         \--- com.google.android.gms:play-services-basement:9.0.0 (*)
|         \--- com.google.firebase:firebase-analytics-impl:9.0.0
|              +--- com.google.android.gms:play-services-base:9.0.0
|              |    +--- com.google.android.gms:play-services-basement:9.0.0 (*)
|              |    +--- com.google.firebase:firebase-common:9.0.0 (*)
|              |    \--- com.google.android.gms:play-services-tasks:9.0.0 (*)
|              +--- com.google.android.gms:play-services-basement:9.0.0 (*)
|              +--- com.google.firebase:firebase-iid:9.0.0
|              |    +--- com.google.android.gms:play-services-basement:9.0.0 (*)
|              |    \--- com.google.firebase:firebase-common:9.0.0 (*)
|              \--- com.google.firebase:firebase-common:9.0.0 (*)
\--- project :common-lib
(............)

Runtime

debugAndroidTestRuntimeClasspath - Resolved configuration for runtime for variant: debugAndroidTest
+--- com.google.firebase:firebase-core:9.0.0 -> 11.6.0
|    \--- com.google.firebase:firebase-analytics:11.6.0
|         +--- com.google.android.gms:play-services-basement:11.6.0
|         |    +--- com.android.support:support-v4:25.2.0 -> 26.1.0
|         |    |    +--- com.android.support:support-compat:26.1.0
|         |    |    |    +--- com.android.support:support-annotations:26.1.0
|         |    |    |    \--- android.arch.lifecycle:runtime:1.0.0
|         |    |    |         +--- android.arch.lifecycle:common:1.0.0
|         |    |    |         \--- android.arch.core:common:1.0.0
|         |    |    +--- com.android.support:support-media-compat:26.1.0
|         |    |    |    +--- com.android.support:support-annotations:26.1.0
|         |    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|         |    |    +--- com.android.support:support-core-utils:26.1.0
|         |    |    |    +--- com.android.support:support-annotations:26.1.0
|         |    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|         |    |    +--- com.android.support:support-core-ui:26.1.0
|         |    |    |    +--- com.android.support:support-annotations:26.1.0
|         |    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|         |    |    \--- com.android.support:support-fragment:26.1.0
|         |    |         +--- com.android.support:support-compat:26.1.0 (*)
|         |    |         +--- com.android.support:support-core-ui:26.1.0 (*)
|         |    |         \--- com.android.support:support-core-utils:26.1.0 (*)
|         |    \--- com.google.android.gms:play-services-basement-license:11.6.0
|         +--- com.google.firebase:firebase-common:11.6.0
|         |    +--- com.google.android.gms:play-services-basement:11.6.0 (*)
|         |    +--- com.google.android.gms:play-services-tasks:11.6.0
|         |    |    +--- com.google.android.gms:play-services-basement:11.6.0 (*)
|         |    |    \--- com.google.android.gms:play-services-tasks-license:11.6.0
|         |    \--- com.google.firebase:firebase-common-license:11.6.0
|         +--- com.google.firebase:firebase-analytics-impl:11.6.0
|         |    +--- com.google.android.gms:play-services-basement:11.6.0 (*)
|         |    +--- com.google.firebase:firebase-iid:11.6.0
|         |    |    +--- com.google.android.gms:play-services-basement:11.6.0 (*)
|         |    |    +--- com.google.firebase:firebase-common:11.6.0 (*)
|         |    |    +--- com.google.android.gms:play-services-tasks:11.6.0 (*)
|         |    |    \--- com.google.firebase:firebase-iid-license:11.6.0
|         |    +--- com.google.firebase:firebase-common:11.6.0 (*)
|         |    +--- com.google.android.gms:play-services-tasks:11.6.0 (*)
|         |    \--- com.google.firebase:firebase-analytics-impl-license:11.6.0
|         \--- com.google.firebase:firebase-analytics-license:11.6.0
\--- project :common-lib
(.............)

Edited:

app dependencies

dependencies {
    implementation project(':common-lib')
}

common-lib dependencies

dependencies {
    //android firebase
    implementation "com.google.firebase:firebase-core:$firebase_version"
    implementation "com.google.firebase:firebase-crash:$firebase_version"
    implementation "com.google.firebase:firebase-messaging:$firebase_version"
    implementation "com.google.firebase:firebase-ads:$firebase_version"
    //android support
    implementation "com.android.support:appcompat-v7:$support_version"
    implementation "com.android.support:design:$support_version"
    implementation "com.android.support:cardview-v7:$support_version"
    implementation "com.android.support:percent:$support_version"
    //others
    implementation 'com.google.code.gson:gson:2.8.0'
    implementation 'com.hannesdorfmann:adapterdelegates3:3.0.1'
    implementation 'net.danlew:android.joda:2.9.9'
    implementation 'org.ocpsoft.prettytime:prettytime:4.0.1.Final'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'com.github.simbiose:Encryption:2.0.1'

    //server
    implementation files('libs/xxx.jar')
    implementation files('libs/yyy.jar')
    implementation files('libs/zzz.jar')

    //tests
    testImplementation 'junit:junit:4.12'

}

versions:

ext {
    firebase_version = '11.6.0'
    support_version = '26.1.0'
}

回答1:

It appears that, previously, you were implicitly depending on the common-lib module to export Firebase SDKs to your app module. Now that you've changed from "compile" to "implementation", you're no longer exporting those SDKs. So, what's happening now is this: the google-services plugin is adding v9.0.0 of firebase-core to your app module since it no longer sees it present in the visible classpath of your app module.

You should be able to work around this by manually adding firebase-core to your app module at the correct version. Or, you can continue to export Firebase SDKs from your library module to your app module by switching to an "api" dependency instead of an "implementation" dependency.



回答2:

this worked for me:

add following code into your your buildscript

subprojects {
  project.configurations.all {
     resolutionStrategy.eachDependency { details ->
        if (details.requested.group == 'com.android.support'
              && !details.requested.name.contains('multidex') ) {
           details.useVersion "version which should be used - in your case 11.6.0"
        }
     }
  }
}

Best Regards,

Eddi



回答3:

Changing the version of google-services over the top level gradle file fixed the issue for me. It seems the older version is indirectly injecting the firebase-core versions and updating the firebase version explicitly makes the conflict

classpath 'com.google.gms:google-services:4.0.2' // Just updated the version here.


回答4:

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

put this code in your project gradle