5 effective methods to prevent ANR in Android

3 min read
zen8labs ANR android
What are the possible problems from ANR for android users?

Normally when programming a mobile application, we often encounter apps crashing, which is when the current application cannot operate (force close). But there is another status that is less serious: Application Not Responding (ANR). Why is it less serious because your application can continue to be used normally after waiting? The question is how to minimize ANR errors? Let’s find out below.

What is ANR?

When the user interface flow of an Android app is blocked for too long, the “Application Not Responding” (ANR) error is triggered. If the application runs in the foreground, the system displays a dialog box to the user as shown in the figure. ANR occurs when some long operation takes place in the main thread. This is the event loop stream, and if it’s busy, Android can’t handle any more GUI events in the app.

zen8labs ANR android

How to prevent an ANR?

Stop doing heavy tasks on the main thread. Instead use worker threads such as Intent Service, Async Task Handler, or another Thread simply.

Detecting where ANRs happen is straightforward if it’s a permanent block (deadlock acquiring some locks for instance), but harder if it’s just a short-lived delay. First, re-evaluate your code and appearance for vulnerable spots and long-running operations. Error-causing examples may include usage:

  • Service.onCreate()/Service.onStartCommand()/Service.onBind() in seconds, thread sleeps, blocking operations from within the event thread and other use Context.startForegroundService() to start a new service on the foreground, but that service doesn’t call startForeground().
  • If BroadcastReceiver has not completed its execution within a certain period of time. If the app has any activity in the foreground, this timeout is 5 seconds.
  • If JobService does not return from JobService.onStartJob() or JobService.onStopJob() within a few seconds, or if a user-requested command starts and your application does not call JobService.setNotification() within a few seconds after JobService.onStartJob() is called. For apps targeting Android 13 and below, ANR errors are inactive and not reported to the app. For apps targeting Android 14 and above, ANR errors must be visible and reported to the app.

Resolutions

Use the following to diagnose and fix the problem:

  • You should make sure these all happen in separate threads. If nothing seems to matter, use Android Resource Analyzer and enable the thread view. This shows all the threads in your application like the trace you have. Reproduce the ANR and refresh the most thread at an equivalent time.
  • Android vitals can help improve your app’s performance by alerting you, via the Play Console, when your app is exhibiting excessive ANRs.
  • The older way might be to use DDMS and enable stream views.
  • You can use ADB to gather logs and have a look.
  • You can also add Crashlytics from Firebase to catch hold of ANRs when they occur in your app and get their frequency.

Conclusion

Encountering errors like crashes and Application Not Responding (ANR) issues is common in mobile application development. Understanding ANRs and implementing strategies to minimize them is essential for maintaining a positive user experience. Diagnostic tools such as Android Resource Analyzer, Android Vitals, and Crashlytics provide valuable insights into app performance and help pinpoint ANR issues. By using these tools and monitoring performance metrics, developers can create responsive and user-friendly mobile applications.

In conclusion, by adopting best practices, implementing effective strategies, and utilizing diagnostic tools, developers can minimize ANR errors and ensure their apps deliver a smooth user experience.

To gain more insightful information, check out our blog!

Tien Tran, Android Developer

Related posts

The need to make mock A.P.I responses has become increasingly crucial for efficient testing and development workflows. While various tools offer powerful capabilities for intercepting and modifying HTTP traffic, they often come with an expensive price tag. This is where mitmproxy steps in as a cost-effective and feature-rich alternative.
5 min read
This tutorial is to provide you with a deep understanding of the core components of RxSwift, focusing mainly on the implementation behind the scenes of RxSwift. After reading this post, it is hoped that you will have a clear understanding of what observables are, what subscriptions are, and how observables work.
7 min read
Communication between different software systems and services is essential in today's digital world. Developers constantly seek efficient and reliable ways to make these connections, and one technology that has gained significant traction in recent years is gRPC. In this article, we will explore what gRPC is, how it works, and why it has become a popular choice for building modern applications. 
3 min read