After adding google_mobile_ads package in my flutter app the application crashes without showing anything.
I have added the required text in the androidManifest file.
androidManifest.xml file:
<application
android:label="ad_prac"
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">
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544/6300978111"/>
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
main.dart file:
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
final initFuture = MobileAds.instance.initialize();
final adState = AdState(initFuture);
runApp(MyApp()),
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MyHomePage();
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: 100,
itemBuilder: (context, index) {
return ListTile(
title: Text("this is $index"),
);
}),
),
],
));
}
}
i am using android emulator with google play
I have added the Meta
tag below the activity
tag and have tried other fixes but none are working
2
Answers
Update the Flutter Version latest (in Pubspec.yaml sdk version
sdk: '>=3.0.1 <4.0.0'
)then change the gradle file in project level
ext.kotlin_version = '1.7.10'
now it will work fine
You also need to add this line to the
dependencies
section of your app-level gradle file:and make sure that the
minSdkVersion
is19
or higher in the same file:AdMob – Get started