gpt4 book ai didi

crashlytics-android - 如何检查 Firebase Crashlytics 是否已初始化?

转载 作者:行者123 更新时间:2023-12-04 13:37:17 29 4
gpt4 key购买 nike

以前的 Fabric SDK 允许这样的检查

if (Fabric.isInitialized()) {
crashSetParam(CrashParameters.ROOT_NODE, nodeInfoCompat.toString())
Crashlytics.logException(ex)
}

我无法确定如何在新的 Firebase Android SDK 中执行此操作,这会导致我的 junit 测试可能会崩溃。我意识到我可以将它包装在一个接口(interface)中,但我也很好奇如何以上面的简单方式解决它。谢谢你。

我也在 github 上记录了一个问题:

https://github.com/firebase/firebase-android-sdk/issues/1437

如果没有检查,我们在运行单元测试时会得到这样的结果:
java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process null. Make sure to call FirebaseApp.initializeApp(Context) first.

我现在已经用以下方法修复了它:
fun logException(ex: Throwable, nodeInfoCompat: AccessibilityNodeInfoCompat?) {
// We check to make sure Fabric is initialized or else the tests will fail - see above comment
try {
FirebaseCrashlytics.getInstance().setCustomKey(CrashParameters.ROOT_NODE, nodeInfoCompat.toString())
FirebaseCrashlytics.getInstance().recordException(ex)
} catch (e: IllegalStateException) {
Timber.e(e);
}
}

最佳答案

我发现的另一个解决方案是将它放在主应用程序类中

if (FirebaseApp.getApps(getApplicationContext()).isEmpty()) {
FirebaseApp.initializeApp(getApplicationContext());}

我只测试了一点,但是在运行测试时,它总是会为 isEmpty() 返回 true,并且在正常使用时它会返回 false。

关于crashlytics-android - 如何检查 Firebase Crashlytics 是否已初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61104162/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com