ACRA with android: how to execute code before crashing

I am using ACRA as an Android app. My question is: when a crash occurs, how can I make sure that I execute the code before the crash is reported. I would like to add some custom variables when the crash occurred, so I know what state the application was in?

Please, help

thanks

+3
source share
3 answers

Submit your own sender as described here . It could just be a wrapper around an existing sender, forwarding the send functions there.

Inside the sender code, you can take any special action before sending the report.

+2
source

This is what worked for me using ACRA 4.5.0 with the HttpSender backend. I am using Acralyzer in Cloudant.

Source:

@ReportsCrashes( mode = ReportingInteractionMode.DIALOG, resDialogText = R.string.crash_dialog_text, resDialogCommentPrompt = R.string.crash_dialog_comment_prompt, formUri = "https://kpc.cloudant.com/acra-openconnect/_design/acra-storage/_update/report", formUriBasicAuthLogin="[restricted reporter login]", formUriBasicAuthPassword="[restricted reporter password]", reportType = org.acra.sender.HttpSender.Type.JSON, httpMethod = org.acra.sender.HttpSender.Method.PUT, formKey = "" ) public class Application extends android.app.Application { public void onCreate() { super.onCreate(); ACRA.init(this); 

My application stores a circular buffer in memory with log messages. This is not written to disk, and I didn’t really want to display it on a line and call putCustomData () every time a record was added. So instead, I call my static "dumper" method, VPNLog.dumpLast (), to modify the report just before running HttpSender.send (). The new code is as follows:

 @ReportsCrashes( mode = ReportingInteractionMode.DIALOG, resDialogText = R.string.crash_dialog_text, resDialogCommentPrompt = R.string.crash_dialog_comment_prompt, formUri = "https://kpc.cloudant.com/acra-openconnect/_design/acra-storage/_update/report", formUriBasicAuthLogin="[restricted reporter login]", formUriBasicAuthPassword="[restricted reporter password]", // reportType and httpMethod are now defined below formKey = "" ) public class Application extends android.app.Application { public void onCreate() { super.onCreate(); ACRA.init(this); ACRA.getErrorReporter().setReportSender( new HttpSender(org.acra.sender.HttpSender.Method.PUT, org.acra.sender.HttpSender.Type.JSON, null) { @Override public void send(CrashReportData report) throws ReportSenderException { report.put(ReportField.APPLICATION_LOG, VPNLog.dumpLast()); super.send(report); } }); 

The value I'm adding is a long string (100+ lines); Acralyzer correctly breaks it into separate numbered lines.

Another option is to populate ReportField.CUSTOM_DATA with a few key / value pairs:

 report.put(ReportField.CUSTOM_DATA, "key0=value0\nkey1=value1\n"); 

Acralyzer displays them in an HTML table.

+1
source

Acra works using Thread.setDefaultUncaughtExceptionHandler (...);

You can install your own unhandled exception handler and then call ACRA to generate the report:

 public class My_Exception_Interceptor implements Thread.UncaughtExceptionHandler { /** * Sets up to catch an exception. * Call this after initializing ACRA. */ public void setUpFatalExceptionHandling() { Thread.setDefaultUncaughtExceptionHandler(this); } /** * Called by the JVM on an uncaught exception. * @param t - the thread * @param e - the throwable */ @Override public void uncaughtException(@Nullable Thread t, @NonNull Throwable e) { ...your code... ACRA.getErrorReporter().handleException(e, true); } } 
0
source

All Articles