我正在尝试在我的应用程序中添加admob广告。其他一切都很好,但是当我添加这个颤振包火力基地时,应用程序就会在启动时崩溃。
该包的文档坚持使用以下信息修改meta部分中的文件,但当我这样做时,应用程序仍然崩溃:
<!-- Below is the default meta-data and default commenting statement explaining the meta-data section -->
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />正如firebase-admob包文档所解释的那样,我已经用下面的元数据替换了上面的元数据。
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544/6300978111"/>
<!-- I'm using sample ad unit id for banner provided here "https://developers.google.com/admob/android/test-ads" -->下面是我遇到的错误:
e: C:\Users\current user\Documents\mobile development\simple_app\android\app\src\main\kotlin\com\example\simple_app\MainActivity.kt: (10, 48): Type mismatch: inferred type is FlutterEngine but PluginRegistry! was expected
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 16s
Finished with error: Gradle task assembleDebug failed with exit code 1我不知道我为什么要犯这个错误。我尝试过不同的方法和类似的admob包,但类似的错误不断发生。
发布于 2019-12-27 14:53:32
您的应用程序id是不正确的android:value="ca-app-pub-3940256099942544/6300978111“<--这是一个广告单元id。
发布于 2019-12-28 09:11:22
我通过使用firebase_admob 0.5.3成功地解决了这个问题。
在pubspec.yaml文件中添加以下内容:
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
firebase_admob: 0.5.3 // add this dependency在AndroidManifest.xml文件中,元数据部分应该如下所示:
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
// Notice that I did not change anything on the meta-data tag this time.这是main.dart文件。它展示了如何正确地在应用程序中实现admob广告。
import 'package:flutter/material.dart';
import 'package:my_app/home.dart';
import 'package:firebase_admob/firebase_admob.dart';
const appId = 'ca-app-pub-XXXXXXXXXX~XXXXXXXXXX';
const bannerId = 'ca-app-pub-XXXXXXXXXX/XXXXXXXXX';
const insterstitialId = 'ca-app-pub-XXXXXXXXX/XXXXXXXX';
MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
keywords: <String>['flutterio', 'beautiful apps'],
contentUrl: 'https://flutter.io',
birthday: DateTime.now(),
childDirected: false,
designedForFamilies: false,
gender: MobileAdGender.unknown, // or MobileAdGender.female, MobileAdGender.unknown
testDevices: <String>[], // Android emulators are considered test devices
);
BannerAd myBanner = BannerAd(
// Replace the testAdUnitId with an ad unit id from the AdMob dash.
// https://developers.google.com/admob/android/test-ads
// https://developers.google.com/admob/ios/test-ads
adUnitId: BannerAd.testAdUnitId,
size: AdSize.smartBanner,
targetingInfo: targetingInfo,
listener: (MobileAdEvent event) {
print("BannerAd event is $event");
},
);
void main() {
runApp(FlashlightApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
FirebaseAdMob.instance.initialize(appId: appId);
super.initState();
}
@override
Widget build(BuildContext context) {
myBanner
// typically this happens well before the ad is shown
..load()
..show(
// Positions the banner ad 60 pixels from the bottom of the screen
anchorOffset: 20.0,
// Banner Position
anchorType: AnchorType.bottom,
);
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}https://stackoverflow.com/questions/59463106
复制相似问题