使用自定义UncaughtExceptionHandler升级Firebase Crashlytics

时间:2020-08-12 10:52:17

标签: android firebase crashlytics

Update your apps to use latest Firebase Crashlytics SDKs before November 15, 2020上工作时,激活我的自定义UncaughtExceptionHandler后,它将不再向Crashlytics发送崩溃。

下面的代码我将其设置在onCreate()的{​​{1}}中。

Application

当我删除自定义 FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance(); crashlytics.setCrashlyticsCollectionEnabled(!BuildConfig.DEBUG); // only release able to send. crashlytics.sendUnsentReports(); // send reports Thread.setDefaultUncaughtExceptionHandler((t, e) -> { FirebaseCrashlytics.getInstance().recordException(e); // record exception. // below things are restart app when crash appeared. PackageManager packageManager = getPackageManager(); Intent intent = packageManager.getLaunchIntentForPackage(getPackageName()); if (intent != null) { ComponentName componentName = intent.getComponent(); Intent mainIntent = Intent.makeRestartActivityTask(componentName); startActivity(mainIntent); Runtime.getRuntime().exit(0); } }); 时,它可以向Crashlytics报告。

使用Fabric库时,下面的代码可以正常工作。 (https://stackoverflow.com/a/49266303/4316327

UncaughtExceptionHandler

有什么解决办法吗?任何帮助将不胜感激。非常感谢。

2 个答案:

答案 0 :(得分:0)

您需要调用getUncaughtExceptionHandler并挂在它上面,然后设置自己的uncaughtexceptionhandler并在退出之前回叫我们的。

答案 1 :(得分:0)

解决方案已发布here。我将在这里将修改后的代码粘贴给任何需要它的人:

下面的代码是用Java编写的。对于Kotlin,请访问原始帖子。

public class NewCrashHandlerContentProvider extends ContentProvider {
    public static final String TAG = "NewCrashHandler";
    static long PRE_DELAY_MILLIS = 3000L;
    static long POST_DELAY_MILLIS = 3000L;

    public static void initializeAfterFirebaseContentProvider() {
        Thread.setDefaultUncaughtExceptionHandler(new PreFirebaseCrashHandler(Thread.getDefaultUncaughtExceptionHandler()));
    }

    @Override
    public boolean onCreate() {
        try {
            Log.i(TAG, "+onCreate()");
            Thread.setDefaultUncaughtExceptionHandler(new PostFirebaseCrashHandler(Thread.getDefaultUncaughtExceptionHandler()));
            return true;
        }finally {
            Log.i(TAG, "-onCreate()");
        }
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }


    static class PreFirebaseCrashHandler implements Thread.UncaughtExceptionHandler {
        Thread.UncaughtExceptionHandler previousUncaughtExceptionHandler;
        public static final String TAG = "PreFirebaseCrashHandler";

        public PreFirebaseCrashHandler(Thread.UncaughtExceptionHandler previousUncaughtExceptionHandler) {
            this.previousUncaughtExceptionHandler = previousUncaughtExceptionHandler;
        }

        @Override
        public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
            LogWrapper.logException(e);
            try {
                // my own logic
                Thread.sleep(PRE_DELAY_MILLIS);
            }catch (Throwable throwable) {
                LogWrapper.e(TAG, throwable.getLocalizedMessage());
            } finally {
                previousUncaughtExceptionHandler.uncaughtException(t,e);
            }
        }
    }

    static class PostFirebaseCrashHandler implements Thread.UncaughtExceptionHandler {
        Thread.UncaughtExceptionHandler previousUncaughtExceptionHandler;
        public static final String TAG = "PostFirebaseCrashHandler";

        public PostFirebaseCrashHandler(Thread.UncaughtExceptionHandler previousUncaughtExceptionHandler) {
            this.previousUncaughtExceptionHandler = previousUncaughtExceptionHandler;
        }

        @Override
        public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
            LogWrapper.e(TAG, "+uncaughtException($thread, $throwable)");
            try {
                Thread.sleep(POST_DELAY_MILLIS);
            }catch (Throwable throwable) {
                LogWrapper.e(TAG, throwable.getLocalizedMessage());
            }finally {
                previousUncaughtExceptionHandler.uncaughtException(t,e);
            }
            LogWrapper.e(TAG, e.getLocalizedMessage());
        }
    }

}

在应用程序文件中,以onCreate()方法编写:

NewCrashHandlerContentProvider.initializeAfterFirebaseContentProvider();

也不要忘记更新manifests

<provider
            android:authorities="${applicationId}"
            android:name=".utils.NewCrashHandlerContentProvider"
            android:exported="false"
            android:initOrder="101"
            android:grantUriPermissions="false"/>