ANR-WatchDog Documentation

repository·master·Indexed 25 days ago

https://github.com/salomonbrys/anr-watchdog

A library for monitoring Application Not Responding (ANR) events in Android. It provides features to configure custom timeouts, handle errors via ANRListener to prevent crashes, filter reports by thread name prefix, and intercept events using ANRInterceptor.

Tokens
1.1K
Snippets
7
Records
8
Agent score
34%

What's inside ANR-WatchDog

  1. Filter ANR reports by thread name prefix

    master

    To limit the number of threads reported in the ANRError stack trace, use setReportThreadNamePrefix(String prefix). Only threads whose names start with this prefix will be included in the report.

    When starting your own threads, ensure you set their name to include this prefix.

    new ANRWatchDog().setReportThreadNamePrefix("APP:").start();
    
    // In your thread:
    public class MyAmazingThread extends Thread {
        @Override
        public void run() {
            setName("APP: Amazing!");
            /* ... do amazing things ... */
        }
    }
  2. Configure ANR-WatchDog timeout

    master

    You can specify a custom timeout (the minimum hanging time required to trigger an ANR) in milliseconds. The default is 5000ms.

    // Example: Set timeout to 10 seconds
    if (BuildConfig.DEBUG == false) {
      new ANRWatchDog(10000 /*timeout*/).start();
    }
    if (BuildConfig.DEBUG == false) {
      new ANRWatchDog(10000 /*timeout*/).start();
    }
  3. Configure ANR-WatchDog debugger behavior

    master

    By default, the watchdog ignores ANRs if a debugger is attached to avoid false positives caused by breakpoints. To force the watchdog to report ANRs even when a debugger is connected, use setIgnoreDebugger(true).

    new ANRWatchDog().setIgnoreDebugger(true).start();
  4. Use an ANR callback instead of crashing

    master

    By default, ANR-WatchDog throws an ANRError when an ANR is detected. To prevent the application from crashing and instead handle the error manually (e.g., logging it to a crash reporting service), use setANRListener with an ANRListener implementation.

    Note: This is recommended for production environments to avoid force-closing the app for the user.

    new ANRWatchDog().setANRListener(new ANRWatchDog.ANRListener() {
        @Override
        public void onAppNotResponding(ANRError error) {
            // Handle the error. For example, log it to HockeyApp:
            ExceptionHandler.saveException(error, new CrashManager());
        }
    }).start();
  5. Use an ANRInterceptor to postpone error reporting

    master

    An ANRInterceptor allows you to intercept an ANR event before the error is raised. The intercept(long duration) method receives the current freeze duration. Returning a value greater than 0 will postpone the error for that many milliseconds.

    This is useful if you want to detect a freeze but wait longer before officially declaring it an ANR.

    new ANRWatchDog(2000).setANRInterceptor(new ANRWatchDog.ANRInterceptor() {
        @Override
        public long intercept(long duration) {
            long ret = 5000 - duration;
            if (ret > 0) {
                Log.w(TAG, "Intercepted ANR that is too short (" + duration + " ms), postponing for " + ret + " ms.");
            }
            return ret;
        }
    })
  6. Understand ANRError stack trace reports

    master

    The ANRError exception contains stack traces for all running threads. In the exception report, each Caused by section represents a different thread, not a nested exception.

    If you are using a crash reporter like Crashlytics that already reports all thread stack traces, it is recommended to use setReportMainThreadOnly() to avoid redundant and cumbersome reports.