Android Crashlytics Log Exception Manually

October 4, 2018
The exception is caught, but still report to Crashlytics
try {
    ...
} catch (IOException e) {
    e.printStackTrace();
    Crashlytics.logException(e);
}
Crashlytics.logException(new Exception("Test"));

Caveats

If you disable Crashlytics, calling Crashlytics.logException or Crashlytics.getInstance will cause the following exception.

java.lang.IllegalStateException: Must Initialize Fabric before using singleton()
    at io.fabric.sdk.android.Fabric.singleton(Fabric.java:302)

As of version 2.9.8, there is no way to check if Crashlytics is enabled/initialized or not. Write a wrapper function instead.

fun logException(throwable: Throwable) {
    try {
        Crashlytics.logException(throwable)
    }
    // java.lang.IllegalStateException: Must Initialize Fabric before using singleton() when Crashlytics is disabled
    catch (e: Exception) { }
}

References:

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.