未调用 Crashlytics 侦听器

CrashlyticsListener not called

我正在使用此代码来跟踪应用何时崩溃:

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)

我正在使用 throw NullPointerException()Crashlytics.getInstance().crash() 对其进行测试。 None 其中调用监听器。当应用程序再次启动时,这是在日志中:

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

我做错了什么?

编辑 我使用了 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)

中的代码

默认情况下,Firebase Crashlytics 使用内容提供程序 hack 来自动初始化自身(com.crashlytics.android.CrashlyticsInitProvider 被注入到合并的 AndroidManifest 中)。

根据 documentation 自动初始化可以用 meta-data 标志覆盖:

<manifest>
    <application>

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

    </application>
</manifest>

现在调用 Fabric.with(context, crashlyticsKit) 将实际初始化 sdk 并且应该正确触发侦听器。

根据 https://docs.fabric.io/javadocs/crashlytics/2.6.8/deprecated-list.html 文档,我们必须使用 CrashlyticsCore.Builder().listener.

在项目 gradle 文件中放在依赖项下。

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
}

在 app.gradle 文件中放在下面 gradle 依赖:

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

在清单文件中将此元数据标记放在应用程序标记下。

在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
    }

  }
}

我的测试代码运行良好。我附上了日志截图。