Google Banner Admob crash: The specified child already has a parent. You must call removeView() on the child's parent first.

329 views
Skip to first unread message

cuong van

unread,
Dec 28, 2022, 8:58:36 PM12/28/22
to Google Mobile Ads SDK Developers
Hi,

Some time when i show banner ads, this app will crash.

My version SDK google ads: 

implementation 'com.google.android.gms:play-services-ads:21.3.0'
37%: Android 13
19%: Android 9
19%: Android 10
19%: Android 12

Here's the error logs:

Fatal Exception: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
       at android.view.ViewGroup.addViewInner(ViewGroup.java:5477)
       at android.view.ViewGroup.addView(ViewGroup.java:5296)
       at android.view.ViewGroup.addView(ViewGroup.java:5236)
       at android.view.ViewGroup.addView(ViewGroup.java:5208)
       at com.google.android.gms.ads.internal.mraid.e.a(e.java:2)
       at com.google.android.gms.ads.internal.gmsg.an.a(an.java:11)
       at com.google.android.gms.ads.internal.webview.q.B(q.java:4)
       at com.google.android.gms.ads.internal.webview.q.e(q.java:8)
       at com.google.android.gms.ads.internal.webview.ah.run(ah.java:4)
       at android.os.Handler.handleCallback(Handler.java:942)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at ass.a(ass.java)
       at com.google.android.gms.ads.internal.util.f.a(f.java:1)
       at ass.dispatchMessage(ass.java)
       at android.os.Looper.loopOnce(Looper.java:226)
       at android.os.Looper.loop(Looper.java:313)
       at android.app.ActivityThread.main(ActivityThread.java:8741)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.ja

Mobile Ads SDK Forum Advisor

unread,
Dec 29, 2022, 12:24:17 AM12/29/22
to vancuon...@gmail.com, google-adm...@googlegroups.com

Hi Cuong,

 

Thank you for reaching out to us.

 

For us to further check the crashes happening in your banner ads, can you provide us the following information below privately? You can provide the following details via reply privately to author option or directly provide it to the link below.

 

  • Sample app project where the crashes is reproducible
  • Steps to replicate
  • SDK version used
  • Device name and versions affected
  • App Id

 

If the file(s) you are looking to share are less than 25mb in total you can attach them to this case on your next reply. If you are having trouble attaching your file to this case or if your file(s) are larger than 25mb, you can share your files with me by performing the following steps:

 

1. Navigate to

https://docs.google.com/forms/d/e/1FAIpQLSfkAiXMeYP-fw1W3Z-tT9uwmATEKO5X6S-th0gR2ezdKaaqfg/viewform?usp=pp_url&entry.400550049=Mobile+Ads+SDK&entry.460850823=5004Q00002hOxjCQAS&entry.80707362=00162006

2. Fill out all fields, and attach your file(s).

3. Please reply back on this thread when you have uploaded your file(s). Please do not share this link.

 

Regards,

Google Logo
Princess Pamela
Mobile Ads SDK Team
 


ref:_00D1U1174p._5004Q2hOxjC:ref

cuong van

unread,
Dec 29, 2022, 12:55:16 AM12/29/22
to Mobile Ads SDK Forum Advisor, google-adm...@googlegroups.com
Hi Mobile Ads SDK Team,

I have sent you the form according to the link you sent me, hope it can help you to solve the problem. Hope to hear from you soon. Thank you!

Vào Th 5, 29 thg 12, 2022 vào lúc 12:23 Mobile Ads SDK Forum Advisor <mobile...@forumsupport.google> đã viết:


--

Thanks and Best regards,

Nguyễn Văn Cường

Mobile: 0982429650

Email: vacuon...@gmail.com

Mobile Ads SDK Forum Advisor

unread,
Dec 29, 2022, 10:50:46 AM12/29/22
to vancuon...@gmail.com, google-adm...@googlegroups.com
Hi Cuong,

Thank you for getting back to us.

I could see that you provided a document which contains the steps to reproduce the issue. Could you please provide to us at least a sample project as well? You may provide it using the steps provided by my colleague on this thread.

Regards,
Google Logo
Teejay Wennie
Mobile Ads SDK Team
 


ref:_00D1U1174p._5004Q2hOxjC:ref

اسلام محمد

unread,
Dec 29, 2022, 11:02:39 AM12/29/22
to Mobile Ads SDK Forum Advisor, vancuon...@gmail.com, google-adm...@googlegroups.com

--

---
You received this message because you are subscribed to the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-admob-ads...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-admob-ads-sdk/OjTZ0000000000000000000000000000000000000000000000RNNTBS00Kb9uNTEfSWe9PhlPdKMVjA%40sfdc.net.

Mobile Ads SDK Forum Advisor

unread,
Jan 3, 2023, 4:42:19 AM1/3/23
to am68...@gmail.com, google-adm...@googlegroups.com, vancuon...@gmail.com

Hi اسلام محمد,

 

Do you have any concerns related to Mobile Ads SDK? If so, please provide us the details. We'll be happy to assist you.

 

On the other hand, I've deleted your post as it contains private information. Below is the copy of your post with scrubbed information.

 

Regards,

Google Logo
Princess Pamela
Mobile Ads SDK Team
 

 

+xxxxxxxxxxx



ref:_00D1U1174p._5004Q2hOxjC:ref

Andrey K

unread,
May 21, 2024, 12:26:43 PMMay 21
to Google Mobile Ads SDK Developers
Hello Google AdMob Ads SDK team,

I am facing the same issue in 2024. Please find my stacktrace attached:

com.google.android.gms:play-services-ads:23.0.0

```

Exception: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
       at android.view.ViewGroup.addViewInner(ViewGroup.java:5505)
       at android.view.ViewGroup.addView(ViewGroup.java:5324)
       at android.view.ViewGroup.addView(ViewGroup.java:5264)
       at android.view.ViewGroup.addView(ViewGroup.java:5236)
       at com.google.android.gms.ads.internal.mraid.f.b(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:30)
       at com.google.android.gms.ads.internal.mraid.f.a(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:47)
       at com.google.android.gms.ads.internal.webview.r.d(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:52)
       at com.google.android.gms.ads.internal.webview.ag.destroy(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:32)
       at com.google.android.gms.ads.internal.webview.y.run(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:3)
       at android.os.Handler.handleCallback(Handler.java:958)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at m.azn.a(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:1)
       at com.google.android.gms.ads.internal.util.f.a(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:2)
       at m.azn.dispatchMessage(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:1)
       at android.os.Looper.loopOnce(Looper.java:230)
       at android.os.Looper.loop(Looper.java:319)
       at android.app.ActivityThread.main(ActivityThread.java:8893)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
```

I carefully read this conversation and I see you asked for the sample project to reproduce this issue. 
Unfortunately, nobody provided it. And I won't be able to provide it either - the issue happens for 1%-2% of my users and I don't have repro steps.

Some stats:
76%
Samsung
11%
Xiaomi
4%
Google
3%
Huawei
6%
Other (7)
------
67%
Android 14
11%
Android 11
9%
Android 13
8%
Android 10
5%
Other (2)

1. Could you kindly deobfuscate the stacktrace I shared, find the problematic line of code and add the "remove parent ViewGroup" one-line fix?

2. Btw, I have recently migrated my app from `AdManagerView` to `AdView` - we started using AdMob instead of AdManager. I didn't have such an issue using `AdManagerView`. Could you tell me if I can continue using `AdManagerView` for AdMob ads?

I'm looking forward to hearing from you and will appreciate any help.


Mobile Ads SDK Forum Advisor

unread,
May 22, 2024, 2:16:28 AMMay 22
to kotkov...@gmail.com, google-adm...@googlegroups.com
Hi Andrey,

Could you please confirm whether you have removed all the adViews before adding adView into the ad container while loading an ad? Also check your source code whether you are using any loop or loading banner again on ad view.

If found then the best practice is to remove those ad from container.

Could you please check and manage your ad view before loading ads.
adContainerView.removeAllViews();
adContainerView.addView(adView);

Kindly refer to our sample project BannerExample for further assistance.

 
This message is in relation to case "ref:!00D1U01174p.!5004Q02hOxjC:ref" (ADR-00162006)

Thanks,
 
Google Logo Mobile Ads SDK Team


Andrey K

unread,
May 22, 2024, 6:32:17 AMMay 22
to Google Mobile Ads SDK Developers
Hi Mobile Ads SDK Forum Advisor,

First of all, I would like to mention that we use Jetpack Compose in our android application.
```
@Composable
fun AdGoogleBannerView(
   cachedAdView: AdView,
   modifier: Modifier = Modifier,
) {
   AndroidView(
      modifier = modifier,
      factory = { context ->
         (cachedAdView.parent as? ViewGroup)?.removeView(cachedAdView)
         FrameLayout(context).apply {
            addView(cachedAdView)
         }
      },
   )
}
```

Second of all, to show Ads asap and prevent the app UI from jumping, we cache AdViews.

> Could you please confirm whether you have removed all the adViews before adding adView into the ad container while loading an ad? Also check your source code whether you are using any loop or loading banner again on ad view.

The ad container is a composable AdGoogleBannerView() with the FrameLayout in our case. When a new composable AdGoogleBannerView() is created, a new empty `FrameLayout` is created as well. If the `cachedAdView` has a parent, we remove it before adding to the `FrameLayout`.

Please let me know if you see issues in our approach and if it can be improved.

Mobile Ads SDK Forum Advisor

unread,
May 22, 2024, 9:58:48 AMMay 22
to kotkov...@gmail.com, google-adm...@googlegroups.com
Hi Andrey,

The implementation seems to be correct in regards to cache since you are removing adView before adding to the `FrameLayout`. Please note that AdMob is not natively supported in Jetpack Compose yet. Please be informed that AdMob exclusively provides support for the following platforms:

Andrey K

unread,
May 22, 2024, 12:48:21 PMMay 22
to Google Mobile Ads SDK Developers
Hi Mobile Ads SDK Forum Advisor,

Thanks for checking my implementation. I am glad it seems to be correct to you as well.

1. I would be interested to know if you have plans to add native support of Jetpack Compose for android AdMob? Would be really helpful.

2. If my implementation seems to be correct (I understand you cannot check my full code, but at least the general idea is correct), could you deobfuscate the stack trace I shared above and fix the problematic line (remove a parent probably) in Android AdMob SDK?

3. Also, I mentioned that we have moved from AdManager to AdMob recently. Could you tell me if I could use `AdManagerView` for AdMob banners? Because we didn't have any issues when we used `AdManagerView`.

Could you reply to all 3 questions one by one? Thanks in advance!

Mobile Ads SDK Forum Advisor

unread,
May 23, 2024, 2:35:28 AMMay 23
to kotkov...@gmail.com, google-adm...@googlegroups.com
Hi Andrey,


1. I would be interested to know if you have plans to add native support of Jetpack Compose for android AdMob? Would be really helpful.

-> This process is in queue, please keep a eye on android release notes.


2. If my implementation seems to be correct (I understand you cannot check my full code, but at least the general idea is correct), could you deobfuscate the stack trace I shared above and fix the problematic line (remove a parent probably) in Android AdMob SDK?

-> The error you're might be occurring because cachedAdView is already attached to a parent view, and you need to remove it from that parent before re-adding it to the new FrameLayout.


3. Also, I mentioned that we have moved from AdManager to AdMob recently. Could you tell me if I could use `AdManagerView` for AdMob banners? Because we didn't have any issues when we used `AdManagerView`.

-> I recommend you to use AdView instead of AdManager View as it might lead to some exceptions.

Andrey K

unread,
May 23, 2024, 7:22:21 AMMay 23
to Google Mobile Ads SDK Developers
Hi Mobile Ads SDK Forum Advisor,

Thanks for your replies. I would like to ask more about the second question.


> -> The error you're might be occurring because cachedAdView is already attached to a parent view, and you need to remove it from that parent before re-adding it to the new FrameLayout.

As you see in my code below, I remove a parent of the `cachedAdView` before adding it to the new FrameLayout:
```
@Composable
fun AdGoogleBannerView(
   cachedAdViewAdView,
   modifierModifier Modifier,
) {
   AndroidView(
      modifier = modifier,
      factory = context ->
         (cachedAdView.parent as? ViewGroup)?.removeView(cachedAdView) // <----- Remove parent here
         FrameLayout(context).apply {
            addView(cachedAdView)
         }
      },
   )
}
```
Also, please have a look at the stack trace:
```
Exception: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
       at android.view.ViewGroup.addViewInner(ViewGroup.java:5505)
       at android.view.ViewGroup.addView(ViewGroup.java:5324)
       at android.view.ViewGroup.addView(ViewGroup.java:5264)
       at android.view.ViewGroup.addView(ViewGroup.java:5236)
       at com.google.android.gms.ads.internal.mraid.f.b(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:30)
       at com.google.android.gms.ads.internal.mraid.f.a(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:47)
       at com.google.android.gms.ads.internal.webview.r.d(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:52)
       at com.google.android.gms.ads.internal.webview.ag.destroy(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:32)
       at com.google.android.gms.ads.internal.webview.y.run(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:3)
       at android.os.Handler.handleCallback(Handler.java:958)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at m.azn.a(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:1)
       at com.google.android.gms.ads.internal.util.f.a(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:2)
       at m.azn.dispatchMessage(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:1)
       at android.os.Looper.loopOnce(Looper.java:230)
       at android.os.Looper.loop(Looper.java:319)
       at android.app.ActivityThread.main(ActivityThread.java:8893)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
com.google.android.gms:play-services-ads:23.0.0
```
The crash doesn't happen because the `AdView` (`cachedAdView` in my case) with a parent are being added to a ViewGroup. The crash happens because the `AdMob SDK` tries to add a child to some `ViewGroup` inside the `AdView` (the `internal` package of the `AdMob SDK`).

Could you deobfuscate this stack trace and add a check `if (hasParent) removeParent` to the problematic line of code in the `internal` SDK package?
It would be extremely helpful because about 1-2% of our users face crashes and we cannot add any fix - the crash happens inside the `AdMob SDK`.

Look forward to hearing from you and would appreciate any help!

Mobile Ads SDK Forum Advisor

unread,
May 23, 2024, 1:07:57 PMMay 23
to kotkov...@gmail.com, google-adm...@googlegroups.com
Hi,

As we already mentioned in the previous mail that AdMob is not natively supported in Jetpack Compose yet, we could not provide any information for Jetpack implementations as this is out of scope for us.

Andrey K

unread,
May 24, 2024, 3:10:02 AMMay 24
to Google Mobile Ads SDK Developers
Hi Mobile Ads SDK Forum Advisor,

I appreciate your guidance, but could we please focus on the issue without considering Jetpack Compose implementation for now?

I have the next stack trace:

```
Exception: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
       at android.view.ViewGroup.addViewInner(ViewGroup.java:5505)
       at android.view.ViewGroup.addView(ViewGroup.java:5324)
       at android.view.ViewGroup.addView(ViewGroup.java:5264)
       at android.view.ViewGroup.addView(ViewGroup.java:5236)
       at com.google.android.gms.ads.internal.mraid.f.b(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:30)
       at com.google.android.gms.ads.internal.mraid.f.a(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:47)
       at com.google.android.gms.ads.internal.webview.r.d(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:52)
       at com.google.android.gms.ads.internal.webview.ag.destroy(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:32)
       at com.google.android.gms.ads.internal.webview.y.run(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:3)
       at android.os.Handler.handleCallback(Handler.java:958)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at m.azn.a(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:1)
       at com.google.android.gms.ads.internal.util.f.a(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:2)
       at m.azn.dispatchMessage(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:1)
       at android.os.Looper.loopOnce(Looper.java:230)
       at android.os.Looper.loop(Looper.java:319)
       at android.app.ActivityThread.main(ActivityThread.java:8893)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
com.google.android.gms:play-services-ads:23.0.0
```
The crash happens because the `AdMob SDK` tries to add a child to some `ViewGroup` inside the `AdView` (the `internal` package of the `AdMob SDK` - please have a look at the bold line  com.google.android.gms.ads.internal.mraid.f.b....... 1:30).

Would it be possible for you to deobfuscate this stack trace and implement a conditional check—something along the lines of `if (hasParent) removeParent`—at the source of the issue within the `internal` SDK package?

Regretfully, we find ourselves unable to directly address this crash (it happens inside the AdMob SDK). Your assistance in resolving this would be immensely beneficial and greatly appreciated.

Thank you for your attention to this matter.

Mobile Ads SDK Forum Advisor

unread,
May 24, 2024, 5:50:57 AMMay 24
to kotkov...@gmail.com, google-adm...@googlegroups.com
Hi,

Could you please confirm whether you are using any third party network adapter, in your application?

Andrey K

unread,
May 24, 2024, 5:58:12 AMMay 24
to Google Mobile Ads SDK Developers
Hi,

Could you clarify what exactly third party network adapters you mean? Could you provide a few examples of third party network adapters?

Mobile Ads SDK Forum Advisor

unread,
May 24, 2024, 12:19:49 PMMay 24
to kotkov...@gmail.com, google-adm...@googlegroups.com

Hi Andrey,

I will check with our team regarding your query and one of my team members will reach out to you once we have an update on this. Meanwhile, your patience is highly appreciated.

Mobile Ads SDK Forum Advisor

unread,
May 28, 2024, 1:54:23 PMMay 28
to kotkov...@gmail.com, google-adm...@googlegroups.com

Hi Andrey,

 

Can you provide the code which caused this error?

 

The error "The specified child already has a parent. You must call removeView() on the child's parent first." Implies the cause of this bug is not internal to the Google Mobile Ads SDK, but from irregular view hierarchy.

 

This message is in relation to case "ref:!00D1U01174p.!5004Q02hOxjC:ref" (ADR-00162006)

Thanks,
 
Google Logo
Nicholas Ventimiglia
Mobile Ads SDK Team


Andrey K

unread,
May 28, 2024, 2:57:17 PMMay 28
to Google Mobile Ads SDK Developers
Hi Nicholas,

The code / sample project to reproduce the issue was already asked several times in this thread but nobody provided it because the issue happens just for about 1% of our users - I don't have stable repro steps.
But please have a look at the stack trace I have:

```
Exception: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
       at android.view.ViewGroup.addViewInner(ViewGroup.java:5505)
       at android.view.ViewGroup.addView(ViewGroup.java:5324)
       at android.view.ViewGroup.addView(ViewGroup.java:5264)
       at android.view.ViewGroup.addView(ViewGroup.java:5236)
       at com.google.android.gms.ads.internal.mraid.f.b(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:30)
       at com.google.android.gms.ads.internal.mraid.f.a(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:47)
       at com.google.android.gms.ads.internal.webview.r.d(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:52)
       at com.google.android.gms.ads.internal.webview.ag.destroy(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:32)
       at com.google.android.gms.ads.internal.webview.y.run(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:3)
       at android.os.Handler.handleCallback(Handler.java:958)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at m.azn.a(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:1)
       at com.google.android.gms.ads.internal.util.f.a(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:2)
       at m.azn.dispatchMessage(:com.google.android.gms.policy_ads_fdr_dynamite@241199803@241199801034.618989241.618989241:1)
       at android.os.Looper.loopOnce(Looper.java:230)
       at android.os.Looper.loop(Looper.java:319)
       at android.app.ActivityThread.main(ActivityThread.java:8893)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
com.google.android.gms:play-services-ads:23.0.0
```

The bold line says that the `ViewGroup.addView()` method is called at the line:
```
com.google.android.gms.ads.internal.mraid.f.b....... 1:30)
```
It's AdMob SDK code. There is no any reference to my code in this stack trace.

Could you deobfuscate this play-services-ads:23.0.0 stack trace and have a look at the problematic line of code?

Mobile Ads SDK Forum Advisor

unread,
May 28, 2024, 5:18:47 PMMay 28
to kotkov...@gmail.com, google-adm...@googlegroups.com

Hi Andrey,

I think the problem is in your use of caching. Can you try a simpler implementation? For instance:

@Composable
fun BannerAd(adSize: AdSize, adRequest: AdRequest, adUnitId: String, modifier: Modifier) {
  
  // Remember the adView so we can dispose of it later.
  var adView by remember { mutableStateOf<AdView?>(null) }

  if (LocalInspectionMode.current) {
    Box(
      modifier =
        Modifier.background(Color.Gray)
          .width(adSize.width.dp)
          .height(adSize.height.dp)
    ) {
      Text(text = "Google Mobile Ads Preview Banner.", modifier.align(Alignment.Center))
    }
    return
  }

  AndroidView(
    modifier = modifier.fillMaxWidth(),
    factory = { context ->
      AdView(context).apply {
        // Make sure we only run this code block once and in non-preview mode.
        if (adView != null) {
          return@apply
        }

        adView = this
        this.adUnitId = adUnitId
        this.setAdSize(adSize)
        this.adListener = ...
        this.loadAd(adRequest)
      }
    },
  )
  // Clean up the AdView after use.
  DisposableEffect(Unit) { onDispose { adView?.destroy() } }
}

Andrey K

unread,
May 29, 2024, 4:58:26 AMMay 29
to Google Mobile Ads SDK Developers
Hi Nicholas,

Thank you for the code example. 

We aim to utilize Ad caching to enhance our users' app experience. By displaying cached Ads immediately, we can prevent any UI disruptions that occur when Ads are loaded in real-time. Based on my understanding, caching AdViews is permitted.

According to this link (https://groups.google.com/g/google-admob-ads-sdk/c/OAGAtvMJyXE/m/4q6bTdzMBwAJ), preloading ads in 2023 (and I hope in 2024) is still fine.

I would be interested to know if you could provide us with any suggestions on how to fix or prevent this crash using Ads caching.

Mobile Ads SDK Forum Advisor

unread,
May 29, 2024, 1:41:49 PMMay 29
to kotkov...@gmail.com, google-adm...@googlegroups.com
Hi Andrey,

Give me some time to cross check this issue with the Jetpack team. I will let you know the results shortly.
Reply all
Reply to author
Forward
0 new messages