Skip to content

Commit cf95cd4

Browse files
authored
Add v1 plugin register function into v2 plugin template (flutter#44166)
1 parent 7aebde1 commit cf95cd4

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed

dev/devicelab/bin/tasks/plugin_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,12 @@ Future<void> main() async {
1111
await task(combine(<TaskFunction>[
1212
PluginTest('apk', <String>['-a', 'java']),
1313
PluginTest('apk', <String>['-a', 'kotlin']),
14+
// These create the plugins using the new v2 plugin templates but create the
15+
// apps using the old v1 embedding app templates to make sure new plugins
16+
// are by default backward compatible.
17+
PluginTest('apk', <String>['-a', 'java'], pluginCreateEnvironment:
18+
<String, String>{'ENABLE_ANDROID_EMBEDDING_V2': 'true'}),
19+
PluginTest('apk', <String>['-a', 'kotlin'], pluginCreateEnvironment:
20+
<String, String>{'ENABLE_ANDROID_EMBEDDING_V2': 'true'}),
1421
]));
1522
}

dev/devicelab/lib/framework/utils.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ Future<Process> startProcess(
256256
assert(isBot != null);
257257
final String command = '$executable ${arguments?.join(" ") ?? ""}';
258258
final String finalWorkingDirectory = workingDirectory ?? cwd;
259-
print('\nExecuting: $command in $finalWorkingDirectory');
259+
print('\nExecuting: $command in $finalWorkingDirectory'
260+
+ (environment != null ? ' with environment $environment' : ''));
260261
environment ??= <String, String>{};
261262
environment['BOT'] = isBot ? 'true' : 'false';
262263
final Process process = await _processManager.start(

dev/devicelab/lib/tasks/plugin_tests.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ TaskFunction combine(List<TaskFunction> tasks) {
2626
/// Defines task that creates new Flutter project, adds a local and remote
2727
/// plugin, and then builds the specified [buildTarget].
2828
class PluginTest {
29-
PluginTest(this.buildTarget, this.options);
29+
PluginTest(this.buildTarget, this.options, { this.pluginCreateEnvironment, this.appCreateEnvironment });
3030

3131
final String buildTarget;
3232
final List<String> options;
33+
final Map<String, String> pluginCreateEnvironment;
34+
final Map<String, String> appCreateEnvironment;
3335

3436
Future<TaskResult> call() async {
3537
final Directory tempDir =
@@ -38,12 +40,12 @@ class PluginTest {
3840
section('Create plugin');
3941
final _FlutterProject plugin = await _FlutterProject.create(
4042
tempDir, options,
41-
name: 'plugintest', template: 'plugin');
43+
name: 'plugintest', template: 'plugin', environment: pluginCreateEnvironment);
4244
section('Test plugin');
4345
await plugin.test();
4446
section('Create Flutter app');
4547
final _FlutterProject app = await _FlutterProject.create(tempDir, options,
46-
name: 'plugintestapp', template: 'app');
48+
name: 'plugintestapp', template: 'app', environment: appCreateEnvironment);
4749
try {
4850
if (buildTarget == 'ios')
4951
await prepareProvisioningCertificates(app.rootPath);
@@ -95,8 +97,13 @@ class _FlutterProject {
9597
}
9698

9799
static Future<_FlutterProject> create(
98-
Directory directory, List<String> options,
99-
{String name, String template}) async {
100+
Directory directory,
101+
List<String> options,
102+
{
103+
String name,
104+
String template,
105+
Map<String, String> environment,
106+
}) async {
100107
await inDirectory(directory, () async {
101108
await flutter(
102109
'create',
@@ -107,6 +114,7 @@ class _FlutterProject {
107114
...options,
108115
name,
109116
],
117+
environment: environment,
110118
);
111119
});
112120
return _FlutterProject(directory, name);

packages/flutter_tools/templates/plugin/android-java.tmpl/src/main/java/androidIdentifier/pluginClass.java.tmpl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import io.flutter.plugin.common.MethodCall;
1212
import io.flutter.plugin.common.MethodChannel;
1313
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
1414
import io.flutter.plugin.common.MethodChannel.Result;
15+
import io.flutter.plugin.common.PluginRegistry.Registrar;
1516

1617
/** {{pluginClass}} */
1718
public class {{pluginClass}} implements FlutterPlugin, MethodCallHandler {
@@ -21,6 +22,20 @@ public class {{pluginClass}} implements FlutterPlugin, MethodCallHandler {
2122
channel.setMethodCallHandler(new {{pluginClass}}());
2223
}
2324

25+
// This static function is optional and equivalent to onAttachedToEngine. It supports the old
26+
// pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
27+
// plugin registration via this function while apps migrate to use the new Android APIs
28+
// post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
29+
//
30+
// It is encouraged to share logic between onAttachedToEngine and registerWith to keep
31+
// them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
32+
// depending on the user's project. onAttachedToEngine or registerWith must both be defined
33+
// in the same class.
34+
public static void registerWith(Registrar registrar) {
35+
final MethodChannel channel = new MethodChannel(registrar.messenger(), "{{projectName}}");
36+
channel.setMethodCallHandler(new {{pluginClass}}());
37+
}
38+
2439
@Override
2540
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
2641
if (call.method.equals("getPlatformVersion")) {

packages/flutter_tools/templates/plugin/android-kotlin.tmpl/src/main/kotlin/androidIdentifier/pluginClass.kt.tmpl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import io.flutter.plugin.common.MethodCall
1212
import io.flutter.plugin.common.MethodChannel
1313
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
1414
import io.flutter.plugin.common.MethodChannel.Result
15+
import io.flutter.plugin.common.PluginRegistry.Registrar
1516

1617
/** {{pluginClass}} */
1718
public class {{pluginClass}}: FlutterPlugin, MethodCallHandler {
@@ -20,6 +21,23 @@ public class {{pluginClass}}: FlutterPlugin, MethodCallHandler {
2021
channel.setMethodCallHandler({{pluginClass}}());
2122
}
2223

24+
// This static function is optional and equivalent to onAttachedToEngine. It supports the old
25+
// pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
26+
// plugin registration via this function while apps migrate to use the new Android APIs
27+
// post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
28+
//
29+
// It is encouraged to share logic between onAttachedToEngine and registerWith to keep
30+
// them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
31+
// depending on the user's project. onAttachedToEngine or registerWith must both be defined
32+
// in the same class.
33+
companion object {
34+
@JvmStatic
35+
fun registerWith(registrar: Registrar) {
36+
val channel = MethodChannel(registrar.messenger(), "{{projectName}}")
37+
channel.setMethodCallHandler({{pluginClass}}())
38+
}
39+
}
40+
2341
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
2442
if (call.method == "getPlatformVersion") {
2543
result.success("Android ${android.os.Build.VERSION.RELEASE}")

0 commit comments

Comments
 (0)