Nikita Khlebushkin
Nikita Khlebushkin

Reputation: 573

CrashlyticsListener not called

I am using this code to track when the app has crashed:

val core = CrashlyticsCore
                .Builder()
                .listener {
                    Log.d("***", "Crash happened")
                }
                .build()
        val crashlyticsKit = Crashlytics
                .Builder()
                .core(core)
                .build()
        // Initialize Fabric with the debug-location_inactive crashlytics.
        Fabric.with(context, crashlyticsKit)

I am testing it with throw NullPointerException() and with Crashlytics.getInstance().crash(). None of them calls listener. When the app starts again, this is in the logs:

I/CrashlyticsCore: Initializing Crashlytics 2.6.1.23
I/CrashlyticsInitProvider: CrashlyticsInitProvider initialization successful
D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
I/CrashlyticsCore: Crashlytics report upload complete: SOME-LETTERS-AND-NUMBERS

What am I doing wrong?

EDIT I used code from How to show a Dialog after crash by using Crashlytics? as a template for mine, but it seems that the API has slightly changed (in this answer, it instantiates as a class, but now it is a listener, see docs)

Upvotes: 3

Views: 1851

Answers (2)

Sergii Pechenizkyi
Sergii Pechenizkyi

Reputation: 22232

By default, Firebase Crashlytics is using content provider hack to automatically initialize itself (com.crashlytics.android.CrashlyticsInitProvider is injected into merged AndroidManifest).

According to the documentation automatic initialization can be overridden with meta-data flag:

<manifest>
    <application>

        <meta-data
            android:name="firebase_crashlytics_collection_enabled"
            android:value="false" />

    </application>
</manifest>

Now calling Fabric.with(context, crashlyticsKit) will actually initialize sdk and should trigger listener correctly.

Upvotes: 5

Darshan Mistry
Darshan Mistry

Reputation: 3372

As per https://docs.fabric.io/javadocs/crashlytics/2.6.8/deprecated-list.html document we have to use CrashlyticsCore.Builder().listener.

In project gradle file put below dependency.

buildscript {
    ext.kotlin_version = '1.3.21'
    repositories {
        google()
        jcenter()
        //TODO for fabric crash
        maven {
            url 'https://maven.fabric.io/public'
        }

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.0-alpha07'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        //TODO for fabric crash
        classpath 'com.google.gms:google-services:4.2.0'
        classpath 'io.fabric.tools:gradle:1.26.1'
        //TODO end
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()


    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

in app.gradle file put below gradle in dependency:

    //TODO fabric crash 
    implementation 'com.google.firebase:firebase-core:16.0.7'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.9'
    //TODO end

In manifest file put this meta-data tag under application tag.

in MainActivity.Kt

package com.darshan.crahdemo

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.crashlytics.android.Crashlytics
import com.crashlytics.android.core.CrashlyticsCore
import io.fabric.sdk.android.Fabric
import kotlinx.android.synthetic.main.activity_main.tvCrash

class MainActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val core = CrashlyticsCore
      .Builder()
      .listener {
        Log.d("****************", "Crash happened")
      }
      .build()

    val crashlyticsKit = Crashlytics
      .Builder()
      .core(core)
      .build()

    Fabric.with(this, crashlyticsKit)

    tvCrash.text = "Crash!"
    tvCrash.setOnClickListener {
      Crashlytics.getInstance().crash() // Force a crash
    }

  }
}

I have test code is work perfectly. I have attached a screenshot of logs.

enter image description here

Upvotes: 1

Related Questions