升级google-services至4.0.0后Firebase与Play Services依赖版本冲突
我完全懂你碰到的坑——升级com.google.gms:google-services到4.0.0之后,插件的版本对齐机制开始“自作主张”:它会把项目里所有Firebase和Play Services依赖的版本统一替换成你声明的最高版本,结果导致那些依赖低版本的库直接找不到,编译报错。下面给你梳理具体的解决思路和修改方案:
问题根源拆解
Google Services插件4.0.0及以上版本引入了自动版本对齐策略:它会扫描所有Firebase和Play Services相关依赖,自动将它们的版本号统一为项目中出现的最高版本。但如果某些库(比如你用的firebase-appindexing:15.0.1)对应的高版本不存在,或者第三方库(像OneSignal)依赖了特定低版本,就会触发依赖解析失败。
分步解决方案
1. 统一兼容的版本号
首先参考Google官方的版本兼容对照表,把所有Firebase和Play Services依赖调整为互相兼容的版本。比如你可以将Firebase库统一到16.x系列,Play Services库保留15.x系列(这个版本组合是经过验证兼容的):
修改app/build.gradle中的依赖片段:
// Firebase 相关统一版本 implementation 'com.google.firebase:firebase-core:16.0.9' implementation 'com.google.firebase:firebase-invites:16.0.6' implementation 'com.google.firebase:firebase-appindexing:16.0.2' implementation 'com.google.firebase:firebase-ads:16.0.1' // Google Play Services 相关统一版本 implementation 'com.google.android.gms:play-services-auth:15.0.1' implementation 'com.google.android.gms:play-services-gcm:15.0.1' implementation 'com.google.android.gms:play-services-location:15.0.1' implementation 'com.google.android.gms:play-services-base:15.0.1' implementation 'com.google.android.gms:play-services-cast-framework:15.0.1'
2. 强制Gradle使用指定版本(兜底方案)
如果统一版本后仍有冲突,可以在app/build.gradle的android块中添加版本强制策略,直接告诉Gradle必须使用你指定的版本:
android { // 其他原有配置... configurations.all { resolutionStrategy { // 强制指定Firebase核心版本 force 'com.google.firebase:firebase-core:16.0.9' // 强制指定Play Services基础版本 force 'com.google.android.gms:play-services-base:15.0.1' // 按需添加其他需要强制锁定的库版本 } } }
3. 清理第三方库的冲突依赖
像OneSignal、Facebook SDK这类第三方库,内部可能依赖了旧版本的Firebase/Play Services,你可以通过./gradlew app:dependencies命令查看完整依赖树,找出冲突点后针对性排除:
比如给OneSignal添加依赖排除:
implementation('com.onesignal:OneSignal:[3.9.1, 3.99.99]') { exclude group: 'com.google.android.gms' exclude group: 'com.google.firebase' }
修改后的完整配置示例
根目录build.gradle
buildscript { ext.kotlin_version = '1.2.41' repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.1.2' classpath 'com.google.gms:google-services:4.0.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { google() jcenter() mavenCentral() } } task clean(type: Delete) { delete rootProject.buildDir }
app/build.gradle
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin' android { compileSdkVersion 27 buildToolsVersion '27.0.3' defaultConfig { applicationId "com.vanse.android" minSdkVersion 19 targetSdkVersion 27 versionCode 1 versionName "8.0" multiDexEnabled true vectorDrawables.useSupportLibrary = true testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" manifestPlaceholders = [ onesignal_app_id : ONE_SIGNAL_APP_ID, onesignal_google_project_number: "REMOTE" ] } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } compileOptions { targetCompatibility 1.8 sourceCompatibility 1.8 } lintOptions { abortOnError false } // 添加版本强制策略 configurations.all { resolutionStrategy { force 'com.google.firebase:firebase-core:16.0.9' force 'com.google.android.gms:play-services-base:15.0.1' } } } buildscript { repositories { maven { url 'https://plugins.gradle.org/m2/' } } dependencies { classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:0.8.1' } } repositories { maven { url 'https://maven.google.com' } } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') 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' implementation 'com.android.support:support-v4:27.1.1' implementation 'com.android.support:support-vector-drawable:27.1.1' implementation 'com.android.support:multidex:1.0.3' implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.0' implementation 'com.android.support:design: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:support-annotations:27.1.1' implementation 'com.android.support:mediarouter-v7:27.1.1' implementation 'com.android.support:exifinterface:27.1.1' implementation 'com.android.support:customtabs:27.1.1' // Firebase 统一版本 implementation 'com.google.firebase:firebase-core:16.0.9' implementation 'com.google.firebase:firebase-invites:16.0.6' implementation 'com.google.firebase:firebase-appindexing:16.0.2' implementation 'com.google.firebase:firebase-ads:16.0.1' // Google Play Services 统一版本 implementation 'com.google.android.gms:play-services-auth:15.0.1' implementation 'com.google.android.gms:play-services-gcm:15.0.1' implementation 'com.google.android.gms:play-services-location:15.0.1' implementation 'com.google.android.gms:play-services-base:15.0.1' implementation 'com.google.android.gms:play-services-cast-framework:15.0.1' // Download Manager implementation "com.tonyodev.fetch2:fetch2:2.0.0-RC19" implementation "com.tonyodev.fetch2downloaders:fetch2downloaders:2.0.0-RC19" implementation "com.tonyodev.fetch2rx:fetch2rx:2.0.0-RC19" implementation "com.tonyodev.fetchmigrator:fetchmigrator:2.0.0-RC19" // Kotlin Dependency implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" // Network Calls implementation 'com.android.volley:volley:1.1.0' // JSON Parsing implementation 'com.google.code.gson:gson:2.8.2' // Push Notification - 排除冲突依赖 implementation('com.onesignal:OneSignal:[3.9.1, 3.99.99]') { exclude group: 'com.google.android.gms' exclude group: 'com.google.firebase' } // Image Loader implementation 'com.github.bumptech.glide:glide:4.7.1' // Facebook SDK implementation 'com.facebook.android:facebook-android-sdk:4.31.0' // Detect Memory leaks debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4' releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4' // Image Loading library implementation 'com.squareup.picasso:picasso:2.71828' // Image Caching library implementation 'com.squareup.okhttp3:okhttp:3.10.0' // For live radio wave layout implementation 'me.itangqi.waveloadingview:library:0.3.5' // For audio and video player(补全你未写完的依赖) implementation 'com.google.android.exoplayer:exoplayer:2.9.6' } // 必须放在dependencies块之后 apply plugin: 'com.google.gms.google-services'
额外小提示
- 执行
./gradlew app:dependencies命令可以查看完整依赖树,轻松找出隐藏的版本冲突 - 定期关注Firebase和Play Services的官方更新,保持依赖版本的兼容性
- 如果你当前的Android Gradle Plugin版本(3.1.2)较低,可以考虑升级到更稳定的版本,能更好地处理依赖冲突
内容的提问来源于stack exchange,提问作者Abilash

