Android Studio中GMS/Firebase版本不一致警告及崩溃问题排查
最近Firebase宣布Android SDK启用独立版本号,你调整版本后遇到的Gradle警告,大概率是Google Play Services与Firebase库版本不匹配,加上旧版google-services插件不支持新的版本管理机制导致的。虽然编译运行暂时正常,但生产环境的Google Play服务崩溃很可能和这个版本不一致有关,先一步步解决警告问题:
一、升级google-services插件版本
你当前用的com.google.gms:google-services:3.2.1是比较旧的版本,它对Firebase独立版本号的兼容性不好,建议升级到和你Gradle版本兼容的稳定版(比如4.3.10)。修改项目级build.gradle:
buildscript { repositories { jcenter() mavenCentral() maven { url 'https://maven.google.com/' name 'Google' } maven { url 'https://maven.fabric.io/public' } google() } dependencies { classpath 'com.android.tools.build:gradle:3.1.2' // 升级到兼容独立版本号的插件版本 classpath 'com.google.gms:google-services:4.3.10' classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' classpath 'io.fabric.tools:gradle:1.25.4' } }
二、统一关联库的版本
你的play-services-auth:15.0.1和firebase-auth:15.1.0版本不一致,这是警告的核心原因。可以选择两种解决方式:
方式1:手动统一版本
把play-services-auth的版本改成和firebase-auth一致的15.1.0,修改app级build.gradle:
// 替换原来的play-services-auth依赖 implementation 'com.google.android.gms:play-services-auth:15.1.0' implementation 'com.google.android.gms:play-services-location:15.0.1' implementation 'com.google.android.gms:play-services-gcm:15.0.1' implementation 'com.google.firebase:firebase-auth:15.1.0'
方式2:使用Firebase BOM统一管理版本(更推荐)
Firebase BOM会自动帮你管理所有Firebase和关联Google Play Services库的兼容版本,不用手动逐个对齐。在app级build.gradle的dependencies里添加BOM依赖,然后其他Firebase/Play Services库可以不用写版本号:
dependencies { // 添加Firebase BOM implementation platform('com.google.firebase:firebase-bom:29.0.0') // 下面的库不需要写版本号,由BOM自动管理 implementation 'com.google.android.gms:play-services-auth' implementation 'com.google.android.gms:play-services-location' implementation 'com.google.android.gms:play-services-gcm' implementation 'com.google.firebase:firebase-auth' implementation 'com.google.firebase:firebase-core' implementation 'com.google.firebase:firebase-config' implementation 'com.google.firebase:firebase-crash' implementation 'com.google.firebase:firebase-invites' // 你的其他依赖保持不变... }
三、清理并同步Gradle
完成上述修改后:
- 点击Android Studio菜单栏的
Build > Clean Project - 再点击
File > Sync Project with Gradle Files
这样应该就能消除Gradle中的警告了。另外生产环境的崩溃问题,大概率是版本不匹配导致的运行时兼容性错误,版本统一后应该会有所改善。
内容的提问来源于stack exchange,提问作者Satwinder Singh

