Wednesday, July 10, 2024


We heard your feedback via [Play Console crash reports](https://support.google.com/googleplay/android-developer/answer/9859174) regarding [Application Not Responding (ANRs)](https://developer.android.com/topic/performance/vitals/anr) errors related to the Google Mobile Ads SDK. After analyzing these reports, we updated our SDK implementation best practices to reduce ANR rates. The recommended best practices are as follows:

<br />

1. Initialize the Mobile Ads SDK on a background thread
2. Enable optimization flag for ad loading


**1. Initialize the Mobile Ads SDK on a background thread**


Our previous best practice was to specify the `OPTIMIZE_INITIALIZATION` manifest flag. However, some work on the calling thread is still required to prepare `MobileAds` to handle other method calls synchronously.


We now recommend calling `MobileAds.initialize()` on a background thread, enabling the work required on the calling thread to happen in the background.

```
import com.google.android.gms.ads.MobileAds
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    CoroutineScope(Dispatchers.IO).launch {
      // Initialize the Google Mobile Ads SDK on a background thread.
      MobileAds.initialize(this@MainActivity) {}
      runOnUiThread {
        // Load an ad on the main thread.
        loadAd()
      }
    }
  }
}
```


Note: When calling `MobileAds.initialize()` on a background thread, the `OPTIMIZE_INITIALIZATION `manifest flag is no longer required.


**2. Enable optimization flag for ad loading**


By enabling the `OPTIMIZE_AD_LOADING` manifest flag, you can offload most ad loading tasks to a background thread. We recommend enabling this flag in your app's `AndroidManifest.xml` file to reduce the occurrence of ad loading causing ANRs.

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


We've updated all of our [Android example apps](https://github.com/googleads/googleads-mobile-android-examples) to implement these best practices. For more details on initialization and optimization flags, see [Get started](https://developers.google.com/admob/android/quick-start.md.txt#initialize_the_mobile_ads_sdk) and [Optimize initialization and ad loading](https://developers.google.com/admob/android/optimize-initialization.md.txt). [Contact us](https://developers.google.com/admob/support) if you have any questions or need additional help.

![Portrait of the author](https://developers.google.com/static/ads/blog/images/ACg8ocKISGPasHSsoY62e-mi7sHCO_fg7CcJLfTQwSzCQ0SNiMhrZ00=s519-c-no.jpg) - Joshua Hui, Mobile Ads Developer Relations


**Labels:**

[ad-manager](https://developers.google.com/ads/blog/feeds/labels/ad-manager.md.txt),

[admob](https://developers.google.com/ads/blog/feeds/labels/admob.md.txt),

[android](https://developers.google.com/ads/blog/feeds/labels/android.md.txt),

[mobile-ads-sdk](https://developers.google.com/ads/blog/feeds/labels/mobile-ads-sdk.md.txt)