Cut back ANRs when implementing cellular adverts
We heard your suggestions through Play Console crash reviews relating to Software Not Responding (ANRs) errors associated to the Google Cellular Advertisements SDK. After analyzing these reviews, we up to date our SDK implementation greatest practices to cut back ANR charges. The really useful greatest practices are as follows:
- Initialize the Cellular Advertisements SDK on a background thread
- Allow optimization flag for advert loading
1. Initialize the Cellular Advertisements SDK on a background thread
Our earlier greatest follow was to specify the OPTIMIZE_INITIALIZATION manifest flag. Nevertheless, some work on the calling thread continues to be required to arrange MobileAds to deal with different methodology calls synchronously.
We now suggest calling MobileAds.initialize() on a background thread, enabling the work required on the calling thread to occur within the background.
import com.google.android.gms.adverts.MobileAds
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
override enjoyable onCreate(savedInstanceState: Bundle?) {
tremendous.onCreate(savedInstanceState)
setContentView(R.format.activity_main)
CoroutineScope(Dispatchers.IO).launch {
// Initialize the Google Cellular Advertisements SDK on a background thread.
MobileAds.initialize(this@MainActivity) {}
runOnUiThread {
// Load an advert on the primary thread.
loadAd()
}
}
}
}
Word: When calling MobileAds.initialize() on a background thread, the OPTIMIZE_INITIALIZATION manifest flag is not required.
2. Allow optimization flag for advert loading
By enabling the OPTIMIZE_AD_LOADING manifest flag, you possibly can offload most advert loading duties to a background thread. We suggest enabling this flag in your app’s AndroidManifest.xml file to cut back the prevalence of advert loading inflicting ANRs.
<manifest>
...
<utility>
...
<meta-data
android:identify="com.google.android.gms.adverts.flag.OPTIMIZE_AD_LOADING"
android:worth="true"/>
</utility>
</manifest>
We’ve up to date all of our Android instance apps to implement these greatest practices. For extra particulars on initialization and optimization flags, see Get began and Optimize initialization and advert loading. Contact us when you’ve got any questions or want extra assist.