6

Problem: I’m working in a legacy project (it’s a very specific project) that require us to have a Custom Exception Handler that intercept an exception, kills the App before the crash occurs and restart the Application (without the crash dialog appears).

What I tried:

I'm using the following Custom Exception Handler to restart the Application process:

public class UncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {

    (…)

    @Override
    public void uncaughtException(final Thread thread, final Throwable throwable) {
        (…)

        AppUtils.restartApplication(context);
    }

}

And this is the AppUtils.restartApplication(context) code:

public final class AppUtils {

    (…)

    public static void restartApplication(@NonNull final Context context) {
        startApplication(context);
        closeApplication();
    }

    private static void startApplication(@NonNull final Context context) {
        final Intent rescueIntent = new Intent(context, Activity.class);
        rescueIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, rescueIntent, rescueIntent.getFlags());
        final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC, DateTime.now().plusSeconds(1).getMillis(), pendingIntent);
    }

    private static void closeApplication() {
        Process.killProcess(Process.myPid());
        System.exit(SYSTEM_EXIT_STATUS_FORCE_QUIT);
    }

}

Before we was using Splunk and it was working very fine. But now I need to add support to Firebase Crashlytics and Crashlytics has it owns ExceptionHandler.

So I came to two scenarios that doesn't work for me:

  1. Don't restart the App, so I can log it normally with Firebase;
  2. Restart the App, so I can't log it with Firebase;

I tried to bypass it using the logException method from Crashlytics inside my Custom Exception Handler logging everything was a non-fatal error, but it didn't work very well: sometimes it log properly, sometimes doesn't. I believe this happens because of the async in the Firebase inicialization and logException method.

I'd appreciate any help or suggestion. Thank you.

0 Answers0