简体   繁体   中英

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. Everything else is working fine but when I add this flutter package firebase_admob: ^0.9.0+10 the app crashes on startup.

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:

<!-- 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.

<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.

您的应用 ID 不正确 android:value="ca-app-pub-3940256099942544/6300978111" <--- 那是广告单元 ID 。

I have managed to solve the issue by using firebase_admob 0.5.3.

In the pubspec.yaml file add the following:

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:

<!-- 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. It is showing how you can implement admob ads in your app properly.

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(),
    );
  }
}


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM