Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 481e055

Browse files
committed
extract into helper class
1 parent b50d4b2 commit 481e055

File tree

4 files changed

+49
-64
lines changed

4 files changed

+49
-64
lines changed

shell/platform/android/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ android_java_sources = [
177177
"io/flutter/embedding/engine/plugins/service/ServicePluginBinding.java",
178178
"io/flutter/embedding/engine/plugins/shim/ShimPluginRegistry.java",
179179
"io/flutter/embedding/engine/plugins/shim/ShimRegistrar.java",
180+
"io/flutter/embedding/engine/plugins/util/GeneratedPluginRegister.java",
180181
"io/flutter/embedding/engine/renderer/FlutterRenderer.java",
181182
"io/flutter/embedding/engine/renderer/FlutterUiDisplayListener.java",
182183
"io/flutter/embedding/engine/renderer/RenderSurface.java",

shell/platform/android/io/flutter/embedding/android/FlutterActivity.java

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@
4242
import io.flutter.embedding.engine.FlutterEngine;
4343
import io.flutter.embedding.engine.FlutterShellArgs;
4444
import io.flutter.embedding.engine.plugins.activity.ActivityControlSurface;
45+
import io.flutter.embedding.engine.plugins.util.GeneratedPluginRegister;
4546
import io.flutter.plugin.platform.PlatformPlugin;
4647
import io.flutter.view.FlutterMain;
47-
import java.lang.reflect.Method;
4848

4949
/**
5050
* {@code Activity} which displays a fullscreen Flutter UI.
@@ -872,7 +872,7 @@ public PlatformPlugin providePlatformPlugin(
872872
*/
873873
@Override
874874
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
875-
registerPlugins(flutterEngine);
875+
GeneratedPluginRegister.registerGeneratedPlugins(flutterEngine);
876876
}
877877

878878
/**
@@ -962,34 +962,4 @@ public boolean shouldRestoreAndSaveState() {
962962
}
963963
return true;
964964
}
965-
966-
/**
967-
* Registers all plugins that an app lists in its pubspec.yaml.
968-
*
969-
* <p>The Flutter tool generates a class called GeneratedPluginRegistrant, which includes the code
970-
* necessary to register every plugin in the pubspec.yaml with a given {@code FlutterEngine}. The
971-
* GeneratedPluginRegistrant must be generated per app, because each app uses different sets of
972-
* plugins. Therefore, the Android embedding cannot place a compile-time dependency on this
973-
* generated class. This method uses reflection to attempt to locate the generated file and then
974-
* use it at runtime.
975-
*
976-
* <p>This method fizzles if the GeneratedPluginRegistrant cannot be found or invoked. This
977-
* situation should never occur, but if any eventuality comes up that prevents an app from using
978-
* this behavior, that app can still write code that explicitly registers plugins.
979-
*/
980-
private static void registerPlugins(@NonNull FlutterEngine flutterEngine) {
981-
try {
982-
Class<?> generatedPluginRegistrant =
983-
Class.forName("io.flutter.plugins.GeneratedPluginRegistrant");
984-
Method registrationMethod =
985-
generatedPluginRegistrant.getDeclaredMethod("registerWith", FlutterEngine.class);
986-
registrationMethod.invoke(null, flutterEngine);
987-
} catch (Exception e) {
988-
Log.w(
989-
TAG,
990-
"Tried to automatically register plugins with FlutterEngine ("
991-
+ flutterEngine
992-
+ ") but could not find and invoke the GeneratedPluginRegistrant.");
993-
}
994-
}
995965
}

shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
import io.flutter.embedding.android.FlutterActivityLaunchConfigs.BackgroundMode;
4040
import io.flutter.embedding.engine.FlutterEngine;
4141
import io.flutter.embedding.engine.FlutterShellArgs;
42+
import io.flutter.embedding.engine.plugins.util.GeneratedPluginRegister;
4243
import io.flutter.plugin.platform.PlatformPlugin;
4344
import io.flutter.view.FlutterMain;
44-
import java.lang.reflect.Method;
4545

4646
/**
4747
* A Flutter {@code Activity} that is based upon {@link FragmentActivity}.
@@ -566,7 +566,7 @@ public FlutterEngine provideFlutterEngine(@NonNull Context context) {
566566
*/
567567
@Override
568568
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
569-
registerPlugins(flutterEngine);
569+
GeneratedPluginRegister.registerGeneratedPlugins(flutterEngine);
570570
}
571571

572572
/**
@@ -716,34 +716,4 @@ protected RenderMode getRenderMode() {
716716
private boolean isDebuggable() {
717717
return (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
718718
}
719-
720-
/**
721-
* Registers all plugins that an app lists in its pubspec.yaml.
722-
*
723-
* <p>The Flutter tool generates a class called GeneratedPluginRegistrant, which includes the code
724-
* necessary to register every plugin in the pubspec.yaml with a given {@code FlutterEngine}. The
725-
* GeneratedPluginRegistrant must be generated per app, because each app uses different sets of
726-
* plugins. Therefore, the Android embedding cannot place a compile-time dependency on this
727-
* generated class. This method uses reflection to attempt to locate the generated file and then
728-
* use it at runtime.
729-
*
730-
* <p>This method fizzles if the GeneratedPluginRegistrant cannot be found or invoked. This
731-
* situation should never occur, but if any eventuality comes up that prevents an app from using
732-
* this behavior, that app can still write code that explicitly registers plugins.
733-
*/
734-
private static void registerPlugins(@NonNull FlutterEngine flutterEngine) {
735-
try {
736-
Class<?> generatedPluginRegistrant =
737-
Class.forName("io.flutter.plugins.GeneratedPluginRegistrant");
738-
Method registrationMethod =
739-
generatedPluginRegistrant.getDeclaredMethod("registerWith", FlutterEngine.class);
740-
registrationMethod.invoke(null, flutterEngine);
741-
} catch (Exception e) {
742-
Log.w(
743-
TAG,
744-
"Tried to automatically register plugins with FlutterEngine ("
745-
+ flutterEngine
746-
+ ") but could not find and invoke the GeneratedPluginRegistrant.");
747-
}
748-
}
749719
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
5+
package io.flutter.embedding.engine.plugins.util;
6+
7+
import java.lang.reflect.Method;
8+
9+
import androidx.annotation.NonNull;
10+
import io.flutter.Log;
11+
import io.flutter.embedding.engine.FlutterEngine;
12+
13+
public class GeneratedPluginRegister {
14+
private static final String TAG = "GeneratedPluginsRegister";
15+
/**
16+
* Registers all plugins that an app lists in its pubspec.yaml.
17+
*
18+
* <p>The Flutter tool generates a class called GeneratedPluginRegistrant, which includes the code
19+
* necessary to register every plugin in the pubspec.yaml with a given {@code FlutterEngine}. The
20+
* GeneratedPluginRegistrant must be generated per app, because each app uses different sets of
21+
* plugins. Therefore, the Android embedding cannot place a compile-time dependency on this
22+
* generated class. This method uses reflection to attempt to locate the generated file and then
23+
* use it at runtime.
24+
*
25+
* <p>This method fizzles if the GeneratedPluginRegistrant cannot be found or invoked. This
26+
* situation should never occur, but if any eventuality comes up that prevents an app from using
27+
* this behavior, that app can still write code that explicitly registers plugins.
28+
*/
29+
public static void registerGeneratedPlugins(@NonNull FlutterEngine flutterEngine) {
30+
try {
31+
Class<?> generatedPluginRegistrant =
32+
Class.forName("io.flutter.plugins.GeneratedPluginRegistrant");
33+
Method registrationMethod =
34+
generatedPluginRegistrant.getDeclaredMethod("registerWith", FlutterEngine.class);
35+
registrationMethod.invoke(null, flutterEngine);
36+
} catch (Exception e) {
37+
Log.w(
38+
TAG,
39+
"Tried to automatically register plugins with FlutterEngine ("
40+
+ flutterEngine
41+
+ ") but could not find and invoke the GeneratedPluginRegistrant.");
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)