Expo SDK 48配置Firebase Analytics遇报错求助
解决Expo SDK 48迁移React Native Firebase Analytics的Android报错问题
问题背景
原使用expo-firebase-analytics,因SDK 48弃用迁移至@react-native-firebase/app和@react-native-firebase/analytics,按官方指南操作后Android真机报错:
ERROR Error: You attempted to use a firebase module that's not installed on your Android project by calling firebase.app().
Ensure you have:
- imported the 'io.invertase.firebase.app.ReactNativeFirebaseAppPackage' module in your 'MainApplication.java' file.
- Added the 'new ReactNativeFirebaseAppPackage()' line inside of the RN 'getPackages()' method list.
核心原因
该错误提示针对裸项目(Bare Workflow),而你使用的是Expo托管项目(Managed Workflow),无法手动修改原生文件,需通过Expo Development Builds完成原生模块集成。
解决步骤
1. 切换使用Expo Development Builds
Expo Go不支持自定义原生模块,必须创建Development Build才能集成React Native Firebase:
# 先清理旧的构建缓存 npx expo prebuild --clean # 构建并运行到Android真机 npx expo run:android --device
2. 完善expo.config.js配置
添加Firebase配置文件路径及插件,确保与Firebase控制台信息一致:
export default { expo: { // 基础配置 name: "你的项目名", slug: "你的项目slug", // Android配置 android: { googleServicesFile: "./google-services.json", // 从Firebase控制台下载的Android配置文件 package: "你的安卓包名" // 需与Firebase项目中注册的包名完全一致 }, // iOS配置(可选,若需兼容iOS) ios: { googleServicesFile: "./GoogleService-Info.plist", bundleIdentifier: "你的iOS Bundle ID" }, plugins: [ [ "expo-build-properties", { ios: { useFrameworks: "static" } } ], "@react-native-firebase/app" // 自动配置原生Firebase依赖 ] } };
3. 修复代码拼写错误
修正事件参数的拼写错误(避免潜在的日志不生效问题):
import analytics from '@react-native-firebase/analytics'; useEffect(() => { analytics().logEvent("test_analytics_event", { additionalParam: "test", // 原拼写为additionaParam,修正后确保参数名正确 }); }, []);
4. 重新安装依赖并构建
rm -rf node_modules package-lock.json npm install npx expo run:android --device
5. 验证Firebase控制台配置
- 确认Firebase项目已添加对应Android应用,包名与
expo.config.js中android.package完全匹配 - 确保
google-services.json文件未被修改,正确放置在项目根目录
内容的提问来源于stack exchange,提问作者newbie coder
相关产品推荐
相关产品推荐

