Crashczyk library

Android library that allows launching a custom activity when your app crashes, instead of showing the hated "Unfortunately, X has stopped" dialog.

License

License

Categories

Categories

CRaSH General Purpose Libraries Utility
GroupId

GroupId

com.henorek
ArtifactId

ArtifactId

crashczyk
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

aar
Description

Description

Crashczyk library
Android library that allows launching a custom activity when your app crashes, instead of showing the hated "Unfortunately, X has stopped" dialog.
Project URL

Project URL

https://github.com/henorek/crashczyk
Source Code Management

Source Code Management

https://github.com/henorek/crashczyk.git

Download crashczyk

How to add to project

<!-- https://jarcasting.com/artifacts/com.henorek/crashczyk/ -->
<dependency>
    <groupId>com.henorek</groupId>
    <artifactId>crashczyk</artifactId>
    <version>1.0.0</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.henorek/crashczyk/
implementation 'com.henorek:crashczyk:1.0.0'
// https://jarcasting.com/artifacts/com.henorek/crashczyk/
implementation ("com.henorek:crashczyk:1.0.0")
'com.henorek:crashczyk:aar:1.0.0'
<dependency org="com.henorek" name="crashczyk" rev="1.0.0">
  <artifact name="crashczyk" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.henorek', module='crashczyk', version='1.0.0')
)
libraryDependencies += "com.henorek" % "crashczyk" % "1.0.0"
[com.henorek/crashczyk "1.0.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

Crashczyk library

This library launch a custom activity with Krzysztof Krawczyk (Polish singer) quotes when your app crashes. It's based on Ereza project and it's directed to Polish users.

Plans for the future

  • Handle different exceptions with different sets of quotes
  • Rewrite whole code to Kotlin

How to use

1. Add a dependency

Add the following dependency to your build.gradle:

dependencies {
    compile 'com.henorek:crashczyk:1.0.0'
}

You can also do it manually, by downloading the source code, importing the library folder as an Android Library Module, and adding a dependency on your project to that module.

2. Set up your application

On your application class, use this snippet:

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

        //Install Crashczyk
        Crashczyk.install(this);

        //Now initialize your error handlers as normal
        //i.e., ACRA.init(this);
        //or Crashlytics.start(this);
    }

WARNING! If you already have ACRA, Crashlytics or any similar library in your app, it will still work as normal, but the CustomActivityOnCrash initialization MUST be done first, or the original reporting tool will stop working.

3. Test it

Make the app crash by using something like this in your code:

throw new RuntimeException("Boom!");

The error activity should show up, instead of the system dialog.

Optional: Customization

Custom behavior

You can call the following methods at any time to customize how the library works, although usually you will call them before calling install(context):

Crashczyk.setLaunchErrorActivityWhenInBackground(boolean);

This method defines if the error activity should be launched when the app crashes while on background. By default, this is true. On API<14, it's always true since there is no way to detect if the app is in foreground. If you set it to false, a crash while in background won't launch the error activity nor the system dialog, so it will be a silent crash. The default is true.

Crashczyk.setShowErrorDetails(boolean);

This method defines if the error activity must show a button with error details. If you set it to false, the button on the default error activity will disappear, thus disabling the user from seeing the stack trace. The default is true.

Crashczyk.setDefaultErrorActivityDrawable(int);

This method allows changing the default upside-down bug image with an image of your choice. You may pass a resource id for a drawable or a mipmap. The default is R.drawable.crashczyk_error_image.

Crashczyk.setEnableAppRestart(boolean);

This method defines if the error activity must show a "Restart app" button or a "Close app" button. If you set it to false, the button on the default error activity will close the app instead of restarting. Warning! If you set it to true, there is the possibility of it still displaying the "Close app" button, if no restart activity is specified or found! The default is true.

Crashczyk.setRestartActivityClass(Class<? extends Activity>);

This method sets the activity that must be launched by the error activity when the user presses the button to restart the app. If you don't set it (or set it to null), the library will use the first activity on your manifest that has an intent-filter with action cat.ereza.customactivityoncrash.RESTART, and if there is none, the default launchable activity on your app. If no launchable activity can be found and you didn't specify any, the "restart app" button will become a "close app" button, even if setEnableAppRestart is set to true.

As noted, you can also use the following intent-filter to specify the restart activity:

<intent-filter>
    <!-- ... -->
    <action android:name="com.henorek.crashczyk.RESTART" />
</intent-filter>
Crashczyk.setErrorActivityClass(Class<? extends Activity>);

This method allows you to set a custom error activity to be launched, instead of the default one. Use it if you need further customization that is not just strings, colors or themes (see below). If you don't set it (or set it to null), the library will use first activity on your manifest that has an intent-filter with action com.henorek.crashczyk.ERROR, and if there is none, a default error activity from the library. If you use this, the activity must be declared in your AndroidManifest.xml, with process set to :error_activity.

Example:

<activity
    android:name="com.henorek.sample.crashczyk.activity.CustomErrorActivity"
    android:label="@string/error_title"
    android:process=":error_activity" />

As noted, you can also use the following intent-filter to specify the error activity:

<intent-filter>
    <!-- ... -->
    <action android:name="com.henorek.crashczyk.ERROR" />
</intent-filter>

Customization of the default activity

You can override several resources to customize the default activity:

Theme:

You can override the default error activity theme by defining a theme in your app with the following id: CrashczykTheme

Image:

By default, an image of a bug is displayed. You can change it to any image by creating a crashczyk_error_image drawable on all density buckets (mdpi, hdpi, xhdpi, xxhdpi and xxxhdpi). You can also use the provided Crashczyk.setDefaultErrorActivityDrawable(int) method.

Strings:

You can provide new strings and translations for the default error activity strings by overriding the following strings:

    <string name="crashczyk_error_activity_error_occurred_explanation">An unexpected error occurred.\nSorry for the inconvenience.</string>
    <string name="crashczyk_error_activity_unknown_exception">Unknown exception</string>
    <string name="crashczyk_error_activity_restart_app">Restart app</string>
    <string name="crashczyk_error_activity_close_app">Close app</string>
    <string name="crashczyk_error_activity_error_details">Error details</string>
    <string name="crashczyk_error_activity_error_details_title">Error details</string>
    <string name="crashczyk_error_activity_error_details_close">Close</string>
    <string name="crashczyk_error_activity_error_details_copy">Copy to clipboard</string>
    <string name="crashczyk_error_activity_error_details_copied">Copied to clipboard</string>
    <string name="crashczyk_error_activity_error_details_clipboard_label">Error information</string>

There is a sample project module with examples of these overrides. If in doubt, check the code in that module.

Completely custom error activity

If you choose to create your own completely custom error activity, you can use these methods:

Crashczyk.getStackTraceFromIntent(getIntent());

Returns the stack trace that caused the error as a string.

Crashczyk.getAllErrorDetailsFromIntent(getIntent());

Returns several error details including the stack trace that caused the error, as a string. This is used in the default error activity error details dialog.

Crashczyk.getRestartActivityClassFromIntent(getIntent());

Returns the class of the activity you have to launch to restart the app, or null if not set.

Crashczyk.restartApplicationWithIntent(activity, intent);

Kills the current process and restarts the app again with an startActivity() to the passed intent. You MUST call this to restart the app, or you will end up having several Application class instances and experience multiprocess issues in API<17.

Crashczyk.closeApplication(activity);

Closes the app and kills the current process. You MUST call this to close the app, or you will end up having several Application class instances and experience multiprocess issues in API<17.

The sample project module includes an example of a custom error activity. If in doubt, check the code in that module.

Using Proguard?

No need to add special rules, the library should work even with obfuscation.

Inner workings

This library relies on the Thread.setDefaultUncaughtExceptionHandler method. When an exception is caught by the library's UncaughtExceptionHandler it does the following:

  1. Captures the stack trace that caused the crash
  2. Launches a new intent to the error activity passing the stacktrace as an extra.
  3. Kills the current process.

The inner workings are based on ACRA's dialog reporting mode with some minor tweaks. Look at the code if you need more detail about how it works.

Incompatibilities

  • Crashczyk will not work in these cases:
    • With any custom UncaughtExceptionHandler set after initializing the library, that does not call back to the original handler.
    • With ACRA enabled and reporting mode set to TOAST or DIALOG.
  • If you use a custom UncaughtExceptionHandler, it will not be called if you initialize it before the library initialization (so, Crashlytics or ACRA initialization must be done after Crashczyk initialization).
  • On some rare cases on devices with API<14, the app may enter a restart loop when a crash occurs. Therefore, using it on API<14 is not recommended.
  • If your app initialization or error activity crash, there is a possibility of entering an infinite restart loop (this is checked by the library for the most common cases, but could happen in rarer cases).
  • The library has not been tested with multidex enabled. It uses Class.forName() to load classes, so maybe that could cause some problem. If you test it with multidex enabled, please provide feedback!
  • The library has not been tested with multiprocess apps. If you test it with such configuration, please provide feedback too!

Disclaimers

  • This will not avoid ANRs from happening.
  • This will not catch native errors.
  • There is no guarantee that this will work on every device.
  • This library will not make you toast for breakfast :)

Contributing & license

Any contribution in order to make this library better will be welcome!

The library is licensed under the Apache License 2.0.

The bug image used in the default error activity is licensed under CC-BY by Riff: https://www.sketchport.com/drawing/6119265933459456/lady-bug If you use the image in your app, don't forget to mention that!

Versions

Version
1.0.0