Android Crashlytics Log Exception Manually

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.

inline fun logException(e: Throwable, message: String? = null) {    try {        // Crashlytics.log(Log.ERROR, tag, message)        if (message != null) {            Crashlytics.log(message)        }        Crashlytics.logException(e)    }    // java.lang.IllegalStateException: Must Initialize Fabric before using singleton() when Crashlytics is disabled    catch (e: Exception) { }}

NOTE: Exception log by Crashlytics.logException is marked as Non-fatals, where you need to adjust the Event type = "Non-fatals" filter to view them in Firebase Console - Crashlytics.

UPDATE 2019-08-29:

To prevent Crashlytics.logException from throwing exception when disabled, initialize with the following during application startup.

val crashlytics = Crashlytics.Builder()    .core(CrashlyticsCore.Builder().disabled(true).build())    .build()Fabric.with(this, crashlytics)

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.