googleads / googleads-mobile-flutter

A Flutter plugin for the Google Mobile Ads SDK
Apache License 2.0
336 stars 276 forks source link

Could not find a GoogleMobileAdsPlugin instance. The plugin may have not been registered. #436

Closed BrianTum closed 2 years ago

BrianTum commented 2 years ago

Plugin Version : google_mobile_ads: ^1.0.0

Steps to Reproduce

Expected results: Just wanted to run and launch the app

Actual results: But app crashed immediately it launches with an error GoogleMobileAdsPlugin instance not found -->

Logs However I have initialized the mobileads SDK in the main.dart ``` void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); await MobileAds.instance.initialize(); Directory document = await getApplicationDocumentsDirectory(); Hive.init(document.path); await Hive.openBox("storyCACHE"); runApp(MyApp()); } ``` Running your application with `flutter run --verbose`. ``` [+5301 ms] E/AndroidRuntime(23382): FATAL EXCEPTION: main [ ] E/AndroidRuntime(23382): Process: com.picmoni, PID: 23382 [ ] E/AndroidRuntime(23382): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.picmoni/com.picmoni.MainActivity}: java.lang.IllegalStateException: Could not find a GoogleMobileAdsPlugin instance. The plugin may have not been registered. [ +1 ms] E/AndroidRuntime(23382): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3827) [ ] E/AndroidRuntime(23382): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4003) [ ] E/AndroidRuntime(23382): at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) [ ] E/AndroidRuntime(23382): at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) [ ] E/AndroidRuntime(23382): at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) [ ] E/AndroidRuntime(23382): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2317) [ ] E/AndroidRuntime(23382): at android.os.Handler.dispatchMessage(Handler.java:106) [ +2 ms] E/AndroidRuntime(23382): at android.os.Looper.loop(Looper.java:246) [ +5 ms] E/AndroidRuntime(23382): at android.app.ActivityThread.main(ActivityThread.java:8595) [ ] E/AndroidRuntime(23382): at java.lang.reflect.Method.invoke(Native Method) [ ] E/AndroidRuntime(23382): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) [ ] E/AndroidRuntime(23382): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) [ +1 ms] E/AndroidRuntime(23382): Caused by: java.lang.IllegalStateException: Could not find a GoogleMobileAdsPlugin instance. The plugin may have not been registered. [ +1 ms] E/AndroidRuntime(23382): at io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin.registerNativeAdFactory(GoogleMobileAdsPlugin.java:176) [ ] E/AndroidRuntime(23382): at io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin.registerNativeAdFactory(GoogleMobileAdsPlugin.java:123) [ ] E/AndroidRuntime(23382): at com.picmoni.MainActivity.configureFlutterEngine(MainActivity.java:13) [ +2 ms] E/AndroidRuntime(23382): at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onAttach(FlutterActivityAndFragmentDelegate.java:187) [ ] E/AndroidRuntime(23382): at io.flutter.embedding.android.FlutterActivity.onCreate(FlutterActivity.java:430) [ ] E/AndroidRuntime(23382): at android.app.Activity.performCreate(Activity.java:8207) [ ] E/AndroidRuntime(23382): at android.app.Activity.performCreate(Activity.java:8191) [ ] E/AndroidRuntime(23382): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) [ +1 ms] E/AndroidRuntime(23382): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3800) [ ] E/AndroidRuntime(23382): ... 11 more ``` Run `flutter analyze` ``` No issues found! (ran in 121.1s) ``` Output of running `flutter doctor -v` ``` [√] Flutter (Channel stable, 2.5.3, on Microsoft Windows [Version 10.0.19043.1348], locale en-US) • Flutter version 2.5.3 at C:\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 18116933e7 (5 weeks ago), 2021-10-15 10:46:35 -0700 • Engine revision d3ea636dc5 • Dart version 2.14.4 [√] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at C:\Users\user\AppData\Local\Android\sdk • Platform android-31, build-tools 31.0.0 • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189) • All Android licenses accepted. [√] Chrome - develop for the web • Chrome at C:\Users\user\AppData\Local\Google\Chrome\Application\chrome.exe [√] Android Studio (version 2020.3) • Android Studio at C:\Program Files\Android\Android Studio • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189) [√] VS Code (version 1.62.2) • VS Code at C:\Users\user\AppData\Local\Programs\Microsoft VS Code • Flutter extension version 3.28.0 [√] Connected device (3 available) • SM M625F (mobile) • 192.168.8.115:5555 • android-arm64 • Android 11 (API 30) • Chrome (web) • chrome • web-javascript • Google Chrome 95.0.4638.69 • Edge (web) • edge • web-javascript • Microsoft Edge 92.0.902.78 • No issues found! ```
maheshj01 commented 2 years ago

Hi @BrianTum, Thanks for filing the issue. I tried running the sample app and I was able to build and run the app using the google_mobile_ads: ^ 1.0.0 on the stable channel. Please share the minimal and complete code sample to investigate further.

flutter doctor -v ``` [✓] Flutter (Channel stable, 2.5.3, on macOS 12.0.1 21A559 darwin-arm, locale en-GB) • Flutter version 2.5.3 at /Users/mahesh/Documents/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 18116933e7 (5 weeks ago), 2021-10-15 10:46:35 -0700 • Engine revision d3ea636dc5 • Dart version 2.14.4 [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at /Users/mahesh/Library/Android/sdk • Platform android-31, build-tools 31.0.0 • ANDROID_HOME = /Users/mahesh/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 13.1, Build version 13A1030d • CocoaPods version 1.10.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 4.2) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264) [✓] IntelliJ IDEA Community Edition (version 2021.2.1) • IntelliJ at /Applications/IntelliJ IDEA CE.app • Flutter plugin version 60.1.4 • Dart plugin version 212.5080.8 [✓] VS Code (version 1.61.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.28.0 [✓] Connected device (4 available) • Redmi K20 Pro (mobile) • d9dac45d • android-arm64 • Android 11 (API 30) • iPhone 12 (mobile) • 19EA5268-C931-4C3A-ACC6-71D3E849C450 • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator) • macOS (desktop) • macos • darwin-arm64 • macOS 12.0.1 21A559 darwin-arm • Chrome (web) • chrome • web-javascript • Google Chrome 96.0.4664.45 • No issues found! ```
code sample ```dart import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:google_mobile_ads/google_mobile_ads.dart'; Future main() async { WidgetsFlutterBinding.ensureInitialized(); // setting prefered orientations SystemChrome.setPreferredOrientations([ DeviceOrientation.portraitUp, ]); await MobileAds.instance.initialize(); runApp(const BannerApp()); } class BannerApp extends StatelessWidget { const BannerApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return const MaterialApp(home: CenteredBanner()); } } class CenteredBanner extends StatefulWidget { const CenteredBanner({Key? key}) : super(key: key); @override _CenteredBannerState createState() => _CenteredBannerState(); } class _CenteredBannerState extends State { late BannerAd _bannerAd; bool isAdloaded = false; @override void initState() { super.initState(); _bannerAd = BannerAd( adUnitId: BannerAd.testAdUnitId, size: AdSize.mediumRectangle, request: AdRequest(), listener: BannerAdListener( onAdLoaded: (Ad ad) { print('$BannerAd loaded.'); setState(() { isAdloaded = true; }); }, ), ); _bannerAd.load(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Banner Ad'), ), body: SizedBox( height: 50, child: AdWidget( ad: BannerAd( adUnitId: BannerAd.testAdUnitId, request: AdRequest(), size: AdSize.banner, listener: BannerAdListener(), )..load(), ), )); } } ```
pav3lo commented 2 years ago

Must to add GeneratedPluginRegistrant.registerWith(flutterEngine); before nativeadfactoryy:

override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
    GeneratedPluginRegistrant.registerWith(flutterEngine); <-----

    val factory: NativeAdFactory = NativeAdFactoryExample(layoutInflater)
    GoogleMobileAdsPlugin.registerNativeAdFactory(flutterEngine, "adFactoryExample", factory)
github-actions[bot] commented 2 years ago

Without additional information, we are unfortunately not sure how to resolve this issue. We are therefore reluctantly going to close this bug for now. If you find this problem please file a new issue with the same description, what happens, logs and the output of 'flutter doctor -v'. All system setups can be slightly different so it's always better to open new issues and reference the related ones. Thanks for your contribution.