Crashlytics和AsyncTask

时间:2017-12-01 17:26:21

标签: android crashlytics

我将Crashlytics添加到应用程序中并运行了几项测试。当我在异步任务中抛出异常时,报告没有出现在控制台中。这是一个已知问题还是应该通过?

AsyncTask.execute(new Runnable()) {
    @Override public void run() {
        throw new RuntimeException("THIS IS A TEST");
    }
 }

我知道Crashlytics设置正确,因为从同一个函数抛出的异常但在AsyncTask包装器之外显示正常。 其他人可以与异步发生的崩溃分享他们的经验吗?

更新 我运行了更多的测试,我发现我的问题的一部分是我有一个未捕获异常的处理程序。我有这个,所以测试人员会得到一个对话框,他们可以点击确定以获得附加到电子邮件的logcat。 (感谢need-to-handle-uncaught-exception-and-send-log-file)我尝试了很多东西,在我的情况下,我只需要选择一个或另一个,未捕获的异常处理程序或崩溃。它以这种方式对我有用,因为我真的只想要生产+版本变体的崩解剂。 我尝试在异常处理程序的主体中包含Crashlytics.logException(e),但这并不起作用。可能是因为该函数刚刚调用System.exit(1)。无论如何......这就是我现在所做的工作。

要使用自定义应用程序类,请更新清单

    <application
    android:name=".util.App"

在App类中,我设置了未捕获的异常处理程序或崩溃。

public class App extends Application {
    public void onCreate() {
            super.onCreate();

            Constants.IS_DEV = BuildConfig.FLAVOR.equals("dev");

            if (Constants.IS_DEV || BuildConfig.DEBUG) {
                    setupUncaughtExceptionHandler();
            } else {
                    Fabric.with(this, new Crashlytics());
            }
            [SNIP]
    }

    private void setupUncaughtExceptionHandler() {

            // Setup handler for uncaught exceptions.
            Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
                    @Override
                    public void uncaughtException(Thread thread, Throwable e) {
                            handleUncaughtException(thread, e);
                    }
            });

    }

    public void handleUncaughtException(Thread thread, Throwable e) {

            // Crashlytics.logException(e); did not work here

            // create intent to launch new instance and show 'send_log' dialog
            Intent intent = new Intent();
            intent.setAction(BuildConfig.APPLICATION_ID + ".SEND_LOG");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            System.exit(1); // kill this instance
    }

    [SNIP]
}

我的测试只是我在设置页面中抓取的功能。他们只是将onClick方法设置为“onClick&#39;”的文本项目。 (原文:)

public class SettingsActivity extends DataSourcedActivity {

    [SNIP]

    public void onClick(View view) {
            if (view == findViewById(R.id.txtSettingsRemove)) {
                    launchRemoveItemsPage();
            } else if (view == findViewById(R.id.txtSettingsRestorePurch)) {
                    launchRestorePurchases();
            } else if (view == findViewById(R.id.txtContactSupport)) {
                    launchContactSupport();
            } else if (view == findViewById(R.id.txtGetUpdates)) {
                    launchGetUpdates();
            } else {
                    throw new DevException(Constants.UNKNOWN_SETTINGS_OPTION);
            }
    }

    private void launchRemoveCollectionsPage() {
            AsyncTask.execute(new Runnable()) {
                    @Override
                    public void run() {
                            throw new RuntimeException("THIS IS AN ASYNCHRONOUS TEST");
                    }
            }
            [SNIPPED ORIGINAL CONTENTS OF FUNCTION]
    }

    private void launchRestorePurchases() {
            throw new RuntimeException("THIS IS A TEST");
            [SNIPPED ORIGINAL CONTENTS OF FUNCTION]
    }

    [SNIP]
}

当我尝试同时使用Crashlytics和uncaughtException处理程序时,我得到了不同的结果,具体取决于我首先设置的结果。如果我先设置Crashlytics然后再设置我的uncaughtExceptionHandler,那么看起来我的覆盖Crashlytics,没有崩溃报告进入控制台。如果我先设置我的uncaughtExceptionHandler,那么我会在控制台上获取崩溃报告。

所以我要把这个留在这里,以防万一可能对其他人有所帮助。

麦克

1 个答案:

答案 0 :(得分:3)

坠机事件将在下一次发布时发生。崩溃将在本地记录,然后在您下次启动相同版本或应用程序时它在启动时发送报告。

请确保您正确启动崩解,并确保您第二次从设备上的同一个应用启动该应用,以确保它已被发送。从调试器中反复播放可能会导致将问题发送到仪表板的不良结果。

此外,在调试中,您可能会发现稍微延迟发布,我已经看到它需要多达5分钟。