将Crashlytics集成到React Native - Android中 [英] Integrating Crashlytics into React Native - Android

查看:191
本文介绍了将Crashlytics集成到React Native - Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我们的React Native项目成功地为iOS设置了Crashlytics集成,并设法获得了注册的Android版本。唯一的问题是它只报告Java /系统相关的崩溃 - 如果有一个JavaScript异常抛出它没有注册报告。我想让它起作用,因为iOS版本已经有了这个功能。

I've successfully set up Crashlytics integration for iOS for our React Native project, and have managed to get the android version registered as well. The only problem is that it only reports Java/system related crashes - if there is a javascript exception thrown it doesn't register with the reporting. I would like to get it working, as the iOS version has the functionality already.

我已经按照 Fabric docs 这个问题 - 看到第二个答案,我已经添加了 onCreate MainActivity。

I've set up the reporting as outlined in the Fabric docs and this question - see the second answer down, I've added the onCreate in MainActivity.

跟进我试图根据此问题评论。代码如下:

Following up I have attempted to create my own NativeModuleCallExceptionHandler as per this issue comment. Code for this is below:

package com.mypackage;

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.core.JavascriptException;
import com.facebook.react.bridge.NativeModuleCallExceptionHandler;

import java.util.Map;

import com.crashlytics.android.Crashlytics;

class CrashlyticsErrorModule extends ReactContextBaseJavaModule {

  public CrashlyticsErrorModule (ReactApplicationContext reactContext) {
    super(reactContext);
    addExceptionHandler(reactContext);
  }

  @Override
  public String getName() {
    return "CrashlyticsErrorModule";
  }

  private void addExceptionHandler(ReactApplicationContext reactContext) {
    reactContext.setNativeModuleCallExceptionHandler(new NativeModuleCallExceptionHandler() {
      @Override
      public void handleException(Exception e) {
        if (e instanceof JavascriptException) {
          Crashlytics.log(e.getMessage());
        } else {
          Crashlytics.logException(e);
        }
      }
    });
  }

代码编译没有错误,但JavaScript异常仍未报告。

The code compiles with no errors, however Javascript exceptions are still not being reported.

即使在 onCreate()的末尾刻意抛出运行时异常, addExceptionHandler()方法, handleException()不会被调用。 Crashlytics确实会收到通知,所以例外情况在某个地方被捕获。

Even when deliberately throwing runtime exceptions in onCreate() or at the end of the addExceptionHandler() method, handleException() does not get called. Crashlytics does get notified however, so the exceptions are getting caught somewhere!!

链接到源代码是在上面的问题评论。

Links to the source code are in the issue comment above.

推荐答案

https:// github.com/facebook/react-native/issues/1194

简而言之:您可以使用 ErrorUtils.setGlobalHandler((err ,isFatal)=> {...}); 捕获js异常并将其抛出Craslytics与 Crashlytics.recordCustomExceptionName(...) react-native-fabric-crashlytics npm模块可以为您自动进行。

In short: you can use ErrorUtils.setGlobalHandler((err, isFatal) => { ... }); to catch js exceptions and throw it to Craslytics with Crashlytics.recordCustomExceptionName(...). react-native-fabric-crashlytics npm module can do it for you automatically.

不要忘记添加 Fabric.with(this,new Crashlytics()) to onCreate()方法 MainApplication (或 MainActivity 如果您使用RN低于0.29)。

Don't forget to add Fabric.with(this, new Crashlytics()) to onCreate() method of MainApplication (or MainActivity if you use RN lower than 0.29).

这篇关于将Crashlytics集成到React Native - Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆