Crashlytics Android SDK -自定义UncaughtExceptionHandler

eyh26e7m  于 2023-04-10  发布在  Android
关注(0)|答案(7)|浏览(131)

是否可以在一个应用程序中合并自定义UncaughtExceptionHandler沿着crashlytics?如果可以,如何合并?

lstz6jyr

lstz6jyr1#

更新

请参阅@kmityak answer,因为Crashlytics/Fabric初始化现在是异步的,我下面的解决方案不再有效。

原始答案

您可以设置自定义UncaughtExceptionHandler,前提是它会将异常传递给默认的UncaughtExceptionHandler,以便稍后通过Crashlytics处理。
下面的代码在Application子类中实现:

private static Thread.UncaughtExceptionHandler mDefaultUEH;
private static Thread.UncaughtExceptionHandler mCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
    @Override 
    public void uncaughtException(Thread thread, Throwable ex) {
        // Custom logic goes here

        // This will make Crashlytics do its job
        mDefaultUEH.uncaughtException(thread, ex);
    }
};

@Override
public void onCreate() {
    super.onCreate();

    // Order is important!
    // First, start Crashlytics
    Crashlytics.start(this);

    // Second, set custom UncaughtExceptionHandler
    mDefaultUEH = Thread.getDefaultUncaughtExceptionHandler();
    Thread.setDefaultUncaughtExceptionHandler(mCaughtExceptionHandler);
}

第二种选择是在设置了自定义UncaughtExceptionHandler之后注册Crashlytics--然后所有未捕获的异常都会被Crashlytics报告为致命异常,然后传递给自定义处理程序。

ukdjmx9f

ukdjmx9f2#

由于Crashlytics的最新版本异步执行初始化,因此最好使用Fabric的初始化回调:

private static Thread.UncaughtExceptionHandler mDefaultUEH;
private static Thread.UncaughtExceptionHandler mCaughtExceptionHandler = 
   new Thread.UncaughtExceptionHandler() {
     @Override 
public void uncaughtException(Thread thread, Throwable ex) {
        // Custom logic goes here

        // This will make Crashlytics do its job
        mDefaultUEH.uncaughtException(thread, ex);
    }
};

CrashlyticsCore core = new CrashlyticsCore.Builder()
            .disabled(BuildConfig.DEBUG)
            .build();
Fabric.with(new Fabric.Builder(this).kits(new Crashlytics.Builder()
            .core(core)
            .build())
            .initializationCallback(new InitializationCallback<Fabric>() {
                @Override
                public void success(Fabric fabric) {
                    mDefaultUEH = Thread.getDefaultUncaughtExceptionHandler();
                    Thread.setDefaultUncaughtExceptionHandler(mCaughtExceptionHandler);
                }

                @Override
                public void failure(Exception e) {

                }
            })
            .build());
raogr8fs

raogr8fs3#

是的,有可能。
在Application类中:

@Override
public void onCreate() {
    super.onCreate();
    Crashlytics.start(this);
    initUncaughtExceptionHandler();
}

private void initUncaughtExceptionHandler() {
    final ScheduledThreadPoolExecutor c = new ScheduledThreadPoolExecutor(1);
    c.schedule(new Runnable() {
        @Override
        public void run() {
            final UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
            Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
                @Override
                public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
                    // do my amazing stuff here 
                    System.err.println("Error!");
                    //then pass the job to the previous handler
                    defaultHandler.uncaughtException(paramThread, paramThrowable);
                }
            });
        }
    }, 5, TimeUnit.SECONDS);
}

我之所以在5秒后安排这个是因为Crashlytics需要一些时间来设置他的东西。我使用这个代码,它工作得很好。当然,如果你的应用程序在启动时崩溃,对不起,但没有自定义处理程序;)

9gm1akwq

9gm1akwq4#

如果这些解决方案对我有效,就没有。我是这样做的:

// Setup handler for uncaught exceptions.
Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
{
    @Override
    public void uncaughtException (Thread thread, Throwable e)
    {
        //Send Report to Crashlytics. Crashlytics will send it as soon as it starts to work
        Crashlytics.logException(e);

        //Your custom codes to Restart the app or handle this crash
        HandleCrashes(thread, e);
    }
});

这是我的自定义方法来重新启动应用程序:

private void HandleCrashes(Thread t, Throwable e) {

    Intent i = new Intent(mContext, frmLogin.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.putExtra("JustLogin", true);
    startActivity(i);

    System.exit(1);
}
58wvjzkj

58wvjzkj5#

1.关闭自动收集
将此添加到AndroidManifest.xml

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

1.注册您的客户UncaughtExceptionHandler

Thread.setDefaultUncaughtExceptionHandler(customerUncaughtExceptionHandler)

1.注册UncaughtExceptionHandler后手动启动Crashlytics

Fabric.with(this, new Crashlytics());

1.重建并重新安装应用程序

628mspwn

628mspwn6#

我找到了Fabric 2.10.1的解决方案:

Thread.setDefaultUncaughtExceptionHandler(yourExceptionHandler)
Fabric.with(this, Crashlytics())
dgsult0t

dgsult0t7#

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread thread, Throwable throwable) {
        // Delegate to the default uncaught exception handler provided by the system
        Thread.getDefaultUncaughtExceptionHandler().uncaughtException(thread, throwable);
    }
});

// Initialize Firebase Crashlytics
FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true);

// Set Firebase Crashlytics as the default uncaught exception handler
Thread.setDefaultUncaughtExceptionHandler(FirebaseCrashlytics.getInstance().getUncaughtExceptionHandler());

相关问题