From 0a8c03dc8a2539f61b553817492cb7141b3e990d Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Wed, 21 Jul 2021 12:51:43 -0700 Subject: [PATCH] Show warning when an app or plugin uses the V1 Android embedding (#86750) --- .../lib/src/flutter_plugins.dart | 38 +++++++-- .../test/general.shard/plugins_test.dart | 84 +++++++++++++++++-- 2 files changed, 108 insertions(+), 14 deletions(-) diff --git a/packages/flutter_tools/lib/src/flutter_plugins.dart b/packages/flutter_tools/lib/src/flutter_plugins.dart index ee2b35368a..32936bbd9f 100644 --- a/packages/flutter_tools/lib/src/flutter_plugins.dart +++ b/packages/flutter_tools/lib/src/flutter_plugins.dart @@ -380,27 +380,47 @@ Future _writeAndroidPluginRegistrant(FlutterProject project, List templateContext['needsShim'] = false; // If a plugin is using an embedding version older than 2.0 and the app is using 2.0, // then add shim for the old plugins. + + final List pluginsUsingV1 = []; for (final Map plugin in androidPlugins) { final bool supportsEmbeddingV1 = (plugin['supportsEmbeddingV1'] as bool?) == true; final bool supportsEmbeddingV2 = (plugin['supportsEmbeddingV2'] as bool?) == true; if (supportsEmbeddingV1 && !supportsEmbeddingV2) { templateContext['needsShim'] = true; - if (project.isModule) { - globals.printStatus( - 'The plugin `${plugin['name']}` is built using an older version ' - "of the Android plugin API which assumes that it's running in a " - 'full-Flutter environment. It may have undefined behaviors when ' - 'Flutter is integrated into an existing app as a module.\n' - 'The plugin can be updated to the v2 Android Plugin APIs by ' - 'following https://flutter.dev/go/android-plugin-migration.' - ); + if (plugin['name'] != null) { + pluginsUsingV1.add(plugin['name']! as String); } } } + if (pluginsUsingV1.length > 1) { + globals.printError( + 'The plugins `${pluginsUsingV1.join(', ')}` use a deprecated version of the Android embedding.\n' + 'To avoid unexpected runtime failures, or future build failures, try to see if these plugins ' + 'support the Android V2 embedding. Otherwise, consider removing them since a future release ' + 'of Flutter will remove these deprecated APIs.\n' + 'If you are plugin author, take a look at the docs for migrating the plugin to the V2 embedding: ' + 'https://flutter.dev/go/android-plugin-migration.' + ); + } else if (pluginsUsingV1.isNotEmpty) { + globals.printError( + 'The plugin `${pluginsUsingV1.first}` uses a deprecated version of the Android embedding.\n' + 'To avoid unexpected runtime failures, or future build failures, try to see if this plugin ' + 'supports the Android V2 embedding. Otherwise, consider removing it since a future release ' + 'of Flutter will remove these deprecated APIs.\n' + 'If you are plugin author, take a look at the docs for migrating the plugin to the V2 embedding: ' + 'https://flutter.dev/go/android-plugin-migration.' + ); + } templateContent = _androidPluginRegistryTemplateNewEmbedding; break; case AndroidEmbeddingVersion.v1: default: + globals.printError( + 'This app is using a deprecated version of the Android embedding.\n' + 'To avoid unexpected runtime failures, or future build failures, try to migrate this ' + 'app to the V2 embedding.\n' + 'Take a look at the docs for migrating an app: https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects' + ); for (final Map plugin in androidPlugins) { final bool supportsEmbeddingV1 = (plugin['supportsEmbeddingV1'] as bool?) == true; final bool supportsEmbeddingV2 = (plugin['supportsEmbeddingV2'] as bool?) == true; diff --git a/packages/flutter_tools/test/general.shard/plugins_test.dart b/packages/flutter_tools/test/general.shard/plugins_test.dart index 2378e99146..a67beb19c5 100644 --- a/packages/flutter_tools/test/general.shard/plugins_test.dart +++ b/packages/flutter_tools/test/general.shard/plugins_test.dart @@ -746,7 +746,11 @@ dependencies: .childFile('GeneratedPluginRegistrant.java'); expect(registrant.readAsStringSync(), contains('plugin3.UseOldEmbedding.registerWith(shimPluginRegistry.registrarFor("plugin3.UseOldEmbedding"));')); - expect(testLogger.statusText, contains('The plugin `plugin3` is built using an older version of the Android plugin API')); + expect(testLogger.errorText, equals( + 'The plugin `plugin3` uses a deprecated version of the Android embedding.\n' + 'To avoid unexpected runtime failures, or future build failures, try to see if this plugin supports the Android V2 embedding. ' + 'Otherwise, consider removing it since a future release of Flutter will remove these deprecated APIs.\n' + 'If you are plugin author, take a look at the docs for migrating the plugin to the V2 embedding: https://flutter.dev/go/android-plugin-migration.\n')); }, overrides: { FileSystem: () => fs, ProcessManager: () => FakeProcessManager.any(), @@ -767,7 +771,7 @@ dependencies: expect(registrant.readAsStringSync(), contains('flutterEngine.getPlugins().add(new plugin1.UseNewEmbedding());')); - expect(testLogger.statusText, isNot(contains('go/android-plugin-migration'))); + expect(testLogger.errorText, isNot(contains('go/android-plugin-migration'))); }, overrides: { FileSystem: () => fs, ProcessManager: () => FakeProcessManager.any(), @@ -788,7 +792,45 @@ dependencies: expect(registrant.readAsStringSync(), contains('flutterEngine.getPlugins().add(new plugin4.UseBothEmbedding());')); - expect(testLogger.statusText, isNot(contains('go/android-plugin-migration'))); + expect(testLogger.errorText, isNot(contains('go/android-plugin-migration'))); + }, overrides: { + FileSystem: () => fs, + ProcessManager: () => FakeProcessManager.any(), + XcodeProjectInterpreter: () => xcodeProjectInterpreter, + }); + + testUsingContext('App using plugin with v1 and v2 support shows no warning', () async { + flutterProject.isModule = false; + androidProject.embeddingVersion = AndroidEmbeddingVersion.v2; + + createDualSupportJavaPlugin4(); + + await injectPlugins(flutterProject, androidPlatform: true); + + final File registrant = flutterProject.directory + .childDirectory(fs.path.join('android', 'app', 'src', 'main', 'java', 'io', 'flutter', 'plugins')) + .childFile('GeneratedPluginRegistrant.java'); + expect(registrant.readAsStringSync(), + contains('flutterEngine.getPlugins().add(new plugin4.UseBothEmbedding());')); + + expect(testLogger.errorText, isNot(contains('go/android-plugin-migration'))); + }, overrides: { + FileSystem: () => fs, + ProcessManager: () => FakeProcessManager.any(), + XcodeProjectInterpreter: () => xcodeProjectInterpreter, + }); + + testUsingContext('App using the v1 embedding shows warning', () async { + flutterProject.isModule = false; + androidProject.embeddingVersion = AndroidEmbeddingVersion.v1; + + await injectPlugins(flutterProject, androidPlatform: true); + + expect(testLogger.errorText, equals( + 'This app is using a deprecated version of the Android embedding.\n' + 'To avoid unexpected runtime failures, or future build failures, try to migrate this app to the V2 embedding.\n' + 'Take a look at the docs for migrating an app: https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects\n' + )); }, overrides: { FileSystem: () => fs, ProcessManager: () => FakeProcessManager.any(), @@ -811,8 +853,40 @@ dependencies: contains('plugin3.UseOldEmbedding.registerWith(shimPluginRegistry.registrarFor("plugin3.UseOldEmbedding"));')); expect(registrant.readAsStringSync(), contains('plugin4.UseOldEmbedding.registerWith(shimPluginRegistry.registrarFor("plugin4.UseOldEmbedding"));')); - expect(testLogger.statusText, contains('The plugin `plugin3` is built using an older version of the Android plugin API')); - expect(testLogger.statusText, contains('The plugin `plugin4` is built using an older version of the Android plugin API')); + expect(testLogger.errorText, equals( + 'The plugins `plugin3, plugin4` use a deprecated version of the Android embedding.\n' + 'To avoid unexpected runtime failures, or future build failures, try to see if these plugins support the Android V2 embedding. ' + 'Otherwise, consider removing them since a future release of Flutter will remove these deprecated APIs.\n' + 'If you are plugin author, take a look at the docs for migrating the plugin to the V2 embedding: https://flutter.dev/go/android-plugin-migration.\n' + )); + }, overrides: { + FileSystem: () => fs, + ProcessManager: () => FakeProcessManager.any(), + XcodeProjectInterpreter: () => xcodeProjectInterpreter, + }); + + testUsingContext('App using multiple old plugins all show warnings', () async { + flutterProject.isModule = false; + androidProject.embeddingVersion = AndroidEmbeddingVersion.v2; + + createOldJavaPlugin('plugin3'); + createOldJavaPlugin('plugin4'); + + await injectPlugins(flutterProject, androidPlatform: true); + + final File registrant = flutterProject.directory + .childDirectory(fs.path.join('android', 'app', 'src', 'main', 'java', 'io', 'flutter', 'plugins')) + .childFile('GeneratedPluginRegistrant.java'); + expect(registrant.readAsStringSync(), + contains('plugin3.UseOldEmbedding.registerWith(shimPluginRegistry.registrarFor("plugin3.UseOldEmbedding"));')); + expect(registrant.readAsStringSync(), + contains('plugin4.UseOldEmbedding.registerWith(shimPluginRegistry.registrarFor("plugin4.UseOldEmbedding"));')); + expect(testLogger.errorText, equals( + 'The plugins `plugin3, plugin4` use a deprecated version of the Android embedding.\n' + 'To avoid unexpected runtime failures, or future build failures, try to see if these plugins support the Android V2 embedding. ' + 'Otherwise, consider removing them since a future release of Flutter will remove these deprecated APIs.\n' + 'If you are plugin author, take a look at the docs for migrating the plugin to the V2 embedding: https://flutter.dev/go/android-plugin-migration.\n' + )); }, overrides: { FileSystem: () => fs, ProcessManager: () => FakeProcessManager.any(),