53

I started using Firebase (Crashlytics) in my project to track app crashes. It works perfectly with crashes but how can I log non-fatal crashes, i.e. caught exceptions. I tried Crashlytics.logException(e) but it doesn't work. I see no reports in the dashboard. I saw answers suggesting to use FirebaseCrash.report(t) but this class doesn't exist in the latest version of Firebase. So does anyone know how it's done?

Dependencies:

implementation 'com.google.firebase:firebase-core:16.0.0'
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.3'

enter image description here

Egis
  • 5,081
  • 5
  • 39
  • 61
  • 6
    Did you kill and restart your app after you logged the exception? – Doug Stevenson Aug 06 '18 at 19:25
  • Even on the latest version of Crashlytics, `Crashlytics.logException(ex)` is the way to go. Are you testing on a debug or a release build? I've never had problems logging non-fatals with `Crashlytics.logException(ex)`. – Eric Bachhuber Aug 06 '18 at 19:27
  • Thanks, everyone. It does work after all. It seems it just takes longer time until non-fatal exceptions are shown in the dashboard or like @DougStevenson said you have to kill and restart the app. – Egis Aug 07 '18 at 07:48
  • +1 on needing to kill and restart the app. My test app has a foreground service, so I had to reinstall the app to get a new process before the report is uploaded. FYI you'll see this in logcat when the events are sent back up to Firebase Crashlytics: `I/CrashlyticsCore: Crashlytics report upload complete: ` – hcabrams Mar 12 '20 at 19:53
  • Is firebase sync with `implementation 'com.crashlytics.sdk.android:crashlytics:2.9.3'` dependency ?? – milad salimi May 17 '20 at 08:57

5 Answers5

58

With the new Firebase Crashlytics you should use recordException(@NonNull Throwable throwable)

...
catch (e: Exception) {
    FirebaseCrashlytics.getInstance().recordException(e)
}
...

Here is firebase documentation stating that

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
Akbolat SSS
  • 1,761
  • 15
  • 23
  • I could not find this `recordException(e)` i think it has removed – milad salimi May 17 '20 at 08:57
  • 1
    Thank you for the link, a lot of the Firebase "getting started" docs omit the Crashlytics dependencies for some reason, but the "upgrade" guide that you posted is complete and accurate. – Andrew Koster Jul 29 '20 at 17:23
  • @miladsalimi maybe you were using `FirebaseAnalytics` instead of `FirebaseCrashlytics`. – rtsketo Oct 27 '21 at 09:07
13

You can use

FirebaseCrashlytics.getInstance().recordException(e)

to log non fatal issues on crashlytics

Jithin Jude
  • 840
  • 14
  • 19
Viswanath Kumar Sandu
  • 2,230
  • 2
  • 17
  • 34
3

Make sure that you add the following codes in you Application class which you attached in your AndroidManifest.xml -

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        .
        .
        .
        FirebaseApp.initializeApp(this);
        FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true);
        .
        .
        .
    }
}

Then call the method where expected -

FirebaseCrashlytics.getInstance().recordException(myException)
Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42
  • 1
    `FirebaseApp.initializeApp(this)` is not required, It is handled internally by the Firebase libraries when initialised by the `ContentProvider`s. Also `setCrashlyticsCollectionEnabled` is enabled by default, afaik. – Darshan Feb 02 '21 at 08:23
1

Module build.gradle

 apply plugin: 'com.google.firebase.crashlytics'
 apply plugin: 'com.google.gms.google-services'
  android {...

 dependencies {
   implementation 'com.google.firebase:firebase-analytics-ktx:17.5.0'
   implementation 'com.google.firebase:firebase-crashlytics-ktx:17.2.1'

Project build.gradle

   dependencies {
    classpath "com.android.tools.build:gradle:4.0.1"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72"
    classpath 'com.google.gms:google-services:4.3.3'
    classpath 'com.google.firebase:firebase-crashlytics-gradle:2.3.0'
}

MainActivity.kt

 class MainActivity : AppCompatActivity() {
    companion object {
         lateinit var mCrash: FirebaseCrashlytics           
         lateinit var instance: MainActivity
         private set
     }
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
         instance = this
        FirebaseApp.initializeApp(instance)
         mCrash = FirebaseCrashlytics.getInstance()
           val exception = Exception("test Exception")
           mCrash.recordException(exception)

enter image description here

AllanRibas
  • 678
  • 5
  • 14
0

Adding to the other answers, take care that recordException(e) method doesn't send exceptions in time to crashlytics as it's mentioned in their docs

Crashlytics batches logged exceptions together and sends them the next time the app launches.