Search code examples
androidfirebasenativescript

Nativescript - FirebaseApp is not initialized in this process


I´m really desperate about this problem. I´m trying to solve this issue for such a long time now, but I just can´t get it working.

My goal is to use Firebase withhin my Nativescript-App, which also uses Angular 7, but every time I try to "begin" using firebase, I get the following error:

Default FirebaseApp is not initialized in this process com.nativescript. Make sure to call FirebaseApp.initializeApp(Context) first.

I already tried that. I followed this tutorial:https://github.com/EddyVerbruggen/nativescript-plugin-firebase

My Nativescript App is build in "Typescript", so I choose the Typescript-way.

Since I don´t have the app.js, I started the .init in app.ts like so:

import * as application from "tns-core-modules/application";
import * as FirebaseApp from "nativescript-plugin-firebase/app"
import { environment } from './environments/environment';


 FirebaseApp.initializeApp(environment.firebase);

 import * as firebase from "nativescript-plugin-firebase";

 firebase.init({
    // Optionally pass in properties for database, authentication and cloud messaging,
    // see their respective docs.
  }).then(
    () => {
      console.log("firebase.init done");
    },
    error => {
      console.log(`firebase.init error: ${error}`);
    }
  );

application.run({ moduleName: "app-root" }); 

When I try FirebaseApp.initializeApp(environment.firebase), it crashes and says in the log:

S: firebase.init error: Firebase already initialized
Successfully synced application com.nativescript. on device 97412f42.
System.err: An uncaught Exception occurred on "main" thread.
System.err: Unable to start activity ComponentInfo{com.nativescript./com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onCreate failed
System.err: Error: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.nativescript.. Make sure to call FirebaseApp.initializeApp(Context) first.
System.err:
...

That´s interesting, because above it says "already initialized", but right after that it says "FirebaseApp is not initialized". Okay, so I decide to remove firebase.init({...}) because maybe that´s just too much init, but obviously nope:

System.err: Unable to start activity ComponentInfo{com.nativescript./com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onCreate failed
System.err: Error: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.nativescript. Make sure to call FirebaseApp.initializeApp(Context) first.
System.err:
...

Don´t worry, I purposly removed com.native.script.THEID, just in case.

When I decide to replace FirebaseApp.initializeApp(environment.firebase) with the firebase.init-part it also gives the same error.

My environment.ts is in a folder called environment and it´s in the same directory as app.ts (the folder).

This is how environment.ts is looking like:

export const environment = {
    production: true,
    firebase: {
      apiKey: 'THE API KEY',
      authDomain: 'THE AUTH DOMAIN',
      databaseURL: 'THE DATABASE URL',
      projectId: 'THE PROJECT ID',
      storageBucket: 'THE STORAGE BUCKET',
      messagingSenderId: 'THE SENDER ID'
    },
  };

I already download the google-services.json from Firebase Console (where you setup your App) and pasted it into app/App_Resources/Android/

Please guys, I really don´t know any further. I even tried the second page of Google Search. If you need any infos, please let me know.


Solution

  • MY SOLUTION Unfortunately I had to abandon this project and create a new one with angular-templates. I must have messed something up while enabling mutliDex since I had struggles enabling it back then.

    My theory is that, by messing up the activation of multiDex, I probably create two instances of my app build when I run tns run android.

    So, the only solution was to start over again, but at least the Firebase initialization works now just fine!