Flutter App crashes when InterstitialAd is loaded

520

I had the same issue, and fixed it by adjusting some dependencies in my build.gradle file.

The problem seems to be caused by play-services-ads version 20, which moves the InterstitialAd class to the namespace "com.google.android.gms.ads.interstitial" (previously it was in namespace "com.google.android.gms.ads").

The Flutter plugin Google Mobile Ads version 0.12.1+1 uses play-services-ads version 19.7, so it tries to load the InterstitialAd class from the old namespace. The problem in my case is that we use Admob mediation, and we use third party mediation adapters. The latest versions of these adapters were using play-services-ads version 20, and that caused the issue. The solution for me was checking the change log in the Admob mediation page, for each adapter we use, and then include the version of the adapter using play-services-ads version 19.7. For example, we use:

implementation 'com.google.ads.mediation:adcolony:4.4.1.0'
implementation 'com.google.ads.mediation:applovin:9.15.2.0'
implementation 'com.google.ads.mediation:tapjoy:12.7.1.0'
implementation 'com.google.ads.mediation:facebook:6.3.0.1'

EDIT: Google just released the google_mobile_ads package version 0.13.0, which targets GMA for Android dependency version 20.1.0, so the issue will be gone if you update this package.

Share:
520
Deepak sharma
Author by

Deepak sharma

Updated on November 27, 2022

Comments

  • Deepak sharma
    Deepak sharma over 1 year

    I have tested bannerAds it is working correctly but when I try to load interstitial ad it crashes the app.

    MinSdkVersion 29 I have tried to load the ad in initState and the build method separately.

    Banner ads are running correctly.

    InterstitialAd loadInterstitialAd() {
        return InterstitialAd(
            adUnitId: "ca-app-pub-3940256099942544/1033173712",
            listener: AdListener(onAdLoaded: (_) {
              setState(() {
                _isAdLoaded = true;
              });
            }, onAdFailedToLoad: (_ad, error) {
              _ad.dispose();
              print("Ad Exited with error $error");
            }, onAdClosed: (_ad) {
              Navigator.pop(context);
              _ad.dispose();
            }),
            request: AdRequest());
      }
    
      @override
      Widget build(BuildContext context) {
        InterstitialAd _ad = loadInterstitialAd();
        _ad.load();
        return Scaffold(
          body: Center(
              child: MaterialButton(
            onPressed: () {},
            color: Colors.red,
          )
              ),
        );
      }
    

    Error log is like

    E/AndroidRuntime(22505): FATAL EXCEPTION: main
    E/AndroidRuntime(22505): Process: tech.deepaksharma.statussaver, PID: 22505
    E/AndroidRuntime(22505): java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/ads/InterstitialAd;
    E/AndroidRuntime(22505):    at io.flutter.plugins.googlemobileads.FlutterInterstitialAd.load(FlutterInterstitialAd.java:70)
    E/AndroidRuntime(22505):    at io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin.onMethodCall(GoogleMobileAdsPlugin.java:299)
    E/AndroidRuntime(22505):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
    E/AndroidRuntime(22505):    at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
    E/AndroidRuntime(22505):    at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:818)
    E/AndroidRuntime(22505):    at android.os.MessageQueue.nativePollOnce(Native Method)
    E/AndroidRuntime(22505):    at android.os.MessageQueue.next(MessageQueue.java:343)
    E/AndroidRuntime(22505):    at android.os.Looper.loop(Looper.java:188)
    E/AndroidRuntime(22505):    at android.app.ActivityThread.main(ActivityThread.java:7582)
    E/AndroidRuntime(22505):    at java.lang.reflect.Method.invoke(Native Method)
    E/AndroidRuntime(22505):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    E/AndroidRuntime(22505):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
    E/AndroidRuntime(22505): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.ads.InterstitialAd" on path: DexPathList[[zip file "/data/app/tech.deepaksharma.statussaver-7ebD-4rtvclB1sSW4YF-Lw==/base.apk"],nativeLibraryDirectories=[/data/app/tech.deepaksharma.statussaver-7ebD-4rtvclB1sSW4YF-Lw==/lib/arm64, /data/app/tech.deepaksharma.statussaver-7ebD-4rtvclB1sSW4YF-Lw==/base.apk!/lib/arm64-v8a, /system/lib64, /system/product/lib64]]
    E/AndroidRuntime(22505):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
    E/AndroidRuntime(22505):    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    E/AndroidRuntime(22505):    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    E/AndroidRuntime(22505):    ... 12 more
    I/Process (22505): Sending signal. PID: 22505 SIG: 9
    Lost connection to device.