Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Fabric not initialized? java.lang.IllegalStateException: Must Initialize Fabric before using singleton()

I set up Firebase Crashlytics according to Get started with Firebase Crashlytics for my Android app (using Android studio 3.1.3). On my own device as well as on the Emulator, everything works fine and my crashes appear correctly within the Firebase Console. So far so good.

However, there was a crash for one of my app users that was unexpected:

java.lang.IllegalStateException: Must Initialize Fabric before using singleton()

The exception was thrown in another Activity than the MainActivity.

I am aware that you could manually execute the initialization as described here by calling Fabric.with(this, new Crashlytics()); However, there is nothing said about one has to manually initialize the Crashlytics in the Getting Started article mentioned above. I was expecting this is done automatically since all my own tests run fine. So why is it that for some users Crashlytics is set up correctly and for some not?

like image 690
John Threepwood Avatar asked Jun 10 '18 08:06

John Threepwood


2 Answers

You need to initialize Crashlytics in your application's onCreate

import android.app.Application;

import com.crashlytics.android.Crashlytics;

import io.fabric.sdk.android.Fabric;

public class TestApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Fabric.with(this, new Crashlytics());
    }
}
like image 158
Gil Goldzweig Avatar answered Nov 19 '22 12:11

Gil Goldzweig


In my case, The below checks helped to get rid of the error.

If you find code like below in your manifest, set it to true or remove it since it's true by default.

   <meta-data
      android:name="firebase_crashlytics_collection_enabled"
      android:value="false" /> 

Also incase if the value is being pulled in from your build.gradle, check which buildType its in and consider not invoking any Crashlytics functions under that buildType.

Example: build.gradle

android{

    ...

   buildTypes {
        debug{
            manifestPlaceholders = [enableCrashReporting:"false"]
        }
        release {
            manifestPlaceholders = [enableCrashReporting:"true"]
        }
   }
}

In this case you should have your Crashlytics calls wrapped like this -

if(!BuildConfig.DEBUG){
   ...
   Crashlytics.setUserIdentifier(...)
   ...
}
like image 11
Jishin Dev Avatar answered Nov 19 '22 11:11

Jishin Dev