Flutter App is crashing on launch after implementing google ads
The app is crashing on launch after implementing google ads. I'm using an ios simulator. Ads are working fine on the other apps with the same code implementation but not on this app. Others apps are displaying test ads even with original Ad Units IDs. Interstitial Ads and Rewarded Ads are working fine only banner ads got the issue.
The error: To get test ads on this device, set: Objective-C GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers = @[ kGADSimulatorID ]; Swift GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers = [ kGADSimulatorID ]
Code: AdHelper Class
import 'package:google_mobile_ads/google_mobile_ads.dart'; import 'dart:io'; class AdHelper{ // Android Ad Units static String _bannerAd_And = 'ca-app-pub-3884661730977437/3917788070'; static String _interAd_And = 'ca-app-pub-3884661730977437/1291624734'; static String _bannerAdTest_And = 'ca-app-pub-3940256099942544/6300978111'; static String _interAdTest_And = 'ca-app-pub-3940256099942544/1033173712'; // iOS Ad Units static String _bannerAd_iOS = 'ca-app-pub-3884661730977437/4131225272'; static String _interAd_iOS = 'ca-app-pub-3884661730977437/6845018522'; static String _bannerAdTest_iOS = 'ca-app-pub-3940256099942544/2934735716'; static String _interAdTest_iOS = 'ca-app-pub-3940256099942544/4411468910'; // FN returns Banner AD Unit Id static String get bannerAdUnitId { if (Platform.isAndroid) { return _bannerAdTest_And; } else if (Platform.isIOS) { return _bannerAdTest_iOS; } else { throw UnsupportedError('Unsupported platform'); } } // FN returns Interstitial Ad Unit Id static String get interAdUnitId { if (Platform.isAndroid) { return _interAdTest_And; } else if (Platform.isIOS) { return _interAdTest_iOS; } else { throw UnsupportedError('Unsupported platform'); } } }
AdController
import 'package:get/get.dart'; import 'package:google_mobile_ads/google_mobile_ads.dart'; import 'AdMob_Helper.dart'; class AdMobController extends GetxController{ ///------------------ Init @override void onInit() { getBannerAd(); super.onInit(); } ///------------------ Dispose @override void onClose() { bannerAd.dispose(); super.onClose(); } late BannerAd bannerAd; bool isBannerLoaded = false; // void getBannerAd() { bannerAd = BannerAd( adUnitId: AdHelper.bannerAdUnitId, size: AdSize.banner, request: AdRequest(), listener: BannerAdListener( onAdLoaded: (_) { isBannerLoaded = true; update(); }, onAdFailedToLoad: (ad, error) { // Releases an ad resource when it fails to load ad.dispose(); print('Ad load failed (code=${error.code} message=${error.message})'); }, ), ); // TODO: Load an ad bannerAd.load(); update(); } }
I know I am late to the party but may this is help someone in future.
In my case I had put my meta-data in wrong place.
My app was also crashing after a white screen, probably splash screen. I opened android studio and view logcat which indicated that my google_mobile_ads was not initialised properly.
I had put <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-xxxxxxx~xxxxxx" /> inside <activity> in manifest file while it has to be inside <application>.
in short your manifest file should look like
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.app"> <application android:label="label" android:name="${applicationName}" android:icon="@mipmap/ic_launcher"> <activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize"> ... </activity> <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-xxxxxxxxxxxxxxxx~xxxxxxxxxx" /> </application> </manifest>The question is not about android, so the answer is irrelevant.
I faced this issue in android build as my client was asking for android only. I just wrote how I fixed this issue. One may get a clue and set the configuration
Thanks for your answer, it helped me a lot. By adding the following code snippet to the manifest file: the app no longer crashes and is working fine.
For iOS: Open the Info.plist file (ios->Runner-> Info.plist) and add this key value
<key>GADApplicationIdentifier</key> <string>ca-app-pub-YOUR_AD_MOB_APP_ID</string>For Android: Open the AddroidManifest.xml file and add this meta data inside the application tag.
<application <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-YOUR_AD_MOB_APP_ID"/> </application>Go through this if you find something related link, Flutter App Crashes when Trying to Load Interstitial Ad