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?

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270

1 Answers1

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">
Community
  • 1
  • 1
Clive Seebregts
  • 2,004
  • 14
  • 18
  • I have thought about this, but does it work? Isn't it the way that Crashlytics work? – android developer May 16 '16 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. – Clive Seebregts May 16 '16 at 17:41
  • Should I call the Thread.setDefaultUncaughtExceptionHandler function before, or after initialization of Crashlytics ? Where and what to put it exactly? – android developer May 17 '16 at 06:45
  • Also, does crashlytics really use this function, as I've guessed? – android developer May 17 '16 at 06:54
  • But Crashlytics uses this function too? – android developer May 17 '16 at 07:08
  • 1
    Is there any way to do the same thing for ndk? I need to get callback when a signal has been thrown. – Fuat Coşkun Feb 02 '18 at 15:11
  • Did you get an answer to this @FuatCoşkun? I am also having the same problem and this is not solving the issue for me – Josh Laird Feb 18 '18 at 21:40
  • 1
    I am also looking for an answer for this. Currently I have a partial solution which I posted on this thread: https://stackoverflow.com/questions/43112864/crashlyticsdiddetectcrashduringpreviousexecution-for-crashlytics-ndk/50746103#50746103 You are welcome to review it and follow up on the thread, maybe we'll get Fabric guys to comment on it. Thanks! – brkeyal Jun 07 '18 at 16:39