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

 ̄綄美尐妖づ 提交于 2019-12-21 07:03:45

问题


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?


回答1:


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());
    }
}



回答2:


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(...)
   ...
}



回答3:


When you are using Firebase Crashlytics, you don't need to do any Fabric initialization. Everything is done automatically.

But if you want to do any custom logging, via (for example) Crashlytics.log("Custom log"), you need to have FirebaseCrashlytics enabled in your manifest. Check the manifest if you have something like that:

<meta-data
    android:name="firebase_crashlytics_collection_enabled"
    android:value="${crashlyticsEnabled}" />

${crashlyticsEnabled} can be set to true or false or via you app level build.gradle. This is typically used to disable Firebase Crashlytics when you are debugging the app.




回答4:


If you used android:process, then automatic initialization wouldn't work because it works by using a content provider in your manifest. In that case, you'll have to manually initialize Crashlytics.




回答5:


If you migrated from Fabric.io to Firebase, there must be some lines you need to DELETE from AndroidManifest.xml

        <!-- Fabric.io Analytics key -->
        <meta-data
            android:name="io.fabric.ApiKey"
            android:value="49yy995568140ee22uio128e00450bd99603fd43" />

With those lines, the Crashlytics plugin can NOT be initialized correctly by Firebase.




回答6:


This error had me beat on an Ionic 4 build. I have installed the Cordova firebaseX module, and this error occurred.

From reading the debug log I managed to see that the firebase crashlytics collection was missing a bool. In the androidmanifest I found this line and simply changed the value to a bool for a hot fix. I put in 'true' - and the error is gone.

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

So for anyone out there was this error on Ionic platform. Look in the androidmanifest file for the above meta line.



来源:https://stackoverflow.com/questions/50782068/why-is-fabric-not-initialized-java-lang-illegalstateexception-must-initialize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!