FFI plugins (#96225)

This commit is contained in:
Daco Harkes
2022-01-26 23:44:45 +01:00
committed by GitHub
parent f15dd780d4
commit 0e2f51dfd0
59 changed files with 1441 additions and 254 deletions

View File

@@ -43,11 +43,14 @@ void main() {
final List<String> templatePaths = <String>[
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'app'),
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'app_shared'),
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'app_test_widget'),
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'cocoapods'),
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'skeleton'),
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'module', 'common'),
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'package'),
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'plugin'),
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'plugin_ffi'),
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'plugin_shared'),
];
for (final String templatePath in templatePaths) {
globals.fs.directory(templatePath).createSync(recursive: true);
@@ -96,6 +99,9 @@ void main() {
await runner.run(<String>['create', '--no-pub', '--template=plugin', 'testy']);
expect((await command.usageValues).commandCreateProjectType, 'plugin');
await runner.run(<String>['create', '--no-pub', '--template=plugin_ffi', 'testy']);
expect((await command.usageValues).commandCreateProjectType, 'plugin_ffi');
}));
testUsingContext('set iOS host language type as usage value', () => testbed.run(() async {

View File

@@ -2492,6 +2492,7 @@ void main() {
final String buildContent = await globals.fs.file('${projectDir.path}/android/app/build.gradle').readAsString();
expect(buildContent.contains('compileSdkVersion flutter.compileSdkVersion'), true);
expect(buildContent.contains('ndkVersion flutter.ndkVersion'), true);
expect(buildContent.contains('targetSdkVersion flutter.targetSdkVersion'), true);
});
@@ -2761,6 +2762,104 @@ void main() {
platform: globals.platform,
),
});
testUsingContext('create an FFI plugin with ios, then add macos', () async {
Cache.flutterRoot = '../..';
final CreateCommand command = CreateCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['create', '--no-pub', '--template=plugin_ffi', '--platform=ios', projectDir.path]);
expect(projectDir.childDirectory('src'), exists);
expect(projectDir.childDirectory('ios'), exists);
expect(projectDir.childDirectory('example').childDirectory('ios'), exists);
validatePubspecForPlugin(
projectDir: projectDir.absolute.path,
expectedPlatforms: const <String>[
'ios',
],
ffiPlugin: true,
unexpectedPlatforms: <String>['some_platform'],
);
await runner.run(<String>['create', '--no-pub', '--template=plugin_ffi', '--platform=macos', projectDir.path]);
expect(projectDir.childDirectory('macos'), exists);
expect(
projectDir.childDirectory('example').childDirectory('macos'), exists);
expect(projectDir.childDirectory('ios'), exists);
expect(projectDir.childDirectory('example').childDirectory('ios'), exists);
}, overrides: <Type, Generator>{
FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true),
});
testUsingContext('FFI plugins error android language', () async {
final CreateCommand command = CreateCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
final List<String> args = <String>[
'create',
'--no-pub',
'--template=plugin_ffi',
'-a',
'kotlin',
'--platforms=android',
projectDir.path,
];
await expectLater(
runner.run(args),
throwsToolExit(message: 'The "android-language" option is not supported with the plugin_ffi template: the language will always be C or C++.'),
);
});
testUsingContext('FFI plugins error ios language', () async {
final CreateCommand command = CreateCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
final List<String> args = <String>[
'create',
'--no-pub',
'--template=plugin_ffi',
'--ios-language',
'swift',
'--platforms=ios',
projectDir.path,
];
await expectLater(
runner.run(args),
throwsToolExit(message: 'The "ios-language" option is not supported with the plugin_ffi template: the language will always be C or C++.'),
);
});
testUsingContext('FFI plugins error web platform', () async {
final CreateCommand command = CreateCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
final List<String> args = <String>[
'create',
'--no-pub',
'--template=plugin_ffi',
'--platforms=web',
projectDir.path,
];
await expectLater(
runner.run(args),
throwsToolExit(message: 'The web platform is not supported in plugin_ffi template.'),
);
});
testUsingContext('should show warning when disabled platforms are selected while creating an FFI plugin', () async {
Cache.flutterRoot = '../..';
final CreateCommand command = CreateCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['create', '--no-pub', '--template=plugin_ffi', '--platforms=android,ios,windows,macos,linux', projectDir.path]);
await runner.run(<String>['create', '--no-pub', '--template=plugin_ffi', projectDir.path]);
expect(logger.statusText, contains(_kDisabledPlatformRequestedMessage));
}, overrides: <Type, Generator>{
FeatureFlags: () => TestFeatureFlags(),
Logger: () => logger,
});
}
Future<void> _createProject(

View File

@@ -9,33 +9,43 @@ import '../src/common.dart';
import 'test_utils.dart';
void main() {
late Directory tempDir;
late Directory tempDirPluginMethodChannels;
late Directory tempDirPluginFfi;
setUp(() async {
tempDir = createResolvedTempDirectorySync('flutter_plugin_test.');
tempDirPluginMethodChannels = createResolvedTempDirectorySync('flutter_plugin_test.');
tempDirPluginFfi =
createResolvedTempDirectorySync('flutter_ffi_plugin_test.');
});
tearDown(() async {
tryToDelete(tempDir);
tryToDelete(tempDirPluginMethodChannels);
tryToDelete(tempDirPluginFfi);
});
test('plugin example can be built using current Flutter Gradle plugin', () async {
Future<void> testPlugin({
required String template,
required Directory tempDir,
}) async {
final String flutterBin = fileSystem.path.join(
getFlutterRoot(),
'bin',
'flutter',
);
final String testName = '${template}_test';
processManager.runSync(<String>[
flutterBin,
...getLocalEngineArguments(),
'create',
'--template=plugin',
'--template=$template',
'--platforms=android',
'plugin_test',
testName,
], workingDirectory: tempDir.path);
final Directory exampleAppDir = tempDir.childDirectory('plugin_test').childDirectory('example');
final Directory exampleAppDir =
tempDir.childDirectory(testName).childDirectory('example');
final File buildGradleFile = exampleAppDir.childDirectory('android').childFile('build.gradle');
expect(buildGradleFile, exists);
@@ -68,6 +78,11 @@ void main() {
));
expect(exampleApk, exists);
if (template == 'plugin_ffi') {
// Does not support AGP 3.3.0.
return;
}
// Clean
processManager.runSync(<String>[
flutterBin,
@@ -101,5 +116,21 @@ android.enableR8=true''');
'--target-platform=android-arm',
], workingDirectory: exampleAppDir.path);
expect(exampleApk, exists);
}
test('plugin example can be built using current Flutter Gradle plugin',
() async {
await testPlugin(
template: 'plugin',
tempDir: tempDirPluginMethodChannels,
);
});
test('FFI plugin example can be built using current Flutter Gradle plugin',
() async {
await testPlugin(
template: 'plugin_ffi',
tempDir: tempDirPluginFfi,
);
});
}

View File

@@ -0,0 +1,86 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:file_testing/file_testing.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/cache.dart';
import '../src/common.dart';
import 'test_utils.dart';
void main() {
late Directory tempDir;
setUp(() {
Cache.flutterRoot = getFlutterRoot();
tempDir = createResolvedTempDirectorySync('flutter_plugin_test.');
});
tearDown(() async {
tryToDelete(tempDir);
});
test('error logged when plugin Android ndkVersion higher than project', () async {
final String flutterBin = fileSystem.path.join(
getFlutterRoot(),
'bin',
'flutter',
);
// Create dummy plugin
processManager.runSync(<String>[
flutterBin,
...getLocalEngineArguments(),
'create',
'--template=plugin_ffi',
'--platforms=android',
'test_plugin',
], workingDirectory: tempDir.path);
final Directory pluginAppDir = tempDir.childDirectory('test_plugin');
final File pluginGradleFile = pluginAppDir.childDirectory('android').childFile('build.gradle');
expect(pluginGradleFile, exists);
final String pluginBuildGradle = pluginGradleFile.readAsStringSync();
// Bump up plugin ndkVersion to 21.4.7075529.
final RegExp androidNdkVersionRegExp = RegExp(r'ndkVersion (\"[0-9\.]+\"|flutter.ndkVersion)');
final String newPluginGradleFile = pluginBuildGradle.replaceAll(androidNdkVersionRegExp, 'ndkVersion "21.4.7075529"');
expect(newPluginGradleFile, contains('21.4.7075529'));
pluginGradleFile.writeAsStringSync(newPluginGradleFile);
final Directory pluginExampleAppDir = pluginAppDir.childDirectory('example');
final File projectGradleFile = pluginExampleAppDir.childDirectory('android').childDirectory('app').childFile('build.gradle');
expect(projectGradleFile, exists);
final String projectBuildGradle = projectGradleFile.readAsStringSync();
// Bump down plugin example app ndkVersion to 21.1.6352462.
final String newProjectGradleFile = projectBuildGradle.replaceAll(androidNdkVersionRegExp, 'ndkVersion "21.1.6352462"');
expect(newProjectGradleFile, contains('21.1.6352462'));
projectGradleFile.writeAsStringSync(newProjectGradleFile);
// Run flutter build apk to build plugin example project
final ProcessResult result = processManager.runSync(<String>[
flutterBin,
...getLocalEngineArguments(),
'build',
'apk',
'--target-platform=android-arm',
], workingDirectory: pluginExampleAppDir.path);
// Check that an error message is thrown.
expect(result.stderr, contains('''
One or more plugins require a higher Android NDK version.
Fix this issue by adding the following to ${projectGradleFile.path}:
android {
ndkVersion 21.4.7075529
...
}
'''));
});
}

View File

@@ -176,6 +176,7 @@ class BasicDeferredComponentsConfig extends DeferredComponentsConfig {
android {
compileSdkVersion flutter.compileSdkVersion
ndkVersion flutter.ndkVersion
sourceSets {
main.java.srcDirs += 'src/main/kotlin'

View File

@@ -11,19 +11,23 @@ import 'common.dart';
/// Check if the pubspec.yaml file under the `projectDir` is valid for a plugin project.
void validatePubspecForPlugin({
required String projectDir,
required String pluginClass,
String? pluginClass,
bool ffiPlugin = false,
required List<String> expectedPlatforms,
List<String> unexpectedPlatforms = const <String>[],
String? androidIdentifier,
String? webFileName,
}) {
assert(pluginClass != null || ffiPlugin);
final FlutterManifest manifest =
FlutterManifest.createFromPath('$projectDir/pubspec.yaml', fileSystem: globals.fs, logger: globals.logger)!;
final YamlMap platformMaps = YamlMap.wrap(manifest.supportedPlatforms!);
for (final String platform in expectedPlatforms) {
expect(platformMaps[platform], isNotNull);
final YamlMap platformMap = platformMaps[platform]! as YamlMap;
expect(platformMap['pluginClass'], pluginClass);
if (pluginClass != null) {
expect(platformMap['pluginClass'], pluginClass);
}
if (platform == 'android') {
expect(platformMap['package'], androidIdentifier);
}