StrictMode violations

In order to quickly understand and fix errors, it is often helpful to send Android StrictMode policy violations to Bugsnag.

This documentation is for version 5 of the Bugsnag Android notifier. If you are using older versions, we recommend upgrading to the latest release using our Upgrade guide. Documentation for the previous release can be found on our legacy pages.

Detecting StrictMode violations with Bugsnag

StrictMode is a tool that detects common mistakes made during development, such as performing I/O on the main thread.

Bugsnag can capture error reports for StrictMode violations that give instant observability into when an application is misbehaving.

You should only enable StrictMode in development/QA builds.

Android 9 and above

Bugsnag provides pre-made listeners that hook into StrictMode’s penaltyListener method and send an error report whenever a violation occurs.

You can enable detection on Android 9 and above by adding the following code to your Application subclass:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    // you must always initialize Bugsnag before setting up StrictMode
    Bugsnag.start(context);
    Executor executor = context.getMainExecutor();

    // setup StrictMode policy for thread violations
    BugsnagThreadViolationListener threadListener = new BugsnagThreadViolationListener();
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
            .detectAll()
            .penaltyListener(executor, threadListener)
            .build());

    // setup StrictMode policy for VM violations
    BugsnagVmViolationListener vmListener = new BugsnagVmViolationListener();
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
            .detectAll()
            .penaltyListener(executor, vmListener)
            .build());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    // you must always initialize Bugsnag before setting up StrictMode
    Bugsnag.start(context)
    val executor = context.mainExecutor

    // setup StrictMode policy for thread violations
    val threadListener = BugsnagThreadViolationListener()
    StrictMode.setThreadPolicy(
        StrictMode.ThreadPolicy.Builder()
            .detectAll()
            .penaltyListener(executor, threadListener)
            .build()
    )

    // setup StrictMode policy for VM violations
    val vmListener = BugsnagVmViolationListener()
    StrictMode.setVmPolicy(
        StrictMode.VmPolicy.Builder()
            .detectAll()
            .penaltyListener(executor, vmListener)
            .build()
    )
}

Android 8 and below

If you wish to capture StrictMode violations in older Android versions then you should use the penaltyDeath() option. This throws an uncaught JVM exception whenever a StrictMode violation occurs.

You can enable detection on older Android versions by adding the following code to your Application subclass:

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
            .detectAll()
            .penaltyDeath()
            .build());

StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
            .detectAll()
            .penaltyDeath()
            .build());
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder()
            .detectAll()
            .penaltyDeath()
            .build())

StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder()
            .detectAll()
            .penaltyDeath()
            .build())