The Business & Technology Network
Helping Business Interpret and Use Technology
S M T W T F S
1
 
2
 
3
 
4
 
5
 
6
 
7
 
8
 
9
 
10
 
11
 
12
 
13
 
14
 
15
 
16
 
17
 
18
 
19
 
20
 
21
 
22
 
23
 
24
 
25
 
26
 
27
 
28
 
29
 
30
 
 
 
 
 
 

Reduce ANRs when implementing mobile ads

Tags: google mobile
DATE POSTED:July 10, 2024

We heard your feedback via Play Console crash reports regarding Application Not Responding (ANRs) 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:

  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.

... ...

We’ve updated all of our Android example apps to implement these best practices. For more details on initialization and optimization flags, see Get started and Optimize initialization and ad loading. Contact us if you have any questions or need additional help.

 - Joshua Hui, Mobile Ads Developer Relations
Tags: google mobile