Android Studio Dolphin中Firebase初始化失败问题求助
问题重现
配置Firebase依赖后出现以下错误:
2023-10-31 11:45:25.623 6335-6335/com.example.management W/FirebaseApp: Default FirebaseApp failed to initialize because no default options were found. This usually means that com.google.gms:google-services was not applied to your gradle project.
2023-10-31 11:45:25.623 6335-6335/com.example.management I/FirebaseInitProvider: FirebaseApp initialization unsuccessful
解决方案
确认
google-services.json文件的存在与位置
从Firebase控制台下载的google-services.json必须放在app模块的根目录(与src文件夹同级),缺少该文件会导致Firebase无法获取初始化配置。调整gradle插件的应用顺序
在应用级build.gradle中,id 'com.google.gms.google-services'必须放在所有依赖声明的最后,示例配置:plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' } // 其他项目配置... dependencies { implementation(platform("com.google.firebase:firebase-bom:32.4.0")) implementation 'com.google.firebase:firebase-auth-ktx:22.2.0' implementation 'com.google.firebase:firebase-firestore:24.9.1' implementation 'com.google.firebase:firebase-analytics-ktx:21.5.0' implementation 'com.google.firebase:firebase-database-ktx:20.3.0' } // 插件声明必须在dependencies块之后 id 'com.google.gms.google-services'清理缓存并重建项目
执行File > Invalidate Caches... > Invalidate and Restart重启Android Studio,再依次执行Build > Clean Project和Build > Rebuild Project,清除缓存冲突。验证项目级gradle配置
确保项目根目录下的build.gradle中,google-services插件的声明在plugins块内,示例:plugins { id 'com.android.application' version '7.2.0' apply false id 'com.android.library' version '7.2.0' apply false id 'com.google.gms.google-services' version "4.4.0" apply false }检查Firebase BOM版本兼容性
若使用的BOM版本过旧可能存在兼容问题,可替换为Firebase官网的最新稳定版BOM后重新同步gradle。
内容的提问来源于stack exchange,提问作者Frans

