ANR-WatchDog


Source link: https://github.com/SalomonBrys/ANR-WatchDog

ANR-WatchDog

A simple watchdog that detects Android ANRs (Application Not Responding).

Table of contents

Why it exists

There is currently no way for an android application to catch and report ANR errors.
If your application is not in the play store (either because you are still developing it or because you are distributing it differently), the only way to investigate an ANR is to pull the file /data/anr/traces.txt.
Additionally, we found that using the Play Store was not as effective as being able to choose our own bug tracking service.

There is an issue entry in the android bug tracker describing this lack, feel free to star it ;)

What it does

It sets up a "watchdog" timer that will detect when the UI thread stops responding. When it does, it raises an error with all threads stack traces (main first).

Can it work with crash reporters?

Yes! I'm glad you asked: That's the reason why it was developed in the first place!
As this throws an error, a crash handler can intercept it and handle it the way it needs.

Known working crash reporters include:

And there is no reason why it should not work with [insert your favourite crash reporting system here].

How it works

The watchdog is a simple thread that does the following in a loop:

  1. Schedules a runnable to be run on the UI thread as soon as possible.
  2. Wait for 5 seconds. (5 seconds is the default, but it can be configured).
  3. See if the runnable has been run. If it has, go back to 1.
  4. If the runnable has not been run, which means that the UI thread has been blocked for at least 5 seconds, it raises an error with all running threads stack traces.

Usage

Install

With Gradle / Android Studio

  1. In the app/build.gradle file, add:

    compile 'com.github.anrwatchdog:anrwatchdog:1.3.+' 

    Alternatively, if you want to avoid the warning in Android Studio, use this format (note that you will not automatically receive fixes):

    compile 'com.github.anrwatchdog:anrwatchdog:1.3.0' 
  2. In your application class, in onCreate, add:

    new ANRWatchDog().start();
    

With Eclipse

  1. Download the latest jar

  2. Put the jar in the libs/ directory of your project

Reading the ANRError exception report

The ANRError stack trace is a bit particular, it has the stack traces of all the threads running in your application. So, in the report, each caused by section is not the cause of the precedent exception, but the stack trace of a different thread.

Here is a dead lock example:

FATAL EXCEPTION: |ANR-WatchDog|
  Process: anrwatchdog.github.com.testapp, PID: 26737
  com.github.anrwatchdog.ANRError: Application Not Responding
  Caused by: com.github.anrwatchdog.ANRError$_$_Thread: main (state = WAITING)

at testapp.MainActivity$1.run(MainActivity.java:46)

at android.os.Handler.handleCallback(Handler.java:739)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:135)

at android.app.ActivityThread.main(ActivityThread.java:5221)
  Caused by: com.github.anrwatchdog.ANRError$_$_Thread: APP: Locker (state = TIMED_WAITING)

at java.lang.Thread.sleep(Native Method)

at java.lang.Thread.sleep(Thread.java:1031)

at java.lang.Thread.sleep(Thread.java:985)

at testapp.MainActivity.SleepAMinute(MainActivity.java:18)

at testapp.MainActivity.access$100(MainActivity.java:12)

at testapp.MainActivity$LockerThread.run(MainActivity.java:36) 

From this report, we can see that the stack traces of two threads. The first (the "main" thread) is stuck at MainActivity.java:46 while the second thread (named "App: Locker") is locked in a Sleep at MainActivity.java:18.
From there, if we looked at those two lines, we would surely understand the cause of the dead lock!

Note that some crash reporting library (such as Crashlytics) report all thread stack traces at the time of an uncaught exception. In that case, having all threads in the same exception can be cumbersome. In such cases, simply use setReportMainThreadOnly().

Configuration

Timeout (minimum hanging time for an ANR)

  • To set a different timeout (5000 millis is the default):

    if (BuildConfig.DEBUG == false) {
    
      new ANRWatchDog(10000 /*timeout*/).start();
     
    }
    

Debugger

  • By default, the watchdog will ignore ANRs if the debugger is attached. This is because it detects execution pauses and breakpoints as ANRs. To disable this and throw an ANRError even if the debugger is connected, you can add setIgnoreDebugger(true):

    new ANRWatchDog().setIgnoreDebugger(true).start();
    

On ANR callback

  • If you would prefer not to crash the application when an ANR is detected, you can enable a callback instead:

    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();
    

    This is very important when delivering your app in production. When in the hand of the final user, it's probably better not to crash after 5 seconds, but simply report the ANR to whatever reporting system you use. Maybe, after some more seconds, the app will "de-freeze".

Filtering reports

  • If you would like to have only your own threads to be reported in the ANRError, and not all threads (including system threads such as the FinalizerDaemon thread), you can set a prefix: only the threads whose name starts with this prefix will be reported.

    new ANRWatchDog().setReportThreadNamePrefix("APP:").start();
    

    Then, when you start a thread, don't forget to set its name to something that starts with this prefix (if you want it to be reported):

    public class MyAmazingThread extends Thread {
    
      @Override
      public void run() {
    
    setName("APP: Amazing!");
    
    /* ... do amazing things ... */
      
    }
     
    }
    
  • If you want to have only the main thread stack trace and not all the other threads (like in version 1.0), you can:

    new ANRWatchDog().setReportMainThreadOnly().start();
    

Watchdog thread

  • ANRWatchDog is a thread, so you can interrupt it at any time.

  • If you are programming with Android's multi process capability (like starting an activity in a new process), remember that you will need an ANRWatchDog thread per process.

Donate

ANR-Watchdog is free to use for both non-profit and commercial use and always will be.

If you wish to show some support or appreciation to my work, you are free to donate!

This would be (of course) greatly appreciated but is by no means necessary to receive help or support, which I'll be happy to provide for free :)

Resources

A single adapter implementation for any scrolling view or ViewGroup.

This library consolidates the differences between BaseAdapter, RecyclerView.Adapter, PagerAdapter, and binding to ViewGroup into one unified API.

A powerful debug module that is fully pluggable, extendable, and very useful. It enables you to create your own Critter that contain UI elements which enable you configure your application on the fly.

It also can inject a right-facing DrawerLayout into your activity on top of all other content, making it very unobtrusive and accessible from everywhere within your application.

This is a simple small class that renders a progress in form of an filling arc shape. A progress text can be rendered additionally. This text is rendered in the secondary color for the 'done' area of the arc.

This library is created for get post and information from a blog or a website created with WordPress.

A sticker view for android application.

Collection of useful classes to design MVC applications.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes