Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GeneratedPluginRegister.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package io.flutter.embedding.engine.plugins.util;
6
7import androidx.annotation.NonNull;
8import io.flutter.Log;
9import io.flutter.embedding.engine.FlutterEngine;
10import java.lang.reflect.Method;
11
13 private static final String TAG = "GeneratedPluginsRegister";
14 /**
15 * Registers all plugins that an app lists in its pubspec.yaml.
16 *
17 * <p>In order to allow each plugin to listen to calls from Dart via Platform Channels, each
18 * plugin is given a chance to initialize and set up Platform Channel listeners in {@link
19 * io.flutter.embedding.engine.plugins.FlutterPlugin#onAttachedToEngine(io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding)}.
20 *
21 * <p>The list of plugins that need to be set up is not known to the Flutter engine. The Flutter
22 * tools generates it at build time based on the plugin dependencies specified in the Flutter
23 * project's pubspec.yaml.
24 *
25 * <p>The Flutter tools generates that list in a class called {@code GeneratedPluginRegistrant}.
26 * That class contains generated code to register every plugin in the pubspec.yaml with a {@link
27 * FlutterEngine}. That code's file is generated in the Flutter project's directory.
28 *
29 * <p>In a normal full-Flutter application, the {@link
30 * io.flutter.embedding.android.FlutterActivity} will automatically call the {@code
31 * GeneratedPluginRegistrant} to register all plugins. In a typical full-Flutter application, the
32 * {@link io.flutter.embedding.android.FlutterActivity} creates a {@link
33 * io.flutter.embedding.engine.FlutterEngine}. When a {@link
34 * io.flutter.embedding.engine.FlutterEngine} is explicitly created, it automatically registers
35 * plugins during its construction.
36 *
37 * <p>Since the {@link io.flutter.embedding.engine.FlutterEngine} belongs to the Flutter engine
38 * and the GeneratedPluginRegistrant class belongs to the app project, the {@link
39 * io.flutter.embedding.engine.FlutterEngine} cannot place a compile-time dependency on
40 * GeneratedPluginRegistrant to invoke it. Instead, this class uses reflection to attempt to
41 * locate the generated file and then uses it at runtime.
42 *
43 * <p>This method fizzles if the GeneratedPluginRegistrant cannot be found or invoked. This
44 * situation should never occur, but if any eventuality comes up that prevents an app from using
45 * this behavior, that app can still write code that explicitly registers plugins.
46 *
47 * <p>To disable this automatic plugin registration behavior:
48 *
49 * <ul>
50 * <li>If you manually construct {@link io.flutter.embedding.engine.FlutterEngine}s, construct
51 * the {@link io.flutter.embedding.engine.FlutterEngine} with the {@code
52 * automaticallyRegisterPlugins} construction parameter set to false.
53 * <li>If you let the {@link io.flutter.embedding.android.FlutterActivity} or {@link
54 * io.flutter.embedding.android.FlutterFragmentActivity} construct a {@link
55 * io.flutter.embedding.engine.FlutterEngine} implicitly, override the {@code
56 * configureFlutterEngine} implementation and don't call through to the superclass
57 * implementation.
58 * </ul>
59 *
60 * <p>Disabling the automatic plugin registration or deferring it by calling this method
61 * explicitly may be useful in fine tuning the application launch latency characteristics for your
62 * application.
63 *
64 * <p>It's also possible to not use GeneratedPluginRegistrant and this method at all in order to
65 * fine tune not only when plugins are registered but which plugins are registered when.
66 * Inspecting the content of the GeneratedPluginRegistrant class will reveal that it's just going
67 * through each of the plugins referenced in pubspec.yaml and calling {@link
68 * io.flutter.embedding.engine.plugins.FlutterPlugin#onAttachedToEngine(io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding)}
69 * on each plugin. That code can be copy pasted and invoked directly per plugin to determine which
70 * plugin gets registered when in your own application's code. Note that when that's done without
71 * using the GeneratedPluginRegistrant, updating pubspec.yaml will no longer automatically update
72 * the list of plugins being registered.
73 */
74 public static void registerGeneratedPlugins(@NonNull FlutterEngine flutterEngine) {
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 }
90}
static void e(@NonNull String tag, @NonNull String message)
Definition Log.java:84
static void registerGeneratedPlugins(@NonNull FlutterEngine flutterEngine)
#define TAG()