4

We usually initialize Crashlytics in the very early stage in our Application.onCreate() method in main thread. The initialization process of Crashlytics is usually very fast, but it could still take more than 2 frames and as long as 32ms on some devices. Since we are constantly improving our cold start time, we thought about putting the Crashlytics initialization into a background thread and the concern is whether we can still capture a crash before the Crashlytics is initialized.

Our approach here is to set up a CustomDefaultUncaughtExceptionHandler as the first thing and initialize Crashlyatics really early as well but in a background thread. If we got a crash before the Crashlytics is initialized or to be initialized, we will reinitialize Crashlytics in the CustomDefaultUncaughtExceptionHandler and use reflection to get CrashlyticsCore's CrashlyticsUncaughtExceptionHandler and then log this uncaught exception.

Our ask is whether it's possible to make a public method for us to log a uncaught exception so that we don't have to use reflection. Or if there are some other suggestions for us to offload the Crashlytics initialization to a background thread but also be able to catch early exceptions.

The pseudo code looks like the following:

    public void uncaughtException(Thread thread, Throwable ex) {
        try {
            if (_crashlyticsEnabled) {
                if (!_crashlyticsInitialized) {
                    // IMPORTANT: in case we encounter a crash before the crashlytics is
                    // initialized, we can initialize crashlytics here and send this crash
                    // to the server right away.
                    initCrashlytics(_context);
                    Crashlytics.logUncaughtException(thread, ex); // API we are asking
                }
            }
        } catch (Exception e) {
            PLog.error(e, "Exception occurred during ...");
        } finally {
            _defaultExceptionHandler.uncaughtException(thread, ex);
        }
    }
Lin
  • 73
  • 4

0 Answers0