Giter Club home page Giter Club logo

anr-spy's Introduction

Android ANR Spy

Android ANR Spy is the most simplest library that helps android developers to detect ANRs.

What is Android ANR (Application Not Responding)

when a developer do most heavy jobs on UI thread (more than 5 seconds usually) and UI thread still receieve more request/events for doing a task then Android system raises ANR message. This is extremely bad effect on your app and may lead to the failure of your business.

Android ANR does matter

Google recommends/suggests your app on play store. If your app raises too many ANRs then your app will be ranked down

Android ANR Durations

  1. Normal on UI Thread in any activity = 5 secs
  2. BroadCast = 10 sec
  3. Service = 20 sec

Note: Sample app is included in the project. Just clone the repo

Android ANR Spy Library

Implement:

implementation("io.github.farimarwat:anrspy:1.3")

Usage

Step 1: Create a Callback Object

(before onCreate() method)

 //Anr Callback
    private var mCallback = object : ANRSpyListener {
        override fun onWait(ms: Long) {
		//Total blocking time of main thread. 
		//Can be used for doing any action e.g. if blocked time is more than 5 seconds then 
		//restart the app to avoid raising ANR message because it will lead to down rank your app.
        }

        override fun onAnrStackTrace(stackstrace: Array<StackTraceElement>) {
		//To  investigate ANR via stackstrace if occured.
		//This method is deprecated and will  be removed in future
        }

        override fun onReportAvailable(methodList: List<MethodModel>) {
		//Get instant report about annotated methods if touches main thread more than target time
        }
        override fun onAnrDetected(
            details: String,
            stackTrace: Array<StackTraceElement>,
            packageMethods: List<String>?
        ) {
           //details: Short description about the detected anr
	   //stacktrace: Stacktrace of the anr
	   //packageMethod: methods hierarchy(bottom up) that causes anr (only if method is inside current app package name)
        }
    }

Step 2: Create Instance

(before onCreate() method)

val anrSpyAgent = ANRSpyAgent.Builder(this)
            .setTimeOut(5000)
            .setSpyListener(mCallback)
            .setThrowException(false)
            .enableReportAnnotatedMethods(true)
            .setFirebaseInstance(firebaseinstance)
            .build()

Step 3: Start Tracing

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
	.....
	......
	anrSpyAgent.start()
	
}

Note:Step 1, 2 and 3 can be done only once on either mainactivity or in appclass.There is not need to create seperate instances for each activity, viewmodel etc. This is thread based process and will be available in whole app.

Builder Methods

setTimeOut(5000)

Time limit to detect ANR

setSpyListener()

Sets ANRSpyListener/callback methods

setThrowException(true)

Convert possible ANR to crash to figure out the line where ANR may be possible and close the app if true. Default is false

enableReportAnnotatedMethods(true)

This will generate report for annotated methods that you want to trace any where in the app. If the specified methods touches main thread for more than target time (default 5 secs), it will trigger onReportAvailable method of the callback to get details about the function e.g. Thread Name, Elapsed Time on main thread and function

Note: If the annotated method is not running on main thread then there will be no report generated.

setFirebaseInstance(firebaseinstance)

To get logs similar to the mention above on firebase. Just set the instance for firebase analytics and all events will be collected as usuall to other events. All the events will be prefixed with: ANR_SPY_ to differenciate from other events on firebase

Annotations

In case if any one want to trace a specific method to trace then there are two types of annotations available:

1. @TraceClass(traceAllMethods = false) This annotatiion is applied to a class and takes one parameter. If the paramater traceAllMethods is set to true then all methods of the class will be traced on main thread. Default is true

Note: If traceAllMethods is set to false and there is no specific annotated method then there will be no report generated. To trace all methods in MainActivity:

Example:

@TraceClass(traceAllMethods = true)
class MainActivity : AppCompatActivity() {
	.......
}

2. @TraceMethod To trace a specific method on main thread for ANR

Example:

@TraceMethod
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
}

Note: If the method is not running on main thread then there will be no report generated

Change Log

version 1.3

  1. Get details of methods which is related to main app package that causes anrs via "onAnrDetected" extra paramater
  2. A bug fixed (classnotfoundexception)

version 1.2 (beta)

  1. Annotation added to trace a specific method for ANR
  2. Store annotated methods report in firebase analytics

version 1.0 Initial release

Support Me

If you want to donate then you are welcome to buy me a cup of tea via PATREON because this encourages me to give you more free stuff and continue to maintain this library

Buy Now!

anr-spy's People

Contributors

farimarwat avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.