How to log non-fatal (caught) exceptions with Firebase (Crashlytics)

限于喜欢 提交于 2020-12-04 14:39:26

问题


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?

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


回答1:


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




回答2:


You can use

Crashlytics.logException(exception);

to log non fatal issues on crashlytics




回答3:


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)



来源:https://stackoverflow.com/questions/51714355/how-to-log-non-fatal-caught-exceptions-with-firebase-crashlytics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!