I’m integrating App Open Ads using AdMob in my Android app. The ads load and display correctly when the app is opened from the background or after the first launch, but on the very first launch, I get a CORS error and the ad fails to load.
Details:
-
The error only occurs on the first launch of the app.
-
If I minimize the app and then reopen it, the ad loads and displays without any issues.
-
I’ve ensured that MobileAds.initialize() is called before loading ads, and I’ve tried loading ads both in the Application class and in MainActivity.
-
The issue persists regardless of where I place the code.
My Application Class:
import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.util.Log
import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.FullScreenContentCallback
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.MobileAds
import com.google.android.gms.ads.appopen.AppOpenAd
import com.kdvhesapla.app.view.MainActivity
import dagger.hilt.android.HiltAndroidApp
@HiltAndroidApp
class KdvApplication : Application(), Application.ActivityLifecycleCallbacks {
private var appOpenAdManager: AppOpenAdManager? = null
override fun onCreate() {
super.onCreate()
MobileAds.initialize(this)
appOpenAdManager = AppOpenAdManager(this)
registerActivityLifecycleCallbacks(this)
}
override fun onActivityCreated(p0: Activity, p1: Bundle?) {
Log.i("KdvApplication", "onActivityCreated: ${p0.localClassName}")
}
override fun onActivityStarted(p0: Activity) {
}
override fun onActivityResumed(activity: Activity) {
if (activity is MainActivity) {
appOpenAdManager?.showAdIfAvailable(activity)
}
}
override fun onActivityPaused(p0: Activity) {
}
override fun onActivityStopped(p0: Activity) {
}
override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {
}
override fun onActivityDestroyed(p0: Activity) {
}
private class AppOpenAdManager(private val application: KdvApplication) {
private var appOpenAd: AppOpenAd? = null
private var isAdShowing = false
fun loadAd() {
Log.i("KdvApplication", "loadAd()")
val adRequest = AdRequest.Builder().build()
AppOpenAd.load(
application,
"ca-app-pub-3940256099942544/9257395921", //TEST ID, NOT PUBLIC
adRequest,
AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT,
object : AppOpenAd.AppOpenAdLoadCallback() {
override fun onAdLoaded(ad: AppOpenAd) {
appOpenAd = ad
setFullScreenContentCallback(ad)
}
override fun onAdFailedToLoad(loadAdError: LoadAdError) {
Log.e("KdvApplication", "OPENAD ERROR: $loadAdError")
}
})
}
fun showAdIfAvailable(activity: MainActivity) {
if (appOpenAd != null && !isAdShowing) {
isAdShowing = true
appOpenAd?.show(activity)
} else {
loadAd()
}
}
private fun setFullScreenContentCallback(ad: AppOpenAd) {
ad.fullScreenContentCallback = object : FullScreenContentCallback() {
override fun onAdDismissedFullScreenContent() {
isAdShowing = false
appOpenAd = null
loadAd()
}
override fun onAdFailedToShowFullScreenContent(adError: AdError) {
isAdShowing = false
}
override fun onAdShowedFullScreenContent() {
Log.i("KdvApplication", "onAdShowedFullScreenContent")
}
}
}
}
}
Logs: (Cors error is a very long log so I couldn’t add them all here.)
I Considering local module com.google.android.gms.ads.dynamite:0 and remote module com.google.android.gms.ads.dynamite:242402501
I Selected remote version of com.google.android.gms.ads.dynamite, version >= 242402501
I Considering local module com.google.android.gms.ads.dynamite:0 and remote module com.google.android.gms.ads.dynamite:242402501
I Selected remote version of com.google.android.gms.ads.dynamite, version >= 242402501
D Instantiating com.google.android.gms.ads.ChimeraMobileAdsSettingManagerCreatorImpl
I Updating ad debug logging enablement.
I App measurement initialized, version: 99007
I To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
D Instantiating com.google.android.gms.ads.ChimeraAdManagerCreatorImpl
I Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("6F10891F6C167243871F6BC27BD44A48")) to get test ads on this device.
W Invoke Firebase method getInstance error.
W The Google Mobile Ads SDK will not integrate with Firebase. Admob/Firebase integration requires the latest Firebase SDK jar, but Firebase SDK is either missing or out of date
W Dynamic lookup for intent failed for action: com.google.android.gms.ads.service.CACHE
D Instantiating com.google.android.gms.ads.ChimeraAdManagerCreatorImpl
I Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("6F10891F6C167243871F6BC27BD44A48")) to get test ads on this device.
I Tag Manager is not found and thus will not be used
W Update ad debug logging enablement as false
W Not retrying to fetch app settings
W Dynamic lookup for intent failed for action: com.google.android.gms.ads.service.START
W Not retrying to fetch app settings
W Analytics storage consent denied; will not get app instance id
I JS: The jsLoaded GMSG has been sent (https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/production/sdk-core-v40-impl.html:778)
I [INFO:CONSOLE(778)] "The jsLoaded GMSG has been sent", source: https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/production/sdk-core-v40-impl.html (778)
I JS: The jsLoaded GMSG has been sent (https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/production/sdk-core-v40-impl.html:778)
I [INFO:CONSOLE(778)] "The jsLoaded GMSG has been sent", source: https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/production/sdk-core-v40-impl.html (778)
I Considering local module com.google.android.gms.ads.dynamite:0 and remote module com.google.android.gms.ads.dynamite:242402501
I Selected remote version of com.google.android.gms.ads.dynamite, version >= 242402501
JS: Access to fetch at 'https://api16-event-va.pangle.io/......
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.google.android.gms.permission.AD_ID"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/Theme.KdvHesapla"
android:name=".KdvApplication"
tools:targetApi="31">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="XXXXXX"/>
<activity
android:name=".view.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".view.ProcessHistoryActivity"
android:exported="true">
</activity>
</application>
</manifest>
build.gradle:
implementation ("com.google.android.gms:play-services-ads:23.3.0")