Purpose and Usage of `GeneratedPluginRegistrant.class` when developing Native Plugin

Viewed 12775

I'm currently building a Plugin to make use of native API's on Android and iOS.

I've written MyPlugin which implements MethodCallHandler. In my plugin project, I have an example app. This example app has a generated class GeneratedPluginRegistrant.class, which calls the static method registerWith. All of this is used as a one liner in the MainActivity.

I have several questions.

  1. It seems that MyPlugin must match the name of the plugin project. IE - project is flutter_tools, my plugin must be named FlutterToolsPlugin, otherwise GeneratedPluginRegistrant will break. How is GeneratedPluginRegistrant generated?

  2. What is the purpose of GeneratedPluginRegistrant?

  3. What is actually happening to my plugin when it is registered?

  4. If I want to use MyPlugin, can it be done so without the GeneratedPluginRegistrant? I wish to respond to lifecycle methods like onActivityResult, and it seems this encapsulation prevents me from interacting with the plugin directly in the FlutterActivity.

  5. Can I interact with other native plugins from my own on the native side without having to go back and forth through Dart?

1 Answers

The GeneratedPluginRegistrant is automatically generated by a FlutterApplication in order to register plugins defined in your Flutter App's pubspec.yaml file. By providing an instance of PluginRegistry like FlutterActivity to the GeneratedPluginRegistrant#registerWith function you enable interactions between your Flutter Plugins and your Android APIs. see @PluginRegistry Documentation

Related