Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Static Public Member Functions | List of all members
io.flutter.embedding.engine.plugins.util.GeneratedPluginRegister Class Reference

Static Public Member Functions

static void registerGeneratedPlugins (@NonNull FlutterEngine flutterEngine)
 

Detailed Description

Definition at line 12 of file GeneratedPluginRegister.java.

Member Function Documentation

◆ registerGeneratedPlugins()

static void io.flutter.embedding.engine.plugins.util.GeneratedPluginRegister.registerGeneratedPlugins ( @NonNull FlutterEngine  flutterEngine)
inlinestatic

Registers all plugins that an app lists in its pubspec.yaml.

In order to allow each plugin to listen to calls from Dart via Platform Channels, each plugin is given a chance to initialize and set up Platform Channel listeners in io.flutter.embedding.engine.plugins.FlutterPlugin#onAttachedToEngine(io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding).

The list of plugins that need to be set up is not known to the Flutter engine. The Flutter tools generates it at build time based on the plugin dependencies specified in the Flutter project's pubspec.yaml.

The Flutter tools generates that list in a class called GeneratedPluginRegistrant. That class contains generated code to register every plugin in the pubspec.yaml with a FlutterEngine. That code's file is generated in the Flutter project's directory.

In a normal full-Flutter application, the io.flutter.embedding.android.FlutterActivity will automatically call the
GeneratedPluginRegistrant
to register all plugins. In a typical full-Flutter application, the io.flutter.embedding.android.FlutterActivity creates a io.flutter.embedding.engine.FlutterEngine. When a io.flutter.embedding.engine.FlutterEngine is explicitly created, it automatically registers plugins during its construction.

Since the io.flutter.embedding.engine.FlutterEngine belongs to the Flutter engine and the GeneratedPluginRegistrant class belongs to the app project, the io.flutter.embedding.engine.FlutterEngine cannot place a compile-time dependency on GeneratedPluginRegistrant to invoke it. Instead, this class uses reflection to attempt to locate the generated file and then uses it at runtime.

This method fizzles if the GeneratedPluginRegistrant cannot be found or invoked. This situation should never occur, but if any eventuality comes up that prevents an app from using this behavior, that app can still write code that explicitly registers plugins.

To disable this automatic plugin registration behavior:

Disabling the automatic plugin registration or deferring it by calling this method explicitly may be useful in fine tuning the application launch latency characteristics for your application.

It's also possible to not use GeneratedPluginRegistrant and this method at all in order to fine tune not only when plugins are registered but which plugins are registered when. Inspecting the content of the GeneratedPluginRegistrant class will reveal that it's just going through each of the plugins referenced in pubspec.yaml and calling io.flutter.embedding.engine.plugins.FlutterPlugin#onAttachedToEngine(io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding) on each plugin. That code can be copy pasted and invoked directly per plugin to determine which plugin gets registered when in your own application's code. Note that when that's done without using the GeneratedPluginRegistrant, updating pubspec.yaml will no longer automatically update the list of plugins being registered.

Definition at line 74 of file GeneratedPluginRegister.java.

74 {
75 try {
76 Class<?> generatedPluginRegistrant =
77 Class.forName("io.flutter.plugins.GeneratedPluginRegistrant");
78 Method registrationMethod =
79 generatedPluginRegistrant.getDeclaredMethod("registerWith", FlutterEngine.class);
80 registrationMethod.invoke(null, flutterEngine);
81 } catch (Exception e) {
82 Log.e(
83 TAG,
84 "Tried to automatically register plugins with FlutterEngine ("
85 + flutterEngine
86 + ") but could not find or invoke the GeneratedPluginRegistrant.");
87 Log.e(TAG, "Received exception while registering", e);
88 }
89 }
void Log(const char *format,...) SK_PRINTF_LIKE(1

The documentation for this class was generated from the following file: