React Native 0.68.2+Expo45集成react-native-firebase报错求助
环境与报错信息
使用React Native 0.68.2、Expo ^45.0.0集成react-native-firebase,未执行react-native-link(RN新版本不支持该命令),运行项目时抛出以下错误:
Error: You attempted to use a firebase module that's not installed on your Android project by calling firebase.app().
请确保:
在'MainApplication.java'文件中导入'io.invertase.firebase.app.ReactNativeFirebaseAppPackage'模块。
在RN的'getPackages()'方法列表中添加'new ReactNativeFirebaseAppPackage()'代码行。
查看http://invertase.link/android获取完整设置说明。
现有配置文件内容
项目根目录build.gradle依赖
classpath('com.android.tools.build:gradle:7.0.4') classpath('com.facebook.react:react-native-gradle-plugin') classpath('de.undercouch:gradle-download-task:4.1.2') classpath 'com.google.gms:google-services:4.3.13'
android/app/build.gradle
apply plugin: "com.android.application" apply plugin: 'com.google.gms.google-services' implementation "com.facebook.react:react-native:+" // From node_modules implementation project(path: ":@react-native-firebase_messaging") implementation project(path: ":@react-native-firebase_app")
settings.gradle
include ':@react-native-firebase_messaging' project(':@react-native-firebase_messaging').projectDir = new File(rootProject.projectDir, './../node_modules/@react-native-firebase/messaging/android') include ':@react-native-firebase_app' project(':@react-native-firebase_app').projectDir = new File(rootProject.projectDir, './../node_modules/@react-native-firebase/app/android')
MainApplication.java
import io.invertase.firebase.messaging.ReactNativeFirebaseMessagingPackage; packages.add(new ReactNativeFirebaseMessagingPackage());
App.js
import messaging from '@react-native-firebase/messaging'; messaging().setBackgroundMessageHandler(async remoteMessage => { console.log("Message handled in the background!", remoteMessage); }) const unsub = messaging().onMessage(async remoteMessage=>{ Alert.alert('A new FCM msg arrived', JSON.stringify(remoteMessage)) console.log(remoteMessage); return unsub; })
app.json
"plugins": [ "@react-native-firebase/app", "@react-native-firebase/crashlytics", [ "expo-build-properties", { "ios": { "useFrameworks": "static" } } ] ],
问题解决步骤
补全MainApplication.java的Firebase核心包配置
错误提示明确要求添加Firebase核心包的配置,你当前只添加了Messaging包,需补充以下代码:// 导入核心包 import io.invertase.firebase.app.ReactNativeFirebaseAppPackage; // 在getPackages()方法的packages列表中添加 packages.add(new ReactNativeFirebaseAppPackage());确保这段代码和已有的
ReactNativeFirebaseMessagingPackage配置放在同一区域。匹配react-native-firebase版本与当前RN/Expo环境
React Native 0.68.2 + Expo 45需要对应版本的react-native-firebase,建议安装14.x系列版本(该版本适配上述环境):# npm npm install @react-native-firebase/app@14.x @react-native-firebase/messaging@14.x # yarn yarn add @react-native-firebase/app@14.x @react-native-firebase/messaging@14.x确认Google服务配置文件存在
将google-services.json文件放置在android/app/目录下,该文件可从Firebase控制台下载,确保文件内容与你的项目配置匹配。清理缓存并重新构建项目
- 删除依赖缓存与构建目录:
rm -rf node_modules android/build npm install - 清理Gradle缓存:
cd android && ./gradlew clean - 重新运行Android项目:
npx expo run:android
- 删除依赖缓存与构建目录:
内容的提问来源于stack exchange,提问作者userkelt inn

