Crashlytics on Android - how to fix all exceptions / failures in one place

Using Java (android, but this is a java question), I created a default exception handler in the application class, like this more or less, which was taken from some SO stream:

public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); AppInitialization(); } private void AppInitialization() { defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler); } private UncaughtExceptionHandler defaultUEH; // handler listener private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread thread, Throwable ex) { ex.printStackTrace(); // TODO handle exception here } }; } 

Let's talk about what my goal is. I need to record additional information for crashLytics crash reports. therefore, a hook uncaughtException (Thread thread, Throwable ex) is thrown at any time. I assume this will work for crashes and exceptions (not sure).

3 questions on this issue:

  • this will affect how crashLytics works as im overloads the default exception handler.

  • This will allow the application to continue working - I do not want this. I only want to register the exception elsewhere and continue it as if defaultexceptionHandler does not exist. Otherwise, QA regression will not be easy to see errors, so that we can fix them. If I call defaultUEH.uncaughtException (thread, ex) from within uncaughtException (thread, ex), will it continue the loop?

  • What thread is associated with setDefaultUncaughtExceptionHandler? Does this work for all threads, for example, or only the main thread?

So, to be clear, I need a way to send additional information about each crashlytics crash / exception. How?

UPDATE: I see here that someone has found a solution, but using this method, does he also crash-report fatal issues?

+1
source share
2 answers

I found a way to do this cleanly and it checks well. be careful with Crashlytics.log to send logs during crash reports. doesn't seem to work consistently on Crashlytics version 2.3.14.151. Instead, I use Crashlytics.setString (key, value). crashlytics.log seems to work fine with crashlytics.logException (..).

another question I had was whether he would work on exceptions to the ashram. Answer: yes, I experienced it myself.

therefore, it captures all exceptions from all threads.

The rest of the idea is similar to here . Man uses the decorator pattern over the standard ExceptionHandler to add functionality to the uncaughtException method.

Here is how I improved it:

  public class DefaultUnCaughtExceptionHandlerDecorator implements UncaughtExceptionHandler { private UncaughtExceptionHandler mDefaultUncaughtExceptionHandler; //we will decorate the default handler to add functionality to it public DefaultUnCaughtExceptionHandlerDecorator(UncaughtExceptionHandler mDefaultUncaughtExceptionHandler) { this.mDefaultUncaughtExceptionHandler = mDefaultUncaughtExceptionHandler; } @Override public void uncaughtException(Thread t, Throwable e) { logToCrashLytics(); //add our crashlytics logging and keys here // we have added our crashlytics extra data, now invoke Crashlytics mDefaultUncaughtExceptionHandler.uncaughtException(t, e); } private void logToCrashLytics() { Crashlytics.setUserIdentifier(Model.getUserID());//or whatever. this will show directly on crashlytics dashboard a pretty element Crashlytics.setString("Some useful Info","user is logged in"); // or whatever you want, such as printing out singleton managers references or model info //dont use Crashlytics.Log(..) here. it wont work consistent. } 

}

now in your subclass of the Application AFTER crashlytics class the following is installed:

 Thread.setDefaultUncaughtExceptionHandler(new DefaultUnCaughtExceptionHandlerDecorator(Thread.getDefaultUncaughtExceptionHandler())); 
+1
source

You can handle uncaught exceptions using the FireCrasher library , recover from it, and send user feedback as a dialog or message.

You can learn more about the library in this middle article.

0
source

All Articles