java.lang.IllegalStateException: The ad unit ID can only be set once on InterstitialAd.
See original GitHub issueDescribe the bug Sometimes when open app Google Play Console gives me the following info on the crashes:
java.lang.IllegalStateException: The ad unit ID can only be set once on InterstitialAd.
at com.google.android.gms.internal.ads.zzxx.setAdUnitId(com.google.android.gms:play-services-ads-lite@@19.1.0:80)
at com.google.android.gms.ads.InterstitialAd.setAdUnitId(com.google.android.gms:play-services-ads-lite@@19.1.0:17)
at com.getcapacitor.community.admob.helpers.AdViewIdHelper.assignIdToAdView(AdViewIdHelper.java:37)
at com.getcapacitor.community.admob.AdMob$2.run(AdMob.java:114)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Device: Google Pixel 2 API 28
Other info: my js code:
initializeApp() {
this.platform.ready().then(() => {
this.prepareConfig() ;
});
}
optionsInterstitial: AdOptions;
prepareConfig() {
if (Capacitor.platform !== 'web') {
AdMob.initialize();
this.prepareConfigInterstitial();
}
}
prepareConfigInterstitial() {
if (Capacitor.platform == 'ios') {
this.optionsInterstitial = {
adId: AppConfig.admob.interstitial.ios,
}
}
if (Capacitor.platform == 'android') {
this.optionsInterstitial = {
adId: AppConfig.admob.interstitial.android,
}
}
if (Capacitor.platform !== 'web') {
AdMob.prepareInterstitial(this.optionsInterstitial)
.then(
async (value: any) => {
console.log(value); // true
},
(error: any) => {
console.error(error); // show error
}
);
}
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
The ad unit ID can only be set once on InterstitialAd #53 - GitHub
As @willoughby said, the problem is likely caused by setting the ad unit id in a lifecycle method. The interstital module is a...
Read more >The ad size and ad unit ID must be set before loadAd when ...
3- If you try to use setAdUnitAd twice this exception will launched The ad unit ID can only be set once on AdView....
Read more >Change Ad Unit ID of AdView - Google Groups
07-17 09:21:04.321: E/AndroidRuntime(12381): java.lang.IllegalStateException: The ad unit ID can only be set once on AdView.
Read more >The ad unit ID must be set on InterstitialAd before show is called.
Hi, this message comes from Crashlytics Report. How can it happen? Fatal Exception: java.lang.IllegalStateException. The ad unit ID must be set ...
Read more >Create an interstitial ad unit - Google AdMob Help
Interstitial ad units show full-page ads in your app. Place them at natural breaks & transitions in your app's interface, such as after...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
AFAIK this happens when you call prepare two times (so the plugin tries to reassign the ad unit id). I suppose we could change the behavior in a way that is the banner was already loaded then nothing is done (and an error is returned) here: https://github.com/capacitor-community/admob/blob/9e153cd62f1736cf40c721ae2464b9005c04550c/android/src/main/java/com/getcapacitor/community/admob/AdMob.java#L105-L107
something like
But you should take a look if you are calling prepare two times somewhere. Perhaps on a component constructor or similar.
Yes you are half right, it should be nulled again, but on close:
https://github.com/capacitor-community/admob/blob/9e153cd62f1736cf40c721ae2464b9005c04550c/android/src/main/java/com/getcapacitor/community/admob/AdMob.java#L151-L155