[英]Whenever I add a certain library and modify androidmanifest.xml on meta-data section, flutter app keeps crashing
I'm trying to add admob ads in my app.我正在尝试在我的应用中添加 admob 广告。 Everything else is working fine but when I add this flutter package firebase_admob: ^0.9.0+10 the app crashes on startup.
其他一切正常,但是当我添加这个 flutter 包firebase_admob: ^0.9.0+10 时,应用程序在启动时崩溃。
The documentation for this package insists that I modify the AndroidManifest.xml file on the meta-data section with the following info, but when I do so, the app still crashes:该包的文档坚持要求我使用以下信息修改元数据部分上的AndroidManifest.xml文件,但是当我这样做时,应用程序仍然崩溃:
<!-- 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" />
I have replaced the above meta-data with the meta-data below as the firebase-admob package documentation is explaining.正如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" -->
Below is the error I'm getting:下面是我得到的错误:
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
I don't know why I'm getting this error.我不知道为什么会出现此错误。 I have tried different approaches and similar admob packages but similar errors keep occurring.
我尝试了不同的方法和类似的 admob 包,但类似的错误不断发生。
您的应用 ID 不正确 android:value="ca-app-pub-3940256099942544/6300978111" <--- 那是广告单元 ID 。
I have managed to solve the issue by using firebase_admob 0.5.3.我设法通过使用 firebase_admob 0.5.3 解决了这个问题。
In the pubspec.yaml file add the following:在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
In the AndroidManifest.xml file, on the meta-data section it should be like:在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.
This is the main.dart file.这是main.dart文件。 It is showing how you can implement admob ads in your app properly.
它展示了如何在您的应用中正确实施 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(),
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.