如何通过Gradle解决Google Play服务依赖冲突问题?
我来帮你搞定这个Gradle依赖冲突的问题!从你给出的报错信息来看,核心矛盾是com.google.android.gms:play-services-measurement-base被多个依赖请求了不同版本(15.0.2和15.0.4),导致解析时出现冲突,进而引发其他依赖无法解析的连锁问题。
未修改项目代码的情况下,Gradle编译失败,无法解析firebase-iid、firebase-common、play-services-analytics-impl、play-services-auth-api-phone和play-services-auth-base依赖。
已尝试升级API版本、添加强制依赖firebase-core、升级google-services插件、添加play-services-base依赖,将firebase-messaging升级至17.0.0、crashlytics升级至2.9.3,但仍报错:
The library com.google.android.gms:play-services-measurement-base is being requested by various other libraries at [[15.0.2,15.0.2], [15.0.4,15.0.4]], but resolves to 15.0.4. Disable the plugin and check your dependencies tree using ./gradlew :app:dependencies.
以下是尝试修复后的模块Gradle依赖部分:
apply plugin: 'com.android.application' ... dependencies { implementation project(':library') implementation project(':otherlibrary') implementation 'com.android.volley:volley:1.1.0' implementation('org.apache.httpcomponents:httpmime:4.3.6') { exclude module: 'httpclient' } implementation 'org.apache.httpcomponents:httpclient-android:4.3.5' implementation 'com.google.android.gms:play-services-base:15.0.1' implementation 'com.google.android.gms:play-services-analytics:15.0.2' implementation 'com.google.android.gms:play-services-location:15.0.1' implementation('com.google.android.gms:play-services-ads:15.0.1') { exclude group: 'com.android.support', module: 'customtabs' } implementation 'com.google.android.gms:play-services-identity:15.0.1' implementation 'com.google.android.gms:play-services-auth:15.0.1' implementation 'com.google.android.gms:play-services-drive:15.0.1' implementation 'com.google.firebase:firebase-core:16.0.0' implementation 'com.google.firebase:firebase-messaging:17.0.0' implementation 'com.android.support:multidex:1.0.3' implementation 'com.readystatesoftware.sqliteasset:sqliteassethelper:2.0.1' implementation 'com.squareup.picasso:picasso:2.5.2' implementation 'com.nineoldandroids:library:2.4.0' implementation 'com.daimajia.slider:library:1.1.5@aar' implementation 'com.android.support.constraint:constraint-layout:1.1.0' implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support:mediarouter-v7:27.1.1' implementation 'com.android.support:support-v4:27.1.1' implementation 'com.android.support:design:27.1.1' implementation 'com.android.support:exifinterface:27.1.1' implementation 'com.android.support:support-v13:27.1.1' // https://mvnrepository.com/artifact/com.darwinsys/hirondelle-date4j implementation group: 'com.darwinsys', name: 'hirondelle-date4j', version: '1.5.1' // For RxAndroid and RxJava implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' // Because RxAndroid releases are few and far between, it is recommended you also // explicitly depend on RxJava's latest version for bug fixes and new features. // (see https://github.com/ReactiveX/RxJava/releases for latest 2.x.x version) implementation 'io.reactivex.rxjava2:rxjava:2.1.12' // Crashlytics implementation 'com.crashlytics.sdk.android:crashlytics:2.9.3' // CSV with outputstream writer implementation 'com.opencsv:opencsv:4.1' // Gson implementation 'com.google.code.gson:gson:2.8.5' debugImplementation 'com.android.support.test:runner:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test:rules:1.0.2' androidTestImplementation 'org.hamcrest:hamcrest-library:1.3' androidTestImplementation 'junit:junit:4.12' } apply plugin: 'com.google.gms.google-services'
项目级Gradle配置包含以下内容:
... repositories { maven { url 'https://maven.google.com/' name 'Google' } ... } dependencies { ... classpath 'com.google.gms:google-services:4.0.1' } ...
已尝试按照Firebase官方文档及相关Stack Overflow问题调整依赖版本,但因版本号不匹配,问题仍未解决。
针对这个版本冲突问题,你可以尝试以下步骤:
强制统一
play-services-measurement-base版本
在模块级的build.gradle中,先添加该依赖的显式声明,再通过解析规则强制所有依赖使用同一版本:configurations.all { resolutionStrategy { force 'com.google.android.gms:play-services-measurement-base:15.0.4' } } dependencies { // 其他依赖... implementation 'com.google.android.gms:play-services-measurement-base:15.0.4' }对齐Google Play Services与Firebase的版本
你当前的firebase-core用了16.0.0,而大部分play-services依赖停留在15.x版本,这很容易导致版本兼容问题。建议将所有play-services-*依赖升级到与Firebase 16.x兼容的版本,比如:implementation 'com.google.android.gms:play-services-base:16.1.0' implementation 'com.google.android.gms:play-services-analytics:16.0.4' implementation 'com.google.android.gms:play-services-location:16.0.0' // 其他play-services依赖同步升级到对应兼容版本检查子模块的隐藏依赖
你的项目依赖了:library和:otherlibrary,这些子模块可能也引入了冲突的Google Play Services依赖。运行./gradlew :app:dependencies生成完整依赖树,找到哪些子模块带来了旧版本的play-services-measurement-base,然后在主模块中排除这些冲突依赖,或者直接调整子模块的依赖版本。升级google-services插件
你当前使用的4.0.1版本相对较旧,升级到更高的稳定版本(比如4.3.10)能更好地处理依赖版本冲突:// 项目级build.gradle dependencies { classpath 'com.google.gms:google-services:4.3.10' // 其他classpath依赖... }
内容的提问来源于stack exchange,提问作者OroshiX

