1

When I try to run this code on android, I get the error FirebaseApp name [DEFAULT] already exists! In this thread, it says that firebaseApp is initialized on app startup, but then why can you call initializeApp like in this thread? It seems like my code is functionally similar to the code in the second thread, so why am I getting an error? It works fine on iOS, so I'm completely lost. What have I missed?

Edit: Forgot to mention that all of the testConfig values are stored in an .env file that is accessed by the react-native-config library.

Edit 2: Changing firebase.initializeApp(testConfig); to firebase.initializeApp(testConfig, 'fire'); fixes android, but breaks ios, causing error No Firebase App '[DEFAULT]' has been created - call firebase.initializeApp()

Code:

import firebase from '@react-native-firebase/app';
import '@react-native-firebase/auth';        // for authentication
import '@react-native-firebase/storage';     // for storage
import '@react-native-firebase/firestore';   // for cloud firestore
import '@react-native-firebase/functions';   // for cloud functions
import '@react-native-firebase/messaging';   // for cloud messaging
import Config from 'react-native-config'

const testConfig = {
  apiKey: Config.REACT_APP_FIREBASE_TEST_API_KEY,
  authDomain: Config.REACT_APP_FIREBASE_TEST_AUTH_DOMAIN,
  databaseURL: Config.REACT_APP_FIREBASE_TEST_DATABASE_URL,
  projectId: Config.REACT_APP_FIREBASE_TEST_PROJECT_ID,
  storageBucket: Config.REACT_APP_FIREBASE_TEST_STORAGE_BUCKET,
  messagingSenderId: Config.REACT_APP_FIREBASE_TEST_MESSAGING_SENDER_ID,
  appId: Config.REACT_APP_FIREBASE_TEST_APP_ID,
  measurementId: Config.REACT_APP_FIREBASE_TEST_MEASUREMENT_ID
};

const fire = firebase.initializeApp(testConfig);

Logcat:

2020-04-16 11:05:17.269 25458-25543/com.nativeminutetech E/unknown:ReactNative: Exception in native call
    java.lang.IllegalStateException: FirebaseApp name [DEFAULT] already exists!
        at com.google.android.gms.common.internal.Preconditions.checkState(Unknown Source:29)
        at com.google.firebase.FirebaseApp.initializeApp(com.google.firebase:firebase-common@@19.3.0:295)
        at com.google.firebase.FirebaseApp.initializeApp(com.google.firebase:firebase-common@@19.3.0:268)
        at io.invertase.firebase.common.RCTConvertFirebase.readableMapToFirebaseApp(RCTConvertFirebase.java:97)
        at io.invertase.firebase.app.ReactNativeFirebaseAppModule.initializeApp(ReactNativeFirebaseAppModule.java:55)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
        at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:151)
        at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27)
        at android.os.Looper.loop(Looper.java:164)
        at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:226)
        at java.lang.Thread.run(Thread.java:764)

3 Answers3

2

As stated in the react-native-firebase docs, Firebase is initialized automatically on app startup, so my original initializeApp call was superfluous. The reason it wasn't initialized for iOS was because I neglected to set up an app in Firebase console for iOS(oops). If anyone else runs into this issue, note that you need to have already done the setup for both OS's, just one doesn't do the trick.

0

Yes, don't forget check:

if (!firebase.apps.length) {
      firebase.initializeApp(testConfig);
}
Thanh Cao
  • 151
  • 5
  • 1
    From the [docs](https://rnfirebase.io/app/usage): "Unlike the Firebase Web SDK, there is no need to manually call the initializeApp method with your project credentials". You should not have to do this unless you are registering secondary apps. You have misconfigured your project if this is required. – EntangledLoops Sep 20 '22 at 21:54
0

Thank Laine Otto-Rognlie for your response.

I feel that this should be included somewhere in the React Native Firebase Getting Started/Set Up documentation at https://rnfirebase.io/. In my experience, working with Firebase for web apps and other mobile app frameworks almost always requires you to call initializeApp somewhere before use. Not having to do so for React Native is a bit counter intuitive.