Reducing ANRs with the Latest AdMob SDK Optimisations

Improving Ad Initialisation and App Performance

Ryan W
3 min readOct 7, 2024
This image was created using an AI image creation program.

Google AdMob recently reached out to developers about the importance of managing Application Not Responding (ANR) issues to maintain app stability and Google Play ranking.

ANRs occur when the main UI thread is blocked, causing the app to become unresponsive to user input. This can significantly affect user experience and app discoverability under the current Google Play policies.

AdMob has introduced a new beta configuration in the recent AdMob Mobile Ads SDK version to address this. This update aims to offload ad initialisation and loading tasks from the main UI thread, potentially reducing the risk of ANRs.

Understanding the New Configuration

With the latest Google Mobile Ads SDK (version 21.0.0 and higher), developers now have the option to use two new flags in their AndroidManifest.xml file:

  • OPTIMIZE_INITIALIZATION: Offloads ad SDK initialisation tasks from the UI thread. This flag should be enabled if MobileAds.initialize() cannot be moved to a background thread.
  • OPTIMIZE_AD_LOADING: Moves ad load calls off the UI thread, reducing the chances of ANRs during ad load operations.

Both flags are designed to move time-consuming operations away from the main UI thread to enhance app responsiveness and reduce the likelihood of ANRs.

Optimal Implementation of MobileAds.initialize()

As stated in Admob’s Get Started guide, the recommended practice is to call MobileAds.initialize() on a background thread, for example, using Kotlin Coroutines. There is no need to enable the OPTIMIZE_INITIALIZATION flag if this is already being handled.

However, AdMob acknowledges that not all developers may have implemented the initialisation this way. If MobileAds.initialize() is currently being called on the main UI thread in our app, enabling the OPTIMIZE_INITIALIZATION flag will offload certain initialisation tasks to the background thread. This serves as a fallback solution for cases where changing the initialisation method is not feasible.

Implementing the New Configuration

The process of enabling these new flags is straightforward:

<manifest>
<application>

<meta-data
android:name="com.google.android.gms.ads.flag.OPTIMIZE_INITIALIZATION"
android:value="true"/>

<meta-data
android:name="com.google.android.gms.ads.flag.OPTIMIZE_AD_LOADING"
android:value="true"/>

</application>
</manifest>

💡 Both the OPTIMIZE_INITIALIZATION and OPTIMIZE_AD_LOADING flags are set to false by default.

This simple change can improve app responsiveness during ad initialisation or loading.

Why This Update Matters

ANRs are often caused by blocking operations on the main UI thread. While many factors can contribute to ANRs, ad initialisation and loading have been identified as common culprits, particularly when they occur on the UI thread.

By introducing these new flags, AdMob is providing developers a way to mitigate ANRs without restructuring their code. However, the preferred approach remains to call MobileAds.initialize() on a background thread wherever possible.

Conclusion

While AdMob’s new optimisations offer a straightforward solution to reduce the potential for ANRs, developers should aim to implement MobileAds.initialize() on a background thread as the best practice. This update provides additional protection for those who may not have configured their apps in this manner yet.

For more detailed instructions, refer to the AdMob Mobile Ads SDK documentation.

--

--

Ryan W
Ryan W

Written by Ryan W

Modern Android Development | Jetpack Compose | Kotlin Multiplatform 📱Creating Android Solutions From Concept to Product | Building Apps That Matter Since 2010

No responses yet