Skip to content

Commit

Permalink
FFI plugins (flutter#96225)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcharkes authored and clocksmith committed Mar 8, 2022
1 parent edb6061 commit b8c3628
Show file tree
Hide file tree
Showing 59 changed files with 1,441 additions and 254 deletions.
2 changes: 2 additions & 0 deletions dev/devicelab/bin/tasks/plugin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ Future<void> main() async {
<String, String>{'ENABLE_ANDROID_EMBEDDING_V2': 'true'}),
// Test that Dart-only plugins are supported.
PluginTest('apk', <String>['--platforms=android'], dartOnlyPlugin: true),
// Test that FFI plugins are supported.
PluginTest('apk', <String>['--platforms=android'], template: 'plugin_ffi'),
]));
}
3 changes: 3 additions & 0 deletions dev/devicelab/bin/tasks/plugin_test_ios.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ Future<void> main() async {
// Test that Dart-only plugins are supported.
PluginTest('ios', <String>['--platforms=ios'], dartOnlyPlugin: true),
PluginTest('macos', <String>['--platforms=macos'], dartOnlyPlugin: true),
// Test that FFI plugins are supported.
PluginTest('ios', <String>['--platforms=ios'], template: 'plugin_ffi'),
PluginTest('macos', <String>['--platforms=macos'], template: 'plugin_ffi'),
]));
}
18 changes: 14 additions & 4 deletions dev/devicelab/lib/tasks/plugin_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,35 @@ class PluginTest {
this.pluginCreateEnvironment,
this.appCreateEnvironment,
this.dartOnlyPlugin = false,
this.template = 'plugin',
});

final String buildTarget;
final List<String> options;
final Map<String, String>? pluginCreateEnvironment;
final Map<String, String>? appCreateEnvironment;
final bool dartOnlyPlugin;
final String template;

Future<TaskResult> call() async {
final Directory tempDir =
Directory.systemTemp.createTempSync('flutter_devicelab_plugin_test.');
// FFI plugins do not have support for `flutter test`.
// `flutter test` does not do a native build.
// Supporting `flutter test` would require invoking a native build.
final bool runFlutterTest = template != 'plugin_ffi';
try {
section('Create plugin');
final _FlutterProject plugin = await _FlutterProject.create(
tempDir, options, buildTarget,
name: 'plugintest', template: 'plugin', environment: pluginCreateEnvironment);
name: 'plugintest', template: template, environment: pluginCreateEnvironment);
if (dartOnlyPlugin) {
await plugin.convertDefaultPluginToDartPlugin();
}
section('Test plugin');
await plugin.test();
if (runFlutterTest) {
await plugin.test();
}
section('Create Flutter app');
final _FlutterProject app = await _FlutterProject.create(tempDir, options, buildTarget,
name: 'plugintestapp', template: 'app', environment: appCreateEnvironment);
Expand All @@ -63,8 +71,10 @@ class PluginTest {
await app.addPlugin('path_provider');
section('Build app');
await app.build(buildTarget, validateNativeBuildProject: !dartOnlyPlugin);
section('Test app');
await app.test();
if (runFlutterTest) {
section('Test app');
await app.test();
}
} finally {
await plugin.delete();
await app.delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ list(APPEND FLUTTER_PLUGIN_LIST
url_launcher_windows
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
)

set(PLUGIN_BUNDLED_LIBRARIES)

foreach(plugin ${FLUTTER_PLUGIN_LIST})
Expand All @@ -14,3 +17,8 @@ foreach(plugin ${FLUTTER_PLUGIN_LIST})
list(APPEND PLUGIN_BUNDLED_LIBRARIES $<TARGET_FILE:${plugin}_plugin>)
list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${plugin}_bundled_libraries})
endforeach(plugin)

foreach(ffi_plugin ${FLUTTER_FFI_PLUGIN_LIST})
add_subdirectory(flutter/ephemeral/.plugin_symlinks/${ffi_plugin}/windows plugins/${ffi_plugin})
list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${ffi_plugin}_bundled_libraries})
endforeach(ffi_plugin)
54 changes: 50 additions & 4 deletions packages/flutter_tools/gradle/flutter.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class FlutterExtension {
/** Sets the targetSdkVersion used by default in Flutter app projects. */
static int targetSdkVersion = 31

/**
* Sets the ndkVersion used by default in Flutter app projects.
* Chosen as default version of the AGP version below.
*/
static String ndkVersion = "21.1.6352462"

/**
* Specifies the relative directory to the Flutter project directory.
* In an app project, this is ../.. since the app's build.gradle is under android/app.
Expand All @@ -54,6 +60,7 @@ buildscript {
mavenCentral()
}
dependencies {
/* When bumping, also update ndkVersion above. */
classpath 'com.android.tools.build:gradle:4.1.0'
}
}
Expand Down Expand Up @@ -409,24 +416,63 @@ class FlutterPlugin implements Plugin<Project> {
}
}

/** Prints error message and fix for any plugin compileSdkVersion that are higher than the project. */
private void detectLowCompileSdkVersion() {
/**
* Compares semantic versions ignoring labels.
*
* If the versions are equal (ignoring labels), returns one of the two strings arbitrarily.
*
* If minor or patch are omitted (non-conformant to semantic versioning), they are considered zero.
* If the provided versions in both are equal, the longest version string is returned.
* For example, "2.8.0" vs "2.8" will always consider "2.8.0" to be the most recent version.
*/
static String mostRecentSemanticVersion(String version1, String version2) {
List version1Tokenized = version1.tokenize('.')
List version2Tokenized = version2.tokenize('.')
def version1numTokens = version1Tokenized.size()
def version2numTokens = version2Tokenized.size()
def minNumTokens = Math.min(version1numTokens, version2numTokens)
for (int i = 0; i < minNumTokens; i++) {
def num1 = version1Tokenized[i].toInteger()
def num2 = version2Tokenized[i].toInteger()
if (num1 > num2) {
return version1
}
if (num2 > num1) {
return version2
}
}
if (version1numTokens > version2numTokens) {
return version1
}
return version2
}

/** Prints error message and fix for any plugin compileSdkVersion or ndkVersion that are higher than the project. */
private void detectLowCompileSdkVersionOrNdkVersion() {
project.afterEvaluate {
int projectCompileSdkVersion = project.android.compileSdkVersion.substring(8) as int
int maxPluginCompileSdkVersion = projectCompileSdkVersion
String ndkVersionIfUnspecified = "21.1.6352462" /* The default for AGP 4.1.0 used in old templates. */
String projectNdkVersion = project.android.ndkVersion ?: ndkVersionIfUnspecified
String maxPluginNdkVersion = projectNdkVersion
int numProcessedPlugins = getPluginList().size()

getPluginList().each { plugin ->
Project pluginProject = project.rootProject.findProject(plugin.key)
pluginProject.afterEvaluate {
int pluginCompileSdkVersion = pluginProject.android.compileSdkVersion.substring(8) as int
maxPluginCompileSdkVersion = Math.max(pluginCompileSdkVersion, maxPluginCompileSdkVersion)
String pluginNdkVersion = pluginProject.android.ndkVersion ?: ndkVersionIfUnspecified
maxPluginNdkVersion = mostRecentSemanticVersion(pluginNdkVersion, maxPluginNdkVersion)

numProcessedPlugins--
if (numProcessedPlugins == 0) {
if (maxPluginCompileSdkVersion > projectCompileSdkVersion) {
project.logger.error("One or more plugins require a higher Android SDK version.\nFix this issue by adding the following to ${project.projectDir}${File.separator}build.gradle:\nandroid {\n compileSdkVersion ${maxPluginCompileSdkVersion}\n ...\n}\n")
}
if (maxPluginNdkVersion != projectNdkVersion) {
project.logger.error("One or more plugins require a higher Android NDK version.\nFix this issue by adding the following to ${project.projectDir}${File.separator}build.gradle:\nandroid {\n ndkVersion ${maxPluginNdkVersion}\n ...\n}\n")
}
}
}
}
Expand Down Expand Up @@ -963,7 +1009,7 @@ class FlutterPlugin implements Plugin<Project> {
}
}
configurePlugins()
detectLowCompileSdkVersion()
detectLowCompileSdkVersionOrNdkVersion()
return
}
// Flutter host module project (Add-to-app).
Expand Down Expand Up @@ -1015,7 +1061,7 @@ class FlutterPlugin implements Plugin<Project> {
}
}
configurePlugins()
detectLowCompileSdkVersion()
detectLowCompileSdkVersionOrNdkVersion()
}
}

Expand Down
Loading

0 comments on commit b8c3628

Please sign in to comment.