36

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
John Threepwood
  • 15,593
  • 27
  • 93
  • 149
  • 1
    Thanks? Do you have any Fabric.with statement in your app? For any Fabric.with statement to be honored with Firebase Crashlytics, you also need to add to your Android Manifest. If not, can you let me know where you are initializing Firebase. – Todd Burner Jun 11 '18 at 15:00
  • Thanks for your reply. No, I do not have any Fabric.with statements in my app. I always thought this is not necessary since the official Getting Started documentation of Crashlytics did not say anything about it and my own tests worked fine. Only 2 of my current 150 active app users experienced the issue. Should I add Fabric.with statements or should it be initizialized automatically? – John Threepwood Jun 12 '18 at 12:28
  • I'm having the same issue when I'm trying to call Crashlytics.log(priority, tag, message) without previously calling Fabric.with(this, new Crashlytics()) and relying on the automatic initialization. – makovkastar Aug 01 '18 at 10:07
  • Hello @ToddBurner , I'm also interested into this question. When is a Fabric.with statement required? I'm using custom logging but I got the same error when I don't initialise using Fabric.with statement. – osrl Nov 16 '18 at 22:50
  • 1
    Did you find a resolution to the issue? I have the same problem with a project that has not had Crashlytics before. – Yordan Lyubenov Dec 14 '18 at 10:57
  • 2
    Firebase Crashlytics is a punishingly frustrating product to configure, especially where it is integrated with Fabric Crashlytics. I've found so many scenarios where crash reports just quietly fail (with nothing in even the Crashlytics debug logs) that I've been forced to abandon Fabric Crashlytics, and use "pure" Firebase Crashlytics, whose documentation and onboarding is really not much better. – Ollie C Dec 19 '18 at 09:19
  • Same issue only on very few devices and when the SyncAdapter is run but the app was not running (during the night). – Gainder Dec 20 '18 at 18:44

8 Answers8

36

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());
    }
}
Gil Goldzweig
  • 1,809
  • 1
  • 13
  • 26
  • 2
    Thank you for your answer. As I described, I know I can just to what you propose. However, I thought it would be set up automatically since on my own device everything runs fine. Also, the official documentation does not say anything about this. I just wanted to know *why* manual setup is required for some devices and for some not? – John Threepwood Jun 10 '18 at 09:02
  • I'm almost positive that it does require on all devices maybe you received an exception on one device which called fabric and on other device it didn't throw an exception there for fabric is never called – Gil Goldzweig Jun 10 '18 at 10:09
  • Thanks for your quick reply. Hm... This makes me thinking. Because on my own device the test example on the Getting Started documentation where one crashes the app on purpose via button works fine. I can see the crash report on Crashlytics without ever initializing anything manually. So on my own device it has been initialized somehow. – John Threepwood Jun 10 '18 at 10:55
  • This one worked for me! Whereas approach with 'auto-initializing as docs say and in Manifest - Failed) – Kirill Karmazin Jan 30 '19 at 13:45
  • @GilGoldzweig If I migrate to firebase shall I need to initialize the above in my application. According to the doc of firebase, i not need to add the above for initialzation – sejn Sep 03 '20 at 13:42
11

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(...)
   ...
}
Jishin Dev
  • 371
  • 7
  • 10
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.

73k05
  • 159
  • 2
  • 5
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.

hiddeneyes02
  • 2,562
  • 1
  • 31
  • 58
  • 2
    Beware that if you set this value to `false` and call `Crashlytics.log()` you'll get exactly the same error the OP has. To avoid this, you have to initiate Crashlytics manually in code and disable its reporting feature conditionally. See [this SO answer](https://stackoverflow.com/questions/16986753/how-to-disable-crashlytics-while-developing/27191197#27191197). – Mickäel A. Sep 19 '18 at 12:58
1

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.

Mygod
  • 2,077
  • 1
  • 19
  • 43
1

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.

1

I get this exception when migrated from Fabric to FirebaseCrashlytics. I didn't remove

  // Remove the Fabric Crashlytics SDK.
  implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'

And I did not update code:

Crachlytics.log(..

Should became

FirebaseCrashlytics.getInstance().log(...

Actually just follow everything in this document Upgrade to the Firebase Crashlytics SDK

Zoran
  • 1,502
  • 19
  • 22
  • Yes, root cause is using "com.crashlytics.sdk.android:crashlytics" Fabric NDK dependency instead of Firebase NDK (com.google.firebase:firebase-crashlytics-ndk). App works fine after I started using com.google.firebase:firebase-crashlytics-ndk. – Manikandan Aug 11 '20 at 10:38
0

To anyone who is working with NDK and has the same issue. Before completing this step from the official documentation // Add the Crashlytics NDK dependency (if you have the // Firebase Crashlytics dependency, replace it). initialize it using original dependency.

enoch root
  • 1
  • 1
  • 2