1

This is my application class:

package com.example.myapp

import android.support.multidex.MultiDexApplication
import com.facebook.appevents.AppEventsLogger
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.FirebaseFirestoreSettings

class App : MultiDexApplication() {
    override fun onCreate() {
        super.onCreate()

        // Facebook
        AppEventsLogger.activateApp(this)

        // Firebase
        FirebaseApp.initializeApp(this)

        val settings = FirebaseFirestoreSettings.Builder()
                .setPersistenceEnabled(false)
                .setTimestampsInSnapshotsEnabled(true)
                .build()

        FirebaseFirestore.getInstance().firestoreSettings = settings
    }
}

This is the error I'm getting. It's failing on the last line of the App class, FirebaseFirestore.getInstance(), saying it's not initialized, but you can see above that I am initializing it?

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapp, PID: 8328
    java.lang.RuntimeException: Unable to create application com.example.myapp.App: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.myapp. Make sure to call FirebaseApp.initializeApp(Context) first.
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5876)
        at android.app.ActivityThread.access$1100(ActivityThread.java:199)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.myapp. Make sure to call FirebaseApp.initializeApp(Context) first.
        at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.1:219)
        at com.google.firebase.firestore.FirebaseFirestore.getInstance(com.google.firebase:firebase-firestore@@17.0.5:49)
        at com.example.myapp.App.onCreate(App.kt:31)
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1154)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5871)
        at android.app.ActivityThread.access$1100(ActivityThread.java:199) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

Does anybody know what I need to do to fix this? This is an instant app. I have put the google services json inside every module (base + app + instant) and have also added the plugin to each gradle.

It's just weird that it's obviously going past the initialize statement and still failing.

TomH
  • 2,581
  • 1
  • 15
  • 30
  • I'm looking through https://stackoverflow.com/questions/45977847/make-sure-to-call-firebaseapp-initializeappcontext-first-in-android and a few things you can try: 1. check com.google.gms:google-services version, 2. re-grab your json file – TWL Aug 20 '18 at 19:24
  • Or looking at https://firebase.google.com/docs/firestore/quickstart, under "Initialize Cloud Firestore (Android)" it says you don't need FirebaseApp.initializeApp(this)? Compare that to (Java) description. – TWL Aug 20 '18 at 19:25

2 Answers2

1

For me none of the solution worked that were given any where. Only this worked. Just had to download grade my google services from 4.1.0 to 4.0.0

dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0-alpha08'
    classpath 'com.google.gms:google-services:4.0.0'
    /*classpath 'com.google.gms:google-services:4.1.0' <-- this was the problem */
}

So if you have updated the google services, just try to downgrade or change to an older version.

Ammar Bukhari
  • 2,082
  • 2
  • 16
  • 16
1

This worked for me..

    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.1'
        classpath 'com.google.gms:google-services:4.2.0'
Rami Alloush
  • 2,308
  • 2
  • 27
  • 33