This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Register plugins at the right time, once #15979
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,9 @@ | |
import io.flutter.plugin.platform.PlatformPlugin; | ||
import io.flutter.view.FlutterMain; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.DART_ENTRYPOINT_META_DATA_KEY; | ||
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.DEFAULT_BACKGROUND_MODE; | ||
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.DEFAULT_DART_ENTRYPOINT; | ||
|
@@ -873,14 +876,18 @@ public PlatformPlugin providePlatformPlugin(@Nullable Activity activity, @NonNul | |
} | ||
|
||
/** | ||
* Hook for subclasses to easily configure a {@code FlutterEngine}, e.g., register | ||
* plugins. | ||
* <p> | ||
* This method is called after {@link #provideFlutterEngine(Context)}. | ||
* Hook for subclasses to easily configure a {@code FlutterEngine}. | ||
* | ||
* <p>This method is called after {@link #provideFlutterEngine(Context)}. | ||
* | ||
* <p>All plugins listed in the app's pubspec are registered in the base implementation of this | ||
* method. To avoid automatic plugin registration, override this method without invoking super(). | ||
* To keep automatic plugin registration and further configure the flutterEngine, override this | ||
* method, invoke super(), and then configure the flutterEngine as desired. | ||
*/ | ||
@Override | ||
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) { | ||
// No-op. Hook for subclasses. | ||
registerPlugins(flutterEngine); | ||
} | ||
|
||
/** | ||
|
@@ -950,4 +957,28 @@ public void onFlutterUiNoLongerDisplayed() { | |
// no-op | ||
} | ||
|
||
/** | ||
* Registers all plugins that an app lists in its pubspec.yaml. | ||
* <p> | ||
* The Flutter tool generates a class called GeneratedPluginRegistrant, which includes the code | ||
* necessary to register every plugin in the pubspec.yaml with a given {@code FlutterEngine}. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
* The GeneratedPluginRegistrant must be generated per app, because each app uses different sets | ||
* of plugins. Therefore, the Android embedding cannot place a compile-time dependency on this | ||
* generated class. This method uses reflection to attempt to locate the generated file and then | ||
* use it at runtime. | ||
* <p> | ||
* 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. | ||
*/ | ||
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."); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
shell/platform/android/test/io/flutter/plugins/GeneratedPluginRegistrant.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.flutter.plugins; | ||
|
||
import android.support.annotation.VisibleForTesting; | ||
import io.flutter.embedding.engine.FlutterEngine; | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
|
||
/** | ||
* A fake of the {@code GeneratedPluginRegistrant} normally built by the tool into Flutter apps. | ||
* | ||
* <p>Used to test engine logic which interacts with the generated class. | ||
*/ | ||
@VisibleForTesting | ||
public class GeneratedPluginRegistrant { | ||
private static final List<FlutterEngine> registeredEngines = new ArrayList<>(); | ||
|
||
/** | ||
* The one and only method currently generated by the tool. | ||
* | ||
* <p>Normally it registers all plugins in an app with the given {@code engine}. This fake tracks | ||
* all registered engines instead. | ||
*/ | ||
public static void registerWith(FlutterEngine engine) { | ||
registeredEngines.add(engine); | ||
} | ||
|
||
/** | ||
* Clears the mutable static state regrettably stored in this class. | ||
* | ||
* <p>{@link #registerWith} is a static call with no visible side effects. In order to verify when | ||
* it's been called we also unfortunately need to store the state statically. This should be | ||
* called before and after each test run accessing this class to make sure the state is clear both | ||
* before and after the run. | ||
*/ | ||
public static void clearRegisteredEngines() { | ||
registeredEngines.clear(); | ||
} | ||
|
||
/** | ||
* Returns a list of all the engines registered so far. | ||
* | ||
* <p>CAUTION: This list is static and must be manually wiped in between test runs. See | ||
* {@link #clearRegisteredEngines()}. | ||
*/ | ||
public static List<FlutterEngine> getRegisteredEngines() { | ||
return new ArrayList<>(registeredEngines); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor nit: plugins that support the android platform
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that would be redundant to point out here. This is already a Javadoc comment in the Android embedding specifically and not a generic part of the engine's C++ source code, so I don't think it's worth pointing out that this logic is Android-only. If you feel strongly about it I can update it though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not at all. 🙂Although, if someone wants to understand why a plugin isn't added to the
GeneratedPluginRegistrant
, the comment might be useful.The current definition of supporting the Android platform is a plugin that contains
android/build.gradle
.https://api.flutter.dev/javadoc/io/flutter/app/FlutterActivity.html.