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:
- Initialize the Mobile Ads SDK on a background thread
- 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 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.