React native expo,您试图通过调用 firebase.app() 来使用未安装在您的 Android 项目上的 firebase 模块

Posted

技术标签:

【中文标题】React native expo,您试图通过调用 firebase.app() 来使用未安装在您的 Android 项目上的 firebase 模块【英文标题】:React native expo, You attempted to use a firebase module that's not installed on your Android project by calling firebase.app() 【发布时间】:2022-01-06 07:03:23 【问题描述】:

在我的项目中,我使用 React 本机 firebase 来获取 Analytics,而我的项目是 expo 裸流程,我只是遵循了这个 https://rnfirebase.io/ 文档,但是在运行应用程序时遇到了类似的错误

backend.js:2173 Possible Unhandled Promise Rejection (id: 3):
Error: You attempted to use a firebase module that's not installed on your android project by calling firebase.app().

Ensure you have:

1) imported the 'io.invertase.firebase.app.ReactNativeFirebaseAppPackage' module in your 'MainApplication.java' file.

2) Added the 'new ReactNativeFirebaseAppPackage()' line inside of the RN 'getPackages()' method list.

See http://invertase.link/android for full setup instructions.
Error: You attempted to use a firebase module that's not installed on your Android project by calling firebase.app().

Ensure you have:

1) imported the 'io.invertase.firebase.app.ReactNativeFirebaseAppPackage' module in your 'MainApplication.java' file.

2) Added the 'new ReactNativeFirebaseAppPackage()' line inside of the RN 'getPackages()' method list.

我的 Android MainApplication.java

import io.invertase.firebase.app.ReactNativeFirebaseAppPackage; <---added for firebase   
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) 
@Override
public boolean getUseDeveloperSupport() 
  return BuildConfignfig.DEBUG;


@Override
protected List<ReactPackage> getPackages() 
  List<ReactPackage> packages = new PackageList(this).getPackages();
  packages.add(new ModuleRegistryAdapter(mModuleRegistryProvider));
  packages.add(new ReactNativeFirebaseAppPackage());<---- added for firebase
  return packages;

package.json

"@react-native-firebase/analytics": "^13.0.1",
"@react-native-firebase/app": "^13.0.1",
"@types/npm": "^7.19.0",
"apisauce": "^0.15.1",
"base-64": "^0.1.0",
"expo": "~42.0.1",
"expo-blur": "~9.0.3",
"expo-camera": "^11.2.2",
"expo-linear-gradient": "~9.2.0",
"expo-secure-store": "~10.2.0",
"expo-splash-screen": "~0.11.2",
"expo-status-bar": "~1.0.4",
"expo-updates": "~0.8.1",
"native-base": "^2.15.2",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-native": "~0.63.4",

我也试过用config-pluginhttps://docs.expo.dev/guides/config-plugins/也没有用,错误没有解决。

【问题讨论】:

【参考方案1】:

我有一个 expo 应用程序并使用了此链接...

如果您退出 expo 或从 bareworkflow 开始,可能会有所不同

https://docs.expo.dev/guides/using-firebase/#using-expo-with-firestore

有一个 firebase.js 文件并导入您需要的不同模块

以我们的为例

import  initializeApp  from "firebase/app"
import  getFirestore  from 'firebase/firestore';
import Constants from "expo-constants"
import "firebase/auth"

 // TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration -- from firebase console
// For Firebase JS SDK v7.20.0 and later, measurementId is optional

const firebaseConfig = 
  apiKey: xxxxxxx,
  authDomain: xxxxxxx,
  databaseURL: xxxxxxxxx,
  projectId: xxxxxxxxx,
  storageBucket: xxxxxxxxx,
  messagingSenderId: xxxxxxxxx,
  appId: xxxxxxxxx,
  measurementId: xxxxxxxxx,
 

// Initialize Firebase
const Firebase = initializeApp(firebaseConfig)
const firestore = getFirestore(); <--- our added firestore module
export default [Firebase, firestore]

确保根据 FB 包安装指南添加了 @firebase/module-name

【讨论】:

感谢您的回复,我将我的项目弹出到 BareWorkFlow,主要问题是无法使用在 MainApplication.java 中添加的本机包,例如 firebase 等。

以上是关于React native expo,您试图通过调用 firebase.app() 来使用未安装在您的 Android 项目上的 firebase 模块的主要内容,如果未能解决你的问题,请参考以下文章

使用expo的react-native消息队列

在React-Native Expo的App购买中?

如何在 expo/react-native 应用程序中添加自定义节点模块?

无法使用 React Native Expo 导入 Firebase JS SDK

在 iOS 上带有 expo 的 react-native statusBar

无法解析依赖树 - React Native Expo