diff --git a/AdMobAdapter/build.gradle.kts b/AdMobAdapter/build.gradle.kts index c95d120..2632a3d 100644 --- a/AdMobAdapter/build.gradle.kts +++ b/AdMobAdapter/build.gradle.kts @@ -34,7 +34,7 @@ android { minSdk = 21 targetSdk = 33 // If you touch the following line, don't forget to update scripts/get_rc_version.zsh - android.defaultConfig.versionName = System.getenv("VERSION_OVERRIDE") ?: "4.22.1.0.1" + android.defaultConfig.versionName = System.getenv("VERSION_OVERRIDE") ?: "4.22.1.0.2" buildConfigField( "String", diff --git a/AdMobAdapter/src/main/java/com/chartboost/mediation/admobadapter/AdMobAdapter.kt b/AdMobAdapter/src/main/java/com/chartboost/mediation/admobadapter/AdMobAdapter.kt index 92b1ab1..e35eaed 100644 --- a/AdMobAdapter/src/main/java/com/chartboost/mediation/admobadapter/AdMobAdapter.kt +++ b/AdMobAdapter/src/main/java/com/chartboost/mediation/admobadapter/AdMobAdapter.kt @@ -27,9 +27,11 @@ import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.launch import kotlinx.coroutines.suspendCancellableCoroutine +import kotlinx.coroutines.withContext import kotlin.coroutines.resume class AdMobAdapter : PartnerAdapter { @@ -49,9 +51,12 @@ class AdMobAdapter : PartnerAdapter { else value.joinToString() }" ) - MobileAds.setRequestConfiguration( - RequestConfiguration.Builder().setTestDeviceIds(value).build() - ) + // There have been known ANRs when calling setRequestConfiguration() on the main thread. + CoroutineScope(IO).launch { + MobileAds.setRequestConfiguration( + RequestConfiguration.Builder().setTestDeviceIds(value).build() + ) + } } /** @@ -127,9 +132,13 @@ class AdMobAdapter : PartnerAdapter { ): Result { PartnerLogController.log(SETUP_STARTED) - // Since Chartboost Mediation is the mediator, no need to initialize AdMob's partner SDKs. - // https://developers.google.com/android/reference/com/google/android/gms/ads/MobileAds?hl=en#disableMediationAdapterInitialization(android.content.Context) - MobileAds.disableMediationAdapterInitialization(context) + withContext(IO) { + // Since Chartboost Mediation is the mediator, no need to initialize AdMob's partner SDKs. + // https://developers.google.com/android/reference/com/google/android/gms/ads/MobileAds?hl=en#disableMediationAdapterInitialization(android.content.Context) + // + // There have been known ANRs when calling disableMediationAdapterInitialization() on the main thread. + MobileAds.disableMediationAdapterInitialization(context) + } return suspendCancellableCoroutine { continuation -> fun resumeOnce(result: Result) { @@ -211,16 +220,19 @@ class AdMobAdapter : PartnerAdapter { else COPPA_NOT_SUBJECT ) - MobileAds.setRequestConfiguration( - MobileAds.getRequestConfiguration().toBuilder() - .setTagForChildDirectedTreatment( - if (isSubjectToCoppa) { - RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_TRUE - } else { - RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_FALSE - } - ).build() - ) + // There have been known ANRs when calling setRequestConfiguration() on the main thread. + CoroutineScope(IO).launch { + MobileAds.setRequestConfiguration( + MobileAds.getRequestConfiguration().toBuilder() + .setTagForChildDirectedTreatment( + if (isSubjectToCoppa) { + RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_TRUE + } else { + RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_FALSE + } + ).build() + ) + } } /** diff --git a/CHANGELOG.md b/CHANGELOG.md index c8f2cbd..65377ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ Note the first digit of every adapter version corresponds to the major version of the Chartboost Mediation SDK compatible with that adapter. Adapters are compatible with any Chartboost Mediation SDK version within that major version. +### 4.22.1.0.2 +- Offload `setRequestConfiguration()` and `disableMediationAdapterInitialization()` calls to background threads to avoid ANRs. + ### 4.22.1.0.1 - Guard against multiple continuation resumes wherever possible. diff --git a/README.md b/README.md index 421bb32..8a1b3e9 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ The Chartboost Mediation AdMob adapter mediates AdMob via the Chartboost Mediati In your `build.gradle`, add the following entry: ``` - implementation "com.chartboost:chartboost-mediation-adapter-admob:4.22.1.0.1" + implementation "com.chartboost:chartboost-mediation-adapter-admob:4.22.1.0.2" ``` ## Contributions