From 215e774f79c77570d598777a00c9a9583cf8bb9a Mon Sep 17 00:00:00 2001 From: Sarbagya Dhaubanjar Date: Mon, 22 Jun 2020 11:45:29 +0545 Subject: [PATCH 1/2] Automatically register plugins for `FlutterFragmentActivity` Follow up of flutter#15979 --- .../android/FlutterFragmentActivity.java | 42 +++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java b/shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java index 92d7586a32a70..f4184d91111c1 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java @@ -41,6 +41,7 @@ import io.flutter.embedding.engine.FlutterShellArgs; import io.flutter.plugin.platform.PlatformPlugin; import io.flutter.view.FlutterMain; +import java.lang.reflect.Method; /** * A Flutter {@code Activity} that is based upon {@link FragmentActivity}. @@ -554,14 +555,19 @@ public FlutterEngine provideFlutterEngine(@NonNull Context context) { return null; } - /** - * Hook for subclasses to easily configure a {@code FlutterEngine}, e.g., register plugins. + /** + * Hook for subclasses to easily configure a {@code FlutterEngine}. * *

This method is called after {@link #provideFlutterEngine(Context)}. + * + *

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); } /** @@ -698,4 +704,34 @@ protected BackgroundMode getBackgroundMode() { private boolean isDebuggable() { return (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; } + + /** + * Registers all plugins that an app lists in its pubspec.yaml. + * + *

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}. 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. + * + *

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."); + } + } } From d90a5ee1dac0a0f69bb3e9d67afd31b36f362544 Mon Sep 17 00:00:00 2001 From: Sarbagya Dhaubanjar Date: Mon, 22 Jun 2020 12:14:22 +0545 Subject: [PATCH 2/2] fix format test --- .../io/flutter/embedding/android/FlutterFragmentActivity.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java b/shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java index f4184d91111c1..82b64d696f13e 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java @@ -555,7 +555,7 @@ public FlutterEngine provideFlutterEngine(@NonNull Context context) { return null; } - /** + /** * Hook for subclasses to easily configure a {@code FlutterEngine}. * *

This method is called after {@link #provideFlutterEngine(Context)}. @@ -704,7 +704,7 @@ protected BackgroundMode getBackgroundMode() { private boolean isDebuggable() { return (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; } - + /** * Registers all plugins that an app lists in its pubspec.yaml. *