Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Tried to automatically register plugins with FlutterEngine but could not find and invoke the GeneratedPluginRegistrant

I'm getting these error while running this command

flutter run --flavor development -t lib/config/main_development.dart --verbose-system-logs

Errors:

Tried to automatically register plugins with FlutterEngine (io.flutter.embedding.engine.FlutterEngine@b5226bb) but could not find and invoke the GeneratedPluginRegistrant.
E/flutter (28896): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: MissingPluginException(No implementation found for method initialize on channel flutter_stetho)

flutter doctor output:

➜ flutter doctor -v                                                                         
[✓] Flutter (Channel master, 1.20.0-8.0.pre.22, on Mac OS X 10.14.6 18G3020, locale en)
    • Flutter version 1.20.0-8.0.pre.22 at /Users/vedantrathore/tools/flutter
    • Framework revision 61a04b1551 (11 hours ago), 2020-07-09 15:52:19 +0800
    • Engine revision 0ec6f6c3f2
    • Dart version 2.9.0 (build 2.9.0-20.0.dev 06cb010247)

 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at /Users/vedantrathore/Library/Android/sdk
    • Platform android-29, build-tools 29.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.3.1, Build version 11C504
    • CocoaPods version 1.9.1

[✓] Android Studio (version 3.6)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 44.0.2
    • Dart plugin version 192.7761
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)

[✓] VS Code (version 1.46.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.12.1

[✓] Connected device (1 available)
    • GM1901 (mobile) • 192.168.29.75:5555 • android-arm64 • Android 10 (API 29)

• No issues found!

This is my app/build.gradle file

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 28

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.habitwave"
        minSdkVersion 18
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
            minifyEnabled true
            useProguard true
        }
        debug{
            minifyEnabled true
            useProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    flavorDimensions "flutter-flavours"

    productFlavors{
        development{
            dimension "flutter-flavours"
            versionNameSuffix "-dev"
        }
        staging{
            dimension "flutter-flavours"
            versionNameSuffix "-stg"
        }
        production{
            dimension "flutter-flavours"
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    implementation 'com.google.firebase:firebase-analytics:17.2.2'
}

I'm trying to use the flutter_secure_storage package, due to which I've to set the minSdkVersion to 18 and use Proguard rules to avoid multiDex (Since that requireds minSdkVersion 21)

Solutions I've tried:

  • Running flutter clean, uninstalling app, clearing ~/.pub-cache
  • Moving to master channel from stable

EDIT:

I solved this by diving deep into this and opened the app in Android studio, went inside the implementation Flutter activity and set a breakpoint where it tries to register plugins, when I ran the application then, I found out that the error was the FacebookSdk wasn't initialised. I'm using the pub.dev/packages/flutter_facebook_login package and forgot the android initialisation. The error handling can be improved to accommodate these scenarios

Facebook SDK Initialization error

like image 915
Vedant Rathore Avatar asked Jul 09 '20 18:07

Vedant Rathore


2 Answers

I have faced the same issue today. This answer can be helpful for those who got this error while developing an Android Plugin for Flutter.

The error was because of an InvocationTargetException that happens while the plugin initialization. InvocationTargetException is thrown when the method which invoked with reflection throws an error. There wasn't any error log other than the "Tried to automatically register plugins..." Flutter warning so I have found the exception while debugging the Flutter source code first (as also done by the OP) and my code after.

This is the Flutter source code where the exception is thrown. (by Flutter 1.20.4/channel:stable)

private static void registerPlugins(@NonNull FlutterEngine flutterEngine) {
  try {
    Class<?> generatedPluginRegistrant =
        Class.forName("io.flutter.plugins.GeneratedPluginRegistrant");
    Method registrationMethod =
        generatedPluginRegistrant.getDeclaredMethod("registerWith", 
FlutterEngine.class);
    registrationMethod.invoke(null, flutterEngine);
  } catch (Exception e) {
    Log.w(
        TAG,
        "Tried to automatically register plugins with FlutterEngine ("
            + flutterEngine
            + ") but could not find and invoke the GeneratedPluginRegistrant.");
  }
}

I have added comments to methods which can cause this error. You can debug your code with Android Studio and find the error that fails the plugin initialization.

public class MyPlugin implements FlutterPlugin, ActivityAware {
    private MethodChannel methodChannel;
    private CustomMethodHandler methodHandler;
    private ActivityPluginBinding mActivityBinding;

    private void initChannels(final BinaryMessenger messenger) {
        methodChannel = new MethodChannel(messenger, Channel.MY_METHOD_CHANNEL);
        methodHandler= new CustomMethodHandler(null);
        methodChannel.setMethodCallHandler(authMethodHandler);
    }

    private void teardownChannels() {
        methodChannel.setMethodCallHandler(null);
        methodChannel = null;
    }

    private void setupActivity(Activity activity) {
        methodChannel.setActivity(activity);
    }

    private void teardownActivity() {
        methodChannel.setActivity(null);
    }

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        // Debug here for errors.
        initChannels(flutterPluginBinding.getBinaryMessenger());
    }

    public static void registerWith(Registrar registrar) {
        MyPlugin myPlugin = new MyPlugin();
        myPlugin.initChannels(registrar.messenger());
        myPlugin.setupActivity(registrar.activity());
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        teardownChannels();
    }

    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        // Debug here for errors.
        mActivityBinding = binding;
        setupActivity(mActivityBinding.getActivity());
    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {
        teardownActivity();
    }

    @Override
    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
        mActivityBinding = binding;
        setupActivity(mActivityBinding.getActivity());
    }

    @Override
    public void onDetachedFromActivity() {
        teardownActivity();
    }

}
like image 61
Ali Türkay Avci Avatar answered Oct 17 '22 05:10

Ali Türkay Avci


Today I was facing the same issue. I think it is related to this: https://flutter.dev/docs/development/packages-and-plugins/plugin-api-migration

(It might be just temporary solution) I wrote back the registrant to my MainActivity.kt - if your class body is empty it might help

package your.package.name

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }
}
like image 25
Marc Sanny Avatar answered Oct 17 '22 06:10

Marc Sanny