3

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)

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Possible duplicate of [How to show a Dialog after crash by using Crashlytics?](https://stackoverflow.com/questions/28319583/how-to-show-a-dialog-after-crash-by-using-crashlytics) – Martin Zeitler Mar 04 '19 at 02:14
  • @MartinZeitler please see my edit, where I explain why your suggestion would not be valid for current API version – Nikita Khlebushkin Mar 04 '19 at 05:48
  • see `CrashlyticsCore.Builder`: https://docs.fabric.io/javadocs/crashlytics-core/2.6.2/com/crashlytics/android/core/CrashlyticsCore.Builder.html ...the only thing that changed is, that `Crashlytics.setListener()` had been deprecated and replaced with `CrashlyticsCore.Builder.listener(com.crashlytics.android.core.CrashlyticsListener)`. `Log` is in every case not an instance of `CrashlyticsListener`. – Martin Zeitler Mar 04 '19 at 13:44
  • @MartinZeitler I mean that the listener(com.crashlytics.android.core.CrashlyticsListener, see the docs you provided) is an interface and thus, my syntax is correct. Building of the same code lines would prove you that – Nikita Khlebushkin Mar 05 '19 at 10:30
  • @NikitaKhlebushkin It should work the way it's written above! where do you call the initialization code? – Ahmed Hegazy Mar 12 '19 at 08:56
  • @AhmedHegazy in Application::onCreate() I call a helper method `initialize(application: Application)`, in which I do all library initialization. It goes AFTER `super.onCreate()` – Nikita Khlebushkin Mar 12 '19 at 09:44
  • I have the same code written above and it's working, that's why I'm skeptical. Do you call the Fabric init line elsewhere with `Fabric.with(context, Crashylitics())` maybe? Do you have the latest version of the library? – Ahmed Hegazy Mar 12 '19 at 13:30
  • @AhmedHegazy Only there. The version I tried: 2.9.1, 2.9.3. I understand your skepticism. This code is pretty straightforward and was working on other people's projects, so there must me something else, I just don't know where to look for it – Nikita Khlebushkin Mar 12 '19 at 14:21
  • I have 2.9.9, could you try this version? – Ahmed Hegazy Mar 12 '19 at 14:26
  • @AhmedHegazy I tried 2.9.9, still no success – Nikita Khlebushkin Mar 13 '19 at 12:47

2 Answers2

5

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.

Sergii Pechenizkyi
  • 22,227
  • 7
  • 60
  • 71
1

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

Darshan Mistry
  • 3,294
  • 1
  • 19
  • 29