If you’ve ever encountered an Application Not Responding (ANR) error in your Android app, then you know how frustrating they can be to find and resolve. Meanwhile, in the time you spend searching for a solution, that ANR is having a very real impact on your app’s user experience and business.
Understanding the root cause of ANRs can be challenging, especially when the information provided by the Android framework isn’t as in-depth as some might believe. In this blog, we will share some key insights from our recent eBook on broadcast receivers and ANRs that the Android framework doesn’t provide.
What is a broadcast receiver?
A broadcast receiver is a component that enables the system to deliver events to the app outside of a regular user flow, allowing the app to respond to system-wide broadcast announcements. Messaging apps that hook into SMS events and perform functionality in the application are a key example.
What the Android framework doesn’t tell you about broadcast receivers and ANRs
The Android framework plainly states that ANRs are triggered when the main thread has been blocked for at least five seconds. While Android’s documentation makes it seem as if that rule applies to all components — including activities, services, and broadcast receivers — closer scrutiny reveals discrepancies for each.
As our engineers discovered while digging through Android source code, the reality of when each component triggers an ANR is more complex.
Take broadcast receivers, for example.
Did you know that the ANR threshold for a broadcast receiver is actually 10 seconds and not 5 seconds?
In other words, broadcast receivers trigger an ANR if they take longer than 10 seconds to process a message. However, there is an interesting exception to this: when the Android OS is booting, a lot of CPU work is underway so early broadcasts could create false positives within your monitoring platform.
Why you might not have noticed this if you’re using Google Play Console and Crashlytics
Many Android developers rely on Google Play Console and Firebase Crashlytics to monitor, identify, and solve ANR errors. While free to use, these tools have limitations that make it difficult to uncover the true causes of ANRs.
For example, it’s common for an Android component to starve the main thread to the point where an activity has no time to respond to input events. Such a scenario can trigger ANRs within an activity which indicate a problem in a different component than the one which caused the ANR.
When using Google Play Console and Firebase Crashlytics to monitor such errors, developers will receive a stack trace from Google and not much further context. This can make it impossible to determine which component is the true culprit, as it depends on which component is running at the time the stack trace was generated.
For example, in the scenario depicted in the image above, Google Play Console and Firebase Crashlytics would point to Activity.loadimage() as the culprit of the ANR, when in reality the root cause is the broadcast receiver.
This can result in highly misleading ANR error reports when viewed in isolation.
Broadcast receivers aren’t the only component that breaks the “5-second ANR rule”
Broadcast receivers aren’t the only Android component that doesn’t adhere to the 5-second ANR trigger rule. In fact, our team’s research has identified at least two other Android components that don’t conform to the triggers described within Android’s own documentation.
Our latest eBook, “Solving ANRs 101: Diving into the Android framework,” takes an in-depth look at the relationship between ANRs and the Android framework and will help you learn:
- The true ANR triggers for Android components, including activities, services, and broadcast receivers.
- The limitations of monitoring ANRs with just the Google Play Console and Crashlytics.
- How session-based monitoring can accelerate your ability to identify and eliminate ANRs.
Download our eBook now and go to school on ANRs.
If you’d like to see how complete mobile visibility can revolutionize how you deliver incredible mobile experiences, get started with a free trial of Embrace today.
Get started today with 1 million free user sessions.
Get started free