Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a callback for when a crash occurred, while using Crashlytics SDK?

Background

We use Crashlytics SDK to manage app crashes and get needed information about them.

So far, the information that the SDK automatically gathered was enough

The problem

I'd like to add more information for each crash, such as: available&total heap memory, activity stack,...

Thing is, I don't see a way to achieve this.

I know that the way Android framework works with unhandled exceptions is pretty easy (using Thread.setDefaultUncaughtExceptionHandler) and it's probably how the SDK works, but I can't find where to use the listener of the SDK itself.

What I've tried

  1. The SDK has a listener, but it seems it's not of the current session, as shown here. The function name is "crashlyticsDidDetectCrashDuringPreviousExecution" , meaning it's of the previous session. Same callback was available before in deprecated methods.

  2. There are "Custom Logging" and "Custom Keys" features, but those occur when I call them (not right when the crash occurs).

The question

Is there a way to add extra information to Crashlytics right when a crash occurs ?

If so, how?

like image 884
android developer Avatar asked May 16 '16 14:05

android developer


1 Answers

Try creating an UncaughtExceptionHandler and use Custom Key(s) to store the information you want to be associated with your crash report.

  1. Create your custom UncaughtExceptionHandler (ensuring that it will pass exception to default UncaughtExceptionHandler to be handled later via Crashlytics).
  2. In the uncaughtException method add custom logic to set your key e.g. Crashlytics.setString("available_memory", "5784");

  3. Check your Crashlytics dashboard to view your custom key(s) when your app crashes

Create a custom Application subclass to hold your logic:

public class MyApplication extends Application {
   private static Thread.UncaughtExceptionHandler mDefaultUncaughtExceptionHandler;

   private static Thread.UncaughtExceptionHandler mCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
       @Override
       public void uncaughtException(Thread thread, Throwable ex) {
          // Custom logic goes here
          // Calculate available memory
          Crashlytics.setString("available_memory", "5784");
          // This will make Crashlytics do its job
          mDefaultUncaughtExceptionHandler.uncaughtException(thread, ex);
       }
   };

   @Override
   public void onCreate() {
     super.onCreate();

     // Order is important!
     // First, start Crashlytics
     Crashlytics.start(this);

     // Second, cache a reference to default uncaught exception handler
     mDefaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
     // Third, set custom UncaughtExceptionHandler
     Thread.setDefaultUncaughtExceptionHandler(mCaughtExceptionHandler);
   }
}

Remember to specify the name of your Application subclass in your AndroidManifest.xml’s tag

<application android:name="MyApplication">
like image 117
Clive Seebregts Avatar answered Nov 14 '22 22:11

Clive Seebregts