googleads-mobile-flutter icon indicating copy to clipboard operation
googleads-mobile-flutter copied to clipboard

Call `MobileAds.initialize` on background thread

Open blaugold opened this issue 1 year ago • 6 comments

The Google Ads team now recommends calling MobileAds.initialize on a background thread to avoid ANRs.

Currently, this Plugin calls MobileAds.initialize on the UI thread.

https://github.com/googleads/googleads-mobile-flutter/blob/8e0834ee7f9b8f0ac1b886c5b3cae8937835c605/packages/google_mobile_ads/android/src/main/java/io/flutter/plugins/googlemobileads/GoogleMobileAdsPlugin.java#L349

https://github.com/googleads/googleads-mobile-flutter/blob/8e0834ee7f9b8f0ac1b886c5b3cae8937835c605/packages/google_mobile_ads/android/src/main/java/io/flutter/plugins/googlemobileads/FlutterMobileAdsWrapper.java#L39

blaugold avatar Jul 15 '24 07:07 blaugold

Hi @blaugold, thanks for flagging. I've shared this with engineering to determine next steps.

malandr2 avatar Jul 15 '24 15:07 malandr2

Is there any workaround we can do on the flutter side while this lib is not updated? Can we call MobileAds.initialize in another Isolate safely?

Gilianp avatar Sep 05 '24 00:09 Gilianp

Hi @Gilianp, we have a PR in the works https://github.com/googleads/googleads-mobile-flutter/pull/1165 that will be included in the next version. Please see until it's submitted

malandr2 avatar Sep 05 '24 14:09 malandr2

Hi @Gilianp, we have a PR in the works #1165 that will be included in the next version. Please see until it's submitted

Thanks @malandr2 , I will wait the next update!

Gilianp avatar Sep 05 '24 15:09 Gilianp

I saw that this is implemented on all the examples folders:

<meta-data
    android:name="com.google.android.gms.ads.flag.OPTIMIZE_AD_LOADING"
    android:value="true" />
  1. AppOpen: https://github.com/googleads/googleads-mobile-flutter/pull/1172/files#diff-ce2cacda361f60f008608daec0de85463f9c41d2b6ed8279242ff65b0b27c099

  2. Banner: https://github.com/googleads/googleads-mobile-flutter/pull/1172/files#diff-065422729ac6c9b5950ec50e23c96293adb847f5b1317ba0f807a9ee4a14e07d

  3. Interstitial: https://github.com/googleads/googleads-mobile-flutter/pull/1172/files#diff-edfadc71f4943e251704336e4e4c3e685c0844261a37ac8aa08b86be13345948

  4. Mediation: https://github.com/googleads/googleads-mobile-flutter/pull/1172/files#diff-e56b43ee67852aaeb05046321fcebb7f988ebbe61d71812caa9f5f786fcbbaf9

5, 6, 7 e 8 is the same thing...

This shouldn't be added into the plugin android manifest too ? Otherwise everyone will have to implement this manually.

marcellocamara avatar Sep 14 '24 14:09 marcellocamara

Hi @marcellocamara, those flags are optional. They are needed if you're not calling MobileAds.initialize on a background thread. When the google_mobile_ads plugin adds the background thread functionality, these flags will no longer be needed in our samples. See https://developers.google.com/admob/android/optimize-initialization#update_your_manifest_file for more details

malandr2 avatar Sep 16 '24 13:09 malandr2

This has been resolved in https://github.com/googleads/googleads-mobile-flutter/pull/1179 and will be included in the next release .5.2.0

malandr2 avatar Oct 07 '24 14:10 malandr2

@LTPhantom thanks is that, seems there is InitializationStatus return, or related, so the (my) app shouldn't await MobileAds.instance.initialize()? thanks

update

thanks, resolved i knew that is about the platform

peterweb2005 avatar Oct 09 '24 09:10 peterweb2005