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: