7

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?

1 Answer 1

15

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">
8
  • I have thought about this, but does it work? Isn't it the way that Crashlytics work? Commented May 16, 2016 at 17:28
  • 2
    Yes, it will work and works the same way as Crashlytics but the difference being your custom UncaughtExceptionHandler will execute and then you call the default UncaughtExceptionHandler (Crashlytics) which will then execute. Commented May 16, 2016 at 17:41
  • Should I call the Thread.setDefaultUncaughtExceptionHandler function before, or after initialization of Crashlytics ? Where and what to put it exactly? Commented May 17, 2016 at 6:45
  • 1
    Is there any way to do the same thing for ndk? I need to get callback when a signal has been thrown. Commented Feb 2, 2018 at 15:11
  • 1
    I am also looking for an answer for this. Currently I have a partial solution which I posted on this thread: stackoverflow.com/questions/43112864/… You are welcome to review it and follow up on the thread, maybe we'll get Fabric guys to comment on it. Thanks!
    – brkeyal
    Commented Jun 7, 2018 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.