Hi Arjun Singh,
Mobile Ads SDK Team |
private val isMobileAdsInitializeCalled = AtomicBoolean(false)
private var adView: AdView? = null
private lateinit var googleMobileAdsConsentManager: GoogleMobileAdsConsentManager
googleMobileAdsConsentManager =
GoogleMobileAdsConsentManager.getInstance(applicationContext)
googleMobileAdsConsentManager.isDebugMode = false
googleMobileAdsConsentManager.gatherConsent(this) { error ->
Log.e(TAG, "" + error.toString());
if (error != null) {
// Log error details
Log.e(
TAG,
"Consent error: Code = ${error.errorCode}, Message = ${error.message}"
)
} else {
Log.d(TAG, "Consent successfully gathered.")
}
Log.d(TAG, "Can request ads: ${googleMobileAdsConsentManager.canRequestAds}")
if (googleMobileAdsConsentManager.canRequestAds) {
initializeMobileAdsSdk()
}
Log.d(
TAG,
"Is privacy options required: ${googleMobileAdsConsentManager.isPrivacyOptionsRequired}"
)
if (googleMobileAdsConsentManager.isPrivacyOptionsRequired) {
invalidateOptionsMenu()
Log.d(TAG, "Privacy options menu invalidated.")
}
}
private val adSize: AdSize
get() {
val displayMetrics = resources.displayMetrics
val adWidthPixels =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val windowMetrics: WindowMetrics = this.windowManager.currentWindowMetrics
windowMetrics.bounds.width()
} else {
displayMetrics.widthPixels
}
val density = displayMetrics.density
val adWidth = (adWidthPixels / density).toInt()
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)
}
/** Called when leaving the activity. */
public override fun onPause() {
adView?.pause()
super.onPause()
}
/** Called when returning to the activity. */
public override fun onResume() {
super.onResume()
adView?.resume()
}
/** Called before the activity is destroyed. */
public override fun onDestroy() {
adView?.destroy()
super.onDestroy()
}
private fun loadBanner() {
val adView = AdView(this)
adView.adUnitId = getString(R.string.admob_add_banners_id)
adView.setAdSize(adSize)
this.adView = adView
// Replace ad container with new ad view.
binding!!.adViewContainer.removeAllViews()
binding!!.adViewContainer.addView(adView)
// [END create_ad_view]
val adRequest: AdRequest = AdRequest.Builder()
.build()
adView.loadAd(adRequest)
}
// [START request_ads]
private fun initializeMobileAdsSdk() {
if (isMobileAdsInitializeCalled.getAndSet(true)) {
return
}
// [END_EXCLUDE]
CoroutineScope(Dispatchers.IO).launch {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this) {
}
runOnUiThread {
// Load an ad on the main thread.
loadBanner()
}
}
}
I am currently experiencing an issue where ads are not displaying in my Android app's release build, despite being correctly configured in the development environment. I have followed the necessary steps to initialize the Mobile Ads SDK, gather consent, and request ads. However, the ads are not appearing in the release version of the app.
Details:I would appreciate your assistance in investigating why ads are not being served in the release build. Could you please provide any insights or additional steps I can take to resolve this issue?
Thank you for your help!
Arjun Singh
Hi Arjun Singh,
Thank you for getting back to us.
I'm deleting the previous thread from the forum as it consists of PII information (app ID: ca-app-pub-656XXXXXXXXX43~24XXXXXX27 and ad unit ID: ca-app-pub-65XXXXXX43/43XXX94).
Please note that Received error HTTP response code: 403 means, AdMob servers rejected the request. Could you try again later? If consistently reproducible, capture the request URL using ad inspector and contact the Product Support Team as they are better equipped to address your concern and provide further assistance.