question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

java.lang.IllegalStateException: The ad unit ID can only be set once on InterstitialAd.

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
distantecommented, Nov 6, 2020

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

if(mInterstitialAd == null) {
   // call some console error
    return
}

But you should take a look if you are calling prepare two times somewhere. Perhaps on a component constructor or similar.

1reaction
distantecommented, Apr 27, 2021

Hi, great work on this plugin, thanks! Just wondering how to show more than one Interstitial Ad? Not sure if i missed something? I thought i had to prepare each time?

Should mInterstitialAd be nulled again maybe on the show?

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found