<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nickolay Chameyev on Medium]]></title>
        <description><![CDATA[Stories by Nickolay Chameyev on Medium]]></description>
        <link>https://medium.com/@lukaville?source=rss-a7bc3980fd44------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*r2oWfnwjBM8ndcbt.</url>
            <title>Stories by Nickolay Chameyev on Medium</title>
            <link>https://medium.com/@lukaville?source=rss-a7bc3980fd44------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 06:49:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lukaville/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How we achieved a 6x reduction of ANRs. Part 2: Fixing ANRs]]></title>
            <link>https://medium.com/bumble-tech/how-we-achieved-a-6x-reduction-of-anrs-part-2-fixing-anrs-24fedf9a973f?source=rss-a7bc3980fd44------2</link>
            <guid isPermaLink="false">https://medium.com/p/24fedf9a973f</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[anr]]></category>
            <dc:creator><![CDATA[Nickolay Chameyev]]></dc:creator>
            <pubDate>Fri, 11 Dec 2020 14:25:09 GMT</pubDate>
            <atom:updated>2020-12-11T14:25:46.492Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wmfTTF9c-ME5kDX_UbHgdg.png" /></figure><h3>How we achieved a 6x reduction of ANRs - Part 2: Fixing ANRs</h3><p>In the <a href="https://medium.com/bumble-tech/how-we-achieved-a-6x-reduction-of-anrs-part-1-collecting-data-7c473ceb1c83?source=friends_link&amp;sk=263921f4cf79940c04c41cac2118074a">first part</a>, we discussed what ANR is and what are the ways of tracking it. In this article, you will find information on what problems we found in our application, how we fixed them, and the results we achieved.</p><h3>Application Startup</h3><p>The first thing to do if you want to reduce the ANR rate is to find the reasons for the errors. The most straightforward way to do this is to try to analyse top ANR groups in Google Play. When we checked the console, it looked like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JfO40EO-v4owQ1CN" /></figure><p>Overall almost each crash group had a title “<strong>Broadcast of Intent { act=com.google.android.c2dm.intent.RECEIVE } …</strong>”. Checking the actual stack traces that we collected using our scraper we noticed that about 60% of reports were in Application.onCreate method at the moment when ANR was triggered.</p><p>Application.onCreate method is part of the app startup critical path and called whenever the application process is initiated. This gave us an idea to investigate how application startup time may affect ANRs.</p><p>The easiest way to check it is simply to add an artificial delay to Application.onCreate and check different scenarios. Here is something interesting we discovered:</p><ul><li>When a user manually triggers an app launch using a launcher app, ANR won’t be reported for the blocking of the main thread in Application.onCreate even if it is blocked for <strong>several minutes</strong></li><li>When an app is launched using a broadcast receiver then ANR won’t be reported for the blocking of the main thread for less than 10 seconds. The timing is not very strict and has some wiggle room but it is much stricter than the previous case</li></ul><p>There is an important note for the second case: by default, if your application is in the background state Android <strong>won’t show any dialogues</strong> and this ANR will be reported silently. There is a way to enable displaying such dialogues using the “Enable background ANR dialogues” option in the developer menu. Practically speaking, it means that your users will most likely not notice anything and it will not significantly affect your application.</p><p>Given this information, we came to the conclusion that most likely the primary cause of our ANRs is that we are doing too much work in Application.onCreate method and when we use Firebase Cloud Messaging broadcast we sometimes exceed the 10 seconds time limit which causes background ANR.</p><p>We checked our internal analytics where we record the time elapsing between creating Application class through to the end of Application.onCreate method: we call it ‘cold app startup time’. This part of the application startup process is the largest for our application and includes all content providers’ initialisation and onCreate method.</p><p>Also, we split this data into two groups: cold app startup background and cold app startup foreground. We used a naive approach for the background launch detection: whether the screen is on or off. It may not be precise but it gives us an overall picture. Here is what we got:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OfIgzhqD0P_BIkdA" /><figcaption><em>Background cold app startup time</em></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0ffQp9g_CQjcjj-K" /><figcaption><em>Foreground cold app startup time</em></figcaption></figure><p>According to our analytics on average foreground launch took about 2.2 seconds, and background launch 5 seconds. We had about 3% of background launches longer than 10 seconds which suggests that these launches could be the cause of our ANRs. To confirm or deny our theory we decided to try to reduce our application startup time.</p><h4>Reducing Application Startup Time</h4><p>The easiest way to identify the potential areas to improve on the critical path of app startup is to capture a CPU trace dump. There are great tools available in Android Studio that can help you do this.</p><p>There are buttons in Android Studio profiler to run an app with profiling or to start and stop it manually but there are also special system <a href="https://developer.android.com/reference/android/os/Debug">methods</a> that can start and stop profiling from the code. These methods may help you to get consistent CPU dumps reliably:</p><ul><li>startMethodTracingSampling</li><li>startMethodTracing</li><li>stopMethodTracing</li></ul><p>Application startup can be captured by starting tracing in Application class static initialiser block and finishing at the end of Application.onCreate or onResume of the first activity. It may look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2cd2d1d78fd5345bb83e38885f3f88ce/href">https://medium.com/media/2cd2d1d78fd5345bb83e38885f3f88ce/href</a></iframe><p>After downloading the trace file, opening it in Android Studio you’ll see something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*B1CPzb1HjFGXk61W" /></figure><p>It is important to bear in mind the following when analysing the results:</p><ul><li><strong>Difference between sample-based tracing and method-based tracing. </strong>Method-based tracing is more prone to skewing the captured time taken for executing specific methods, while sample-based approach is more precise but may lose some method calls. You cannot use absolute time values from both methods, but sample-based tracing gives more usable data to perform a relative comparison</li><li><strong>Difference between production and debug app. </strong>If you want to capture CPU trace in the environment as close as possible to production you need to disable all debug tools such as leak canary, or compile the app using release build type.</li></ul><p>After checking the CPU trace, we found several places that had taken a relatively long time to complete. We didn’t want to rewrite the whole application to optimise the startup time (although this may be sometimes <a href="https://eng.snap.com/dont-rewrite-your-app-unless-you-have-to/">necessary</a> 🙂) so we tried to get the most results with the least amount of effort. We used different techniques and here are a few of them that you can apply for your application:</p><p><strong>Correctly scope your components</strong></p><p>One of the easiest ways to optimise your application startup time is to go for lazy initialisation of all your components. It might sometimes be tempting to create some data structures or classes in the application scope, so they are available during the whole application lifetime but this is best avoided, if possible. Additionally, correct scoping may improve not only the startup time but also memory usage because you won’t be holding references to these objects for the whole application lifetime.</p><p><strong>Background and delayed initialization</strong></p><p>If you still need to initialise something when your application starts, ask yourself if you can postpone it for at least a few seconds. If the answer is yes, then it makes sense to try to run this code on a background thread. If it is not possible, then even just delaying initialisation for a few seconds may help. This is because, by distributing the tasks over time, you are reducing the chance of blocking the main thread.</p><p><strong>Third-party content providers</strong></p><p>Some third-party libraries can invoke initialisation code using <a href="https://developer.android.com/guide/topics/providers/content-providers">content providers</a>. It is always important to check what you have in the merged AndroidManifest.xml as, by default, Android build toolchain will merge all content providers from all third-party libraries, and it may affect application startup time.</p><p>If you don’t need automatic initialisation of content providers for certain libraries, you can alter the manifest by adding the following entry to your manifest:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/286c8964a78a0b48c06074a7f08b4756/href">https://medium.com/media/286c8964a78a0b48c06074a7f08b4756/href</a></iframe><p>This can be useful if you want to manually control the initialisation process: for example, you can initialise a library by invoking the same code that was in the content provider only when you actually want to use the provided functionality. But be careful with this approach and perform thorough testing afterwards as the library may have been designed for Application-wide initialisation.</p><p>When we released an update with these optimisations, we reduced the 95th percentile of cold application startup time by about 50% (from ~10 seconds to ~5 seconds):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zd6zszeio99-TgRV" /></figure><p>And what about ANRs count? Here is what we’ve got from the Google Play console:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*waD17bnU2hgFcC3NTxPG8A.png" /><figcaption>Absolute ANRs count graph in the Google Play console</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*ZPg4CtKZdAnkdSSPl-H5Nw.png" /><figcaption>Source: picture from The Office series</figcaption></figure><p>OK, we found out that app startup directly affects the ANR rate and we significantly reduced the ANR count by one third. But it still turned out to be above the threshold so we had to continue our search.</p><h4>SharedPreferences and apply() method</h4><p>Another thing that we noticed during analysing of the ANR stack traces in Google Play and examining our internal reports, there were these strange groups:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1zr7Wz4WSPUtlOC6" /></figure><p>These showed there are many cases when the main thread is blocked on something related to shared preferences. Generally, it is not recommended to do disk writes on the main thread because disk IO can have unexpected delays and cause UI freezes.</p><p>We checked our codebase and we found no usages of commit method (which performs blocking shared preferences writing to disk). All our modifications were done using the apply method. So, how is it possible that we are experiencing such issues when we are using non-blocking API?</p><p>Going deeper into Android source code will help us see what is happening. The default implementation of shared preferences is located in <strong>SharedPreferencesImpl.java</strong>. During the editing of shared preferences using the Editor interface, all modifications are saved in a temporary hashmap which is then applied to the main in-memory cache on commit or apply invocation. When it applies the map to the in-memory cache it also calculates which keys were changed to use it further when we need to write these changes to disk and notify shared preferences listeners. This information is stored in <em>MemoryCommitResult</em>.</p><p>If we check the body of <em>apply() </em>method we can see that it schedules background disk write using <em>enqueueDiskWrite()</em>, nothing wrong with that. Here is the simplified implementation of <em>apply</em> method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1380521921f64bf8d645f8ef6361331c/href">https://medium.com/media/1380521921f64bf8d645f8ef6361331c/href</a></iframe><p>Taking a closer look we see that it first creates a Runnable that waits for writes in a synchronous fashion. This runnable is added to QueuedWork. When we check JavaDoc for these classes we see the following:</p><blockquote>Internal utility class to keep track of process-global work that’s outstanding and hasn’t been finished yet.</blockquote><blockquote>This was created for writing SharedPreference edits out asynchronously so we’d have a mechanism to wait for the writes in Activity.onPause and similar places, but we may use this mechanism for other things in the future.</blockquote><p>This class holds all pending asynchronous operations in a list with the ability to execute them in a synchronous way on several events such as Activity.onStop, Service.onStartCommand and Service.onDestroy. This was most likely done to reduce the probability of losing data on unexpected process shutdowns.</p><p>We saw that if you simply use standard Android components it may sometimes lead to running all pending shared preferences disk writes on the main thread. If we do any of these operations right after calling the apply method then it effectively becomes a synchronous commit method.</p><p>We suspect that this was done to reduce potential data loss when the application gets killed by the system. But how likely is such a situation? We usually use shared preferences when the application is in the foreground state, and it is highly unlikely that Android will kill the foreground application. Moreover, we frequently use shared preferences to cache some values that can be easily restored from the server-side, so losing it is no big deal.</p><p>To check how shared preferences affect the ANR rate and whether this synchronous apply logic is useful, we decided to implement an A/B test that disables it. To do this, we replaced all creations of shared preferences with our factory function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9f59579d7f1c8970b88bcdd3c7fa6ba/href">https://medium.com/media/d9f59579d7f1c8970b88bcdd3c7fa6ba/href</a></iframe><p>Now we have control over the implementation of shared preferences in our application and we can create an alternative one. We created a simple class that delegates everything to the original implementation except the apply method: here we call the commit method instead and schedule it to be executed on a background thread. The implementation is very similar to a library that provides an alternative to shared preferences: <a href="https://github.com/yandextaxitech/binaryprefs">binary prefs</a> with the exception that we don’t change the serialisation/deserialisation mechanism to ease backward migration in case of problems.</p><p>So, having introduced a new implementation we can use it under A/B test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c9253ceaca514c2cb1e32082687d091/href">https://medium.com/media/5c9253ceaca514c2cb1e32082687d091/href</a></iframe><p>Next, we started slowly rolling out the A/B test, keeping an eye on the metrics. We have very good coverage of product-related metrics that include any that might be affected if there are problems with the new shared preferences implementation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qfXsRjsQhR2h-Vp8" /></figure><p>As a result, we found no issues with the new implementation and we achieved about a 4% reduction of total ANRs compared to the control group. That’s not bad but we still were above the required threshold.</p><h4>Push notifications handling</h4><p>Still not being where we wanted to be, we had to figure out a way to reduce the ANR rate even more.</p><p>We had already found out that there is an almost linear correlation between app startup time and ANR rate which most likely due to the fact that the majority of ANRs are happening during handling broadcast receivers on Application.onCreate stage. Unfortunately, we had exhausted all the easiest ways of reducing the startup time and everything that remained would require major refactorings and a lot of work.</p><p>Another thing that we had noticed when analysing our analytics is that most of our process launches were coming from handling push notifications broadcasts. Based on this we’ve got an idea: maybe we could perform all notifications handling in a separate process that doesn’t need all the data structures and services to be initialised on application startup?</p><p>Just for some context, by default on Android, your application is running within a single process. When you click on the app icon Android will create a process for your application. When your application receives a push notification broadcast it will start a new process if it has not started yet. Each time when Android starts an application’s process it invokes Application.onCreate method:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*s008vJY_s_f5SW3z" /></figure><p>But there are ways to run some parts of the application in separate processes. In such case, Application class will be instantiated for each process instance independently and there will be no shared memory between all processes. It can help in our case because we can move all push-related operations to a separate process and remove almost everything from the Application.onCreate method in the push process. This way, we both significantly reduce push broadcast handling time and lower the probability of getting an ANR:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4zMyxEcjeqw9y1Cg" /></figure><p>You can control which process your component will be launched by using AndroidManifest.xml. For example, to run a broadcast receiver in a non-default process we need to add a name for the process using the “android:process” attribute in the “receiver” tag. But how can we do this for external libraries such as Firebase Cloud Messaging?</p><p>There are special tags that control the manifest merging process. We can patch the original FCM broadcast manifest declaration using <em>tools:node=”replace”</em> attribute. Apart from FCM receiver, there is a FirebaseMessagingService that is also responsible for handling push notification and we want to run this in a separate process too. Overall, we need to add the following manifest entries:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9651e602844566e98cb9b1de3fecda6/href">https://medium.com/media/a9651e602844566e98cb9b1de3fecda6/href</a></iframe><p>Now, whenever Google Play Services sends a broadcast with a new cloud message, we will be able to skip usual initialisations in Application.onCreate by checking if are we in the main process or not:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a043918de3fddcef70b2c0eac4454e0/href">https://medium.com/media/0a043918de3fddcef70b2c0eac4454e0/href</a></iframe><p>There are many ways of checking this. You can check one of the implementations <a href="https://github.com/int02h/primaree/blob/7c481566136dec00bf762a416101b6f1e3c250a5/library/src/main/java/com/dpforge/primaree/ApplicationExtensions.kt#L19">here</a>.</p><p>Most likely if you don’t do anything special with pushes this will work out of the box: your code responsible for displaying notifications will be executed in a separate process and if you are passing PendingIntent for activities they will be launched in the default process.</p><p>After implementing this we immediately noticed that now we display notifications significantly more quickly than before:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FyQtvzY7q_bA%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DyQtvzY7q_bA&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FyQtvzY7q_bA%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/3aa26fcde420b705cde57a7c6e157746/href">https://medium.com/media/3aa26fcde420b705cde57a7c6e157746/href</a></iframe><p>This gave us hope that it should improve our situation with the ANR rate as well. We released a new update with these changes and carefully rolled it out while keeping an eye on the main metrics.</p><p>After about a week most of our users had updated to the new version and here is what we then had:</p><ul><li>Badoo ANR rate dropped from 0.80% to 0.41% and eventually dropped below that of our peers, to 0.28%</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qGdBrVRxwUIUgTpT" /></figure><ul><li>Absolute ANRs count per day dropped by more than 2 times</li></ul><p>Although these changes had a significant impact on ANR rate there are a few things that you need to consider before implementing them in your app:</p><ul><li>Modifying FCM client library behaviour by patching the manifest may not be intended by the developers of the library. There are no official guides or documentation on how to do this and potentially it can be broken at any point in the future, so it requires thorough testing after each client library update.</li><li>Updating FCM libraries now requires extra caution because we must ensure that our patched XML manifest entries are aligned with the original ones.</li><li>Launching an extra process uses additional memory and CPU footprint.</li></ul><p>Given this, I would only recommend using it only as a last resort or as a temporary fix to buy some time. If you find it possible to reduce the application startup’s critical path then it is better to try to focus on it first as it will not only help reduce the ANR rate but also the cold app startup time, both clearly benefit to users.</p><h3>Overall Results</h3><p>After all these changes, we had managed to reduce our ANR rate and absolute errors count by six times:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ilV_wiSgkiqanNkM" /></figure><p>I hope you find our experience useful in helping you to reduce ANR rate and improve your application quality.</p><p>Have you encountered any interesting ANR bugs? Do share your experience in the comments below :)</p><h3>Useful Links</h3><ol><li><a href="https://developer.android.com/reference/android/os/Debug">https://developer.android.com/reference/android/os/Debug</a></li><li><a href="https://eng.snap.com/dont-rewrite-your-app-unless-you-have-to/">https://eng.snap.com/dont-rewrite-your-app-unless-you-have-to/</a></li><li><a href="https://programmer.ink/think/count-the-slots-in-shared-preferences.html">https://programmer.ink/think/count-the-slots-in-shared-preferences.html</a></li><li><a href="https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/app/SharedPreferencesImpl.java">https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/app/SharedPreferencesImpl.java</a></li><li><a href="https://github.com/int02h/primaree">https://github.com/int02h/primaree</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=24fedf9a973f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bumble-tech/how-we-achieved-a-6x-reduction-of-anrs-part-2-fixing-anrs-24fedf9a973f">How we achieved a 6x reduction of ANRs. Part 2: Fixing ANRs</a> was originally published in <a href="https://medium.com/bumble-tech">Bumble Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How we achieved a 6x reduction of ANRs. Part 1: Collecting Data]]></title>
            <link>https://medium.com/bumble-tech/how-we-achieved-a-6x-reduction-of-anrs-part-1-collecting-data-7c473ceb1c83?source=rss-a7bc3980fd44------2</link>
            <guid isPermaLink="false">https://medium.com/p/7c473ceb1c83</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[anr]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Nickolay Chameyev]]></dc:creator>
            <pubDate>Fri, 11 Dec 2020 14:24:29 GMT</pubDate>
            <atom:updated>2020-12-11T14:26:24.393Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*96oTTiVCyKjTleEYe9OMhA.png" /></figure><h3>How we achieved a 6x reduction of ANRs - Part 1: Collecting Data</h3><p>One of the worst things that can happen to your app’s responsiveness is an Application Not Responding (ANR) dialogue. A high ANR rate may affect user experience and, potentially, Google Play search positions and featuring.</p><p>At the beginning of the year, we had ANR rate above the Google Play threshold for the Badoo application — operated by its parent company <a href="https://thebeehive.bumble.com/careers">Bumble</a>. So, we set up an ad-hoc team to work on this problem and spent a few months trying different approaches to fix it. By the end of the period, we managed to reduce the number of ANRs by more than <strong>six times</strong>.</p><p>In this blog post series, I will share our journey of reducing this type of error and describe exactly what were the most effective approaches and how you can apply them to your application. In Part 1 of this series, we will discuss what ANR is and what are the best ways of tracking them. If you already know about this part, feel free to jump straight to <a href="https://medium.com/bumble-tech/how-we-achieved-a-6x-reduction-of-anrs-part-2-fixing-anrs-24fedf9a973f?source=friends_link&amp;sk=949a47e899cf521d1feaf3e69a07bb81">Part 2</a> where I explain our approaches to fix ANRs.</p><h3>What is an ANR error?</h3><p>Usually, any software that has a visual interface performs all UI-related operations and rendering in a separate “UI” thread. Android is no exception, with the main thread of the app running a loop responsible for all UI operations:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*u8zqU370bcpkXlKk" /></figure><p>When using this loop, it is crucial not to execute any long-running operations, as they directly affect the responsiveness of your app. If you are doing too much work on the main thread, you may decrease the frame rate or even hang the application’s user interface, like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*o8dHou7j7osSbf9R" /></figure><p>To provide a clear indication that the app is stuck, Android has introduced a concept called ANR. This is what the official <a href="https://developer.android.com/training/articles/perf-anr">documentation</a> says on this:</p><blockquote>When the UI thread of an Android app is blocked for too long, an Application Not Responding (ANR) error is triggered.</blockquote><blockquote>An ANR will be triggered for your app when one of the following conditions occur:</blockquote><blockquote>1) While your activity is in the foreground, your app has not responded to an input event or BroadcastReceiver, such as key press or screen touch events, within 5 seconds</blockquote><blockquote>2) While you do not have an activity in the foreground, your BroadcastReceiver hasn’t finished executing within a considerable amount of time</blockquote><p>If the ANR is triggered when your application has an activity in the foreground, Android shows a dialogue suggesting either to close the app or to wait.</p><p>You can force ANR by simply putting <em>Thread.sleep()</em> invocation in a UI event handler, such as button click. Then, if you click on the button you will see a dialogue like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*0DL7dQFweZ97bTJ4SN2w9A.png" /></figure><p>Having ANRs not only affects your application’s user experience but also, according to Google <a href="https://developer.android.com/distribute/best-practices/develop/android-vitals">documentation</a>, can affect search ranking and Google Play promotions.</p><p>To prevent ANRs we need to avoid performing any long-running operations on the main thread. This sounds simple enough but sometimes it can be tricky to identify the underlying problem. Therefore, it is vital to have good ANR reporting to ease this process.</p><p>Let’s see what the available options and tools are that can help with the investigation of these issues.</p><h3>ANR Tracking</h3><h4>Analysing ANRs locally</h4><p>If you are able to reproduce an ANR locally, then that’s great news because it should be fairly easy to pinpoint the source of the problem and there are many ways to dig into it.</p><p>A good start is to check the thread dump. When ANR is occurring, Android captures a thread dump which can help analyse the cause. Thread dumps are usually stored in the <em>/data/anr/</em> directory. The exact name of the file can be found in the logs. Just check the logcat right after when the ANR is reported.</p><p>The thread dump contains a list of stack traces: for each thread, you will see which line was being executed at the time — basically representing the state of the application at the precise moment it occurred.</p><p>In most cases, you will find the reason for the ANR just by looking at the main thread stack trace. In other cases, check out this great <a href="https://developer.android.com/topic/performance/vitals/anr">documentation page</a> from Google that describes how to investigate ANRs locally using different tools.</p><h4>Google Play Tracking</h4><p>Google Play automatically sends ANR reports if users opted in for the crash report collection. The Google Play console has several metrics and tools for analysing ANRs.</p><p>Firstly, it provides a way to check aggregated graphs for both absolute ANR count per day and the ANR rate. The ANR rate is defined as a ratio between daily sessions affected by at least one ANR to total daily sessions. This metric also has a threshold of 0.47%; exceed this and it will be classified as “bad behaviour”, which can negatively impact your Google Play listing.</p><p>Secondly, there is an option to check individual ANR reports grouped by similarity. In the Android Vitals section, you can check top ANR groups. This can work as the main tool for identifying the most popular reasons for ANRs in your application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Fh3WeFAzGtdh5uAu" /></figure><p>Occasionally, you may notice that the capabilities of the Google Play console are insufficient for several reasons. There are several issues with it. For example, there is no way to attach additional logging information to individual reports. Another problem is that it is impossible to tune the grouping logic. It sometimes groups ANRs with different reasons in one group, and groups with the same reason in different groups.</p><p>These issues make it difficult to see clearly what the main problems are and how to fix them. So how can we improve it?</p><h4>Downloading data from Google Play</h4><p>To solve one of the problems with grouping logic you can try downloading ANR reports from Google Play for further analysis. Previously, there was a way to download raw detailed reports using Google Cloud Storage, but a few years ago Google removed this option:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6BudWkMZ-3f5uwHa" /></figure><p>While it is still possible to view individual reports in the console, how can you export thousands of reports without doing tons of repetitive work?</p><p>There are many ways of scraping information from websites. The cleanest way is to download it using APIs but in our case, Google does not provide any official APIs to get this data. In this case, you can emulate user behaviour by automatically clicking on links and buttons in a browser and saving the displayed text.</p><p>A web scraper can be implemented using a commonly used tool called <a href="https://www.selenium.dev/">Selenium</a> which provides simple APIs to interact with web pages. It was initially designed to create automated tests for web applications and is available in various languages, including Java/Kotlin.</p><p>Using this scraper, we downloaded raw ANR reports for one release and were able to perform analysis not possible with Google Play Console’s built-in tools.</p><p>For example, just by searching “Application.onCreate” in the reports, we found out that approximately <strong>60%</strong> of ANR reports were occurring while executing Application.onCreate method.</p><h4>Custom reporting</h4><p>Another way to collect more data and perform more advanced analysis is to implement custom ANR reporting. In the past, we had been experiencing similar problems with crash reporting tools. Therefore, we created our own internal crash analytics tool called “Gelato”.</p><p>It provides similar functionality as other crash reporting tools, such as Firebase Crashlytics or App Center, but allows us to fully control the data that we store, change grouping logic, and perform complex filtering:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SXH30KDrsgDtn2kb" /><figcaption><em>This illustration is not representative of true Bumble data and is for example only</em></figcaption></figure><p>We decided to track ANRs to Gelato as well, in the hope that it would provide additional insight. To do this we needed to know when the application gets an ANR.</p><p>Whilst there is a new <a href="https://developer.android.com/reference/kotlin/android/app/ActivityManager#gethistoricalprocessexitreasons">API</a> in Android 11 that provides information on the latest process exit reasons, but the majority of our users are still using lower Android versions, so this meant we had to find something else.</p><p>We found a simple but effective idea that is commonly used to track main thread freezes. We could run a “watchdog” thread that periodically tries to run a task on the main thread. If the task fails to complete within the specified timeout, then we could simply dump the current threads state and report it to our crash reporting tool:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mrYc_DYGrR8Ugouh" /></figure><p>There is a great <a href="https://github.com/SalomonBrys/ANR-WatchDog">library</a> that implements this logic and we used it to add ANR reporting to our internal crash reporting tool. It allowed us to perform more in-depth analysis and facilitated better integration with our infrastructure. For example, now we are able to compare the main thread freezes between different A/B test variants.</p><p>Here is an example of a report in our system:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pwv3QzHpcey7KMK5" /><figcaption><em>This illustration is not representative of true Bumble data and is for example only</em></figcaption></figure><p>One useful tip is to collect and send recent analytics events with the report. Sometimes it can give literal steps to reproduce the problem.</p><p>If you don’t have plans to introduce in-house crash reporting solutions, you can try other third-party tools. For example, you can track ANRs as custom crashes to AppCenter or Firebase Crashlytics.</p><p>It is important to know that these reports cannot be considered as 1:1 alternatives to Google Play ANR reports. Android has slightly different rules for detecting ANRs, as we mentioned before, but it can give an overall picture of the main problems. It is still highly likely that if there are many reports of main thread freezes in some part of your app, then it contains ANRs.</p><h3>Outro</h3><p>So, we’ve discussed what ANR is and how we can track it. <a href="https://medium.com/bumble-tech/how-we-achieved-a-6x-reduction-of-anrs-part-2-fixing-anrs-24fedf9a973f?source=friends_link&amp;sk=949a47e899cf521d1feaf3e69a07bb81">In Part 2</a> of this blog post series, I will share our approaches to reducing the ANR rate and provide some of our own results.</p><h3>Useful Links</h3><ol><li><a href="https://developer.android.com/training/articles/perf-anr">https://developer.android.com/training/articles/perf-anr</a></li><li><a href="https://developer.android.com/distribute/best-practices/develop/android-vitals">https://developer.android.com/distribute/best-practices/develop/android-vitals</a></li><li><a href="https://developer.android.com/topic/performance/vitals/anr">https://developer.android.com/topic/performance/vitals/anr</a></li><li><a href="https://developer.android.com/reference/kotlin/android/app/ActivityManager#gethistoricalprocessexitreasons">https://developer.android.com/reference/kotlin/android/app/ActivityManager#gethistoricalprocessexitreasons</a></li><li><a href="https://www.selenium.dev/">https://www.selenium.dev/</a></li><li><a href="https://github.com/SalomonBrys/ANR-WatchDog">https://github.com/SalomonBrys/ANR-WatchDog</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7c473ceb1c83" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bumble-tech/how-we-achieved-a-6x-reduction-of-anrs-part-1-collecting-data-7c473ceb1c83">How we achieved a 6x reduction of ANRs. Part 1: Collecting Data</a> was originally published in <a href="https://medium.com/bumble-tech">Bumble Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>