Unverified Commit bd96a8f9 authored by Alan Trope's avatar Alan Trope Committed by GitHub

Catch errors during android plugin registration (#78964)

parent 0f8a7cb3
......@@ -567,6 +567,7 @@ package io.flutter.plugins;
import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import io.flutter.Log;
import io.flutter.embedding.engine.FlutterEngine;
{{#needsShim}}
......@@ -580,17 +581,26 @@ import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry;
*/
@Keep
public final class GeneratedPluginRegistrant {
private static final String TAG = "GeneratedPluginRegistrant";
public static void registerWith(@NonNull FlutterEngine flutterEngine) {
{{#needsShim}}
ShimPluginRegistry shimPluginRegistry = new ShimPluginRegistry(flutterEngine);
{{/needsShim}}
{{#plugins}}
{{#supportsEmbeddingV2}}
try {
flutterEngine.getPlugins().add(new {{package}}.{{class}}());
} catch(Exception e) {
Log.e(TAG, "Error registering plugin {{name}}, {{package}}.{{class}}", e);
}
{{/supportsEmbeddingV2}}
{{^supportsEmbeddingV2}}
{{#supportsEmbeddingV1}}
try {
{{package}}.{{class}}.registerWith(shimPluginRegistry.registrarFor("{{package}}.{{class}}"));
} catch(Exception e) {
Log.e(TAG, "Error registering plugin {{name}}, {{package}}.{{class}}", e);
}
{{/supportsEmbeddingV1}}
{{/supportsEmbeddingV2}}
{{/plugins}}
......
......@@ -823,6 +823,32 @@ dependencies:
XcodeProjectInterpreter: () => xcodeProjectInterpreter,
});
testUsingContext('Module using multiple old and new plugins should be wrapped with try catch', () async {
when(flutterProject.isModule).thenReturn(true);
when(androidProject.getEmbeddingVersion()).thenReturn(AndroidEmbeddingVersion.v2);
createOldJavaPlugin('abcplugin1');
createNewJavaPlugin1();
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');
const String newPluginName = 'flutterEngine.getPlugins().add(new plugin1.UseNewEmbedding());';
const String oldPluginName = 'abcplugin1.UseOldEmbedding.registerWith(shimPluginRegistry.registrarFor("abcplugin1.UseOldEmbedding"));';
final String content = registrant.readAsStringSync();
for(final String plugin in <String>[newPluginName,oldPluginName]){
expect(content, contains(plugin));
expect(content.split(plugin).first.trim().endsWith('try {'), isTrue);
expect(content.split(plugin).last.trim().startsWith('} catch(Exception e) {'), isTrue);
}
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => FakeProcessManager.any(),
XcodeProjectInterpreter: () => xcodeProjectInterpreter,
});
testUsingContext('Does not throw when AndroidManifest.xml is not found', () async {
when(flutterProject.isModule).thenReturn(false);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment