Capacitor JS安卓构建中Firebase App未初始化致崩溃的解决咨询
Capacitor JS中Firebase谷歌认证安卓端崩溃(Default Firebase App未初始化)问题解决
问题描述
- 使用
@capacitor-firebase/authentication实现Firebase谷歌认证,Web应用运行正常,但安卓应用启动即崩溃,报错:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.demo.application/com.demo.application.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialised in this process, com.demo.application. Make sure to call FirebaseApp.initializeApp(Context) first.
- 已完成的配置:
- 添加了Google Service JSON文件
- 相关Gradle配置:
安卓级Gradle:
应用级Gradle:dependencies { classpath 'com.android.tools.build:gradle:7.3.1' classpath 'com.google.gms:google-services:4.3.15' }dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion" implementation "androidx.coordinatorlayout:coordinatorlayout:$androidxCoordinatorLayoutVersion" implementation "androidx.core:core-splashscreen:$coreSplashScreenVersion" implementation project(':capacitor-android') testImplementation "junit:junit:$junitVersion" androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion" androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion" implementation project(':capacitor-cordova-android-plugins') implementation 'com.google.firebase:firebase-core:21.1.1' implementation "com.google.gms:google-services:4.4.1" } apply from: 'capacitor.build.gradle' try { def servicesJSON = file('google-services.json') if (servicesJSON.text) { apply plugin: 'com.google.gms.google-services' } } catch(Exception e) { logger.info("google-services.json not found, google-services plugin not applied. Push Notifications won't work." }
解决方案
1. 正确初始化Firebase JS SDK
在项目前端入口文件(如src/main.js或src/app.js)中,使用Firebase JS SDK完成初始化,代码示例:
import { initializeApp } from "firebase/app"; // 从Firebase控制台获取的项目配置,需与google-services.json参数一致 const firebaseConfig = { apiKey: "你的API_KEY", authDomain: "你的AUTH_DOMAIN", projectId: "你的PROJECT_ID", storageBucket: "你的STORAGE_BUCKET", messagingSenderId: "你的MESSAGING_SENDER_ID", appId: "你的APP_ID" }; // 初始化Firebase App initializeApp(firebaseConfig);
2. 修正Gradle配置错误
- 移除应用级Gradle中重复的
implementation "com.google.gms:google-services:4.4.1",谷歌服务插件只需在安卓级Gradle的classpath中声明,无需在应用级依赖重复添加。 - 将判断文件存在的逻辑从
servicesJSON.text改为servicesJSON.exists(),避免读取文件内容时触发异常。修正后的应用级Gradle相关代码:
dependencies { // 保留其他原有依赖 implementation 'com.google.firebase:firebase-core:21.1.1' // 移除该行:implementation "com.google.gms:google-services:4.4.1" } apply from: 'capacitor.build.gradle' try { def servicesJSON = file('google-services.json') if (servicesJSON.exists()) { apply plugin: 'com.google.gms.google-services' } } catch(Exception e) { logger.info("google-services.json not found, google-services plugin not applied. Push Notifications won't work.") }
3. 安卓端手动初始化Firebase(兜底方案)
若上述步骤无效,可在安卓MainActivity中手动初始化Firebase:
打开android/app/src/main/java/com/demo/application/MainActivity.java,添加初始化代码:
import com.google.firebase.FirebaseApp; import android.content.Context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 检查Firebase是否已初始化,未初始化则手动启动 if (FirebaseApp.getApps(this).isEmpty()) { FirebaseApp.initializeApp((Context) this); } }
4. 清理重建项目
- 执行
npx cap sync android同步Capacitor配置 - 在Android Studio中执行
Build -> Clean Project,再执行Build -> Rebuild Project - 卸载设备上的旧应用,重新安装测试
内容的提问来源于stack exchange,提问作者Kishan Sardhara
相关产品推荐
相关产品推荐

