4

The previous Fabric SDK allowed a check like this

if (Fabric.isInitialized()) {
    crashSetParam(CrashParameters.ROOT_NODE, nodeInfoCompat.toString())
    Crashlytics.logException(ex)
}

I am unable to determine how to do this in the new Firebase Android SDK and it causes my junit tests that might touch this to crash. I realize I can wrap it in an interface, but I am also curious how to solve it in a simple manner like above. Thank you.

I logged an issue on github too:

https://github.com/firebase/firebase-android-sdk/issues/1437

Without a check we get something like this when running unit tests:

java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process null. Make sure to call FirebaseApp.initializeApp(Context) first.

I've fixed it with the following for now:

fun logException(ex: Throwable, nodeInfoCompat: AccessibilityNodeInfoCompat?) {
    // We check to make sure Fabric is initialized or else the tests will fail - see above comment
    try {
        FirebaseCrashlytics.getInstance().setCustomKey(CrashParameters.ROOT_NODE, nodeInfoCompat.toString())
        FirebaseCrashlytics.getInstance().recordException(ex)
    } catch (e: IllegalStateException) {
        Timber.e(e);
    }
}
dazza5000
  • 7,075
  • 9
  • 44
  • 89

1 Answers1

1

Another solution that I've found is putting this in the main application class

if (FirebaseApp.getApps(getApplicationContext()).isEmpty()) {
FirebaseApp.initializeApp(getApplicationContext());}

I've only tested it for a bit but while running tests it would always return true for isEmpty() and on normal utilization it would return false.