簡體   English   中英

每當我在元數據部分添加某個庫並修改 androidmanifest.xml 時,flutter 應用程序都會崩潰

[英]Whenever I add a certain library and modify androidmanifest.xml on meta-data section, flutter app keeps crashing

我正在嘗試在我的應用中添加 admob 廣告。 其他一切正常,但是當我添加這個 flutter 包firebase_admob: ^0.9.0+10 時,應用程序在啟動時崩潰。

該包的文檔堅持要求我使用以下信息修改元數據部分上的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" />

正如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 包,但類似的錯誤不斷發生。

您的應用 ID 不正確 android:value="ca-app-pub-3940256099942544/6300978111" <--- 那是廣告單元 ID 。

我設法通過使用 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(),
    );
  }
}


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM