Add v1 plugin register function into v2 plugin template (#44166)

This commit is contained in:
xster 2019-11-05 20:03:50 -08:00 committed by GitHub
parent 7aebde1904
commit cf95cd4394
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 6 deletions

View file

@ -11,5 +11,12 @@ Future<void> main() async {
await task(combine(<TaskFunction>[
PluginTest('apk', <String>['-a', 'java']),
PluginTest('apk', <String>['-a', 'kotlin']),
// These create the plugins using the new v2 plugin templates but create the
// apps using the old v1 embedding app templates to make sure new plugins
// are by default backward compatible.
PluginTest('apk', <String>['-a', 'java'], pluginCreateEnvironment:
<String, String>{'ENABLE_ANDROID_EMBEDDING_V2': 'true'}),
PluginTest('apk', <String>['-a', 'kotlin'], pluginCreateEnvironment:
<String, String>{'ENABLE_ANDROID_EMBEDDING_V2': 'true'}),
]));
}

View file

@ -256,7 +256,8 @@ Future<Process> startProcess(
assert(isBot != null);
final String command = '$executable ${arguments?.join(" ") ?? ""}';
final String finalWorkingDirectory = workingDirectory ?? cwd;
print('\nExecuting: $command in $finalWorkingDirectory');
print('\nExecuting: $command in $finalWorkingDirectory'
+ (environment != null ? ' with environment $environment' : ''));
environment ??= <String, String>{};
environment['BOT'] = isBot ? 'true' : 'false';
final Process process = await _processManager.start(

View file

@ -26,10 +26,12 @@ TaskFunction combine(List<TaskFunction> tasks) {
/// Defines task that creates new Flutter project, adds a local and remote
/// plugin, and then builds the specified [buildTarget].
class PluginTest {
PluginTest(this.buildTarget, this.options);
PluginTest(this.buildTarget, this.options, { this.pluginCreateEnvironment, this.appCreateEnvironment });
final String buildTarget;
final List<String> options;
final Map<String, String> pluginCreateEnvironment;
final Map<String, String> appCreateEnvironment;
Future<TaskResult> call() async {
final Directory tempDir =
@ -38,12 +40,12 @@ class PluginTest {
section('Create plugin');
final _FlutterProject plugin = await _FlutterProject.create(
tempDir, options,
name: 'plugintest', template: 'plugin');
name: 'plugintest', template: 'plugin', environment: pluginCreateEnvironment);
section('Test plugin');
await plugin.test();
section('Create Flutter app');
final _FlutterProject app = await _FlutterProject.create(tempDir, options,
name: 'plugintestapp', template: 'app');
name: 'plugintestapp', template: 'app', environment: appCreateEnvironment);
try {
if (buildTarget == 'ios')
await prepareProvisioningCertificates(app.rootPath);
@ -95,8 +97,13 @@ class _FlutterProject {
}
static Future<_FlutterProject> create(
Directory directory, List<String> options,
{String name, String template}) async {
Directory directory,
List<String> options,
{
String name,
String template,
Map<String, String> environment,
}) async {
await inDirectory(directory, () async {
await flutter(
'create',
@ -107,6 +114,7 @@ class _FlutterProject {
...options,
name,
],
environment: environment,
);
});
return _FlutterProject(directory, name);

View file

@ -12,6 +12,7 @@ import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
/** {{pluginClass}} */
public class {{pluginClass}} implements FlutterPlugin, MethodCallHandler {
@ -21,6 +22,20 @@ public class {{pluginClass}} implements FlutterPlugin, MethodCallHandler {
channel.setMethodCallHandler(new {{pluginClass}}());
}
// This static function is optional and equivalent to onAttachedToEngine. It supports the old
// pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
// plugin registration via this function while apps migrate to use the new Android APIs
// post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
//
// It is encouraged to share logic between onAttachedToEngine and registerWith to keep
// them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
// depending on the user's project. onAttachedToEngine or registerWith must both be defined
// in the same class.
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "{{projectName}}");
channel.setMethodCallHandler(new {{pluginClass}}());
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("getPlatformVersion")) {

View file

@ -12,6 +12,7 @@ import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
/** {{pluginClass}} */
public class {{pluginClass}}: FlutterPlugin, MethodCallHandler {
@ -20,6 +21,23 @@ public class {{pluginClass}}: FlutterPlugin, MethodCallHandler {
channel.setMethodCallHandler({{pluginClass}}());
}
// This static function is optional and equivalent to onAttachedToEngine. It supports the old
// pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
// plugin registration via this function while apps migrate to use the new Android APIs
// post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
//
// It is encouraged to share logic between onAttachedToEngine and registerWith to keep
// them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
// depending on the user's project. onAttachedToEngine or registerWith must both be defined
// in the same class.
companion object {
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "{{projectName}}")
channel.setMethodCallHandler({{pluginClass}}())
}
}
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "getPlatformVersion") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")