Native assets support for Android (#135148)
Support for FFI calls with `@Native external` functions through Native assets on Android. This enables bundling native code without any build-system boilerplate code. For more info see: * https://github.com/flutter/flutter/issues/129757 ### Implementation details for Android. Mainly follows the design of the previous PRs. For Android, we detect the compilers inside the NDK inside SDK. And bundling of the assets is done by the flutter.groovy file. The `minSdkVersion` is propagated from the flutter.groovy file as well. The NDK is not part of `flutter doctor`, and users can omit it if no native assets have to be build. However, if any native assets must be built, flutter throws a tool exit if the NDK is not installed. Add 2 app is not part of this PR yet, instead `flutter build aar` will tool exit if there are any native assets.
This commit is contained in:
@@ -43,6 +43,32 @@ Future<void> main() async {
|
||||
);
|
||||
});
|
||||
|
||||
section('Create package with native assets');
|
||||
|
||||
await flutter(
|
||||
'config',
|
||||
options: <String>['--enable-native-assets'],
|
||||
);
|
||||
|
||||
const String ffiPackageName = 'ffi_package';
|
||||
await createFfiPackage(ffiPackageName, tempDir);
|
||||
|
||||
section('Add FFI package');
|
||||
|
||||
final File pubspec = File(path.join(projectDir.path, 'pubspec.yaml'));
|
||||
String content = await pubspec.readAsString();
|
||||
content = content.replaceFirst(
|
||||
'dependencies:$platformLineSep',
|
||||
'dependencies:$platformLineSep $ffiPackageName:$platformLineSep path: ..${Platform.pathSeparator}$ffiPackageName$platformLineSep',
|
||||
);
|
||||
await pubspec.writeAsString(content, flush: true);
|
||||
await inDirectory(projectDir, () async {
|
||||
await flutter(
|
||||
'packages',
|
||||
options: <String>['get'],
|
||||
);
|
||||
});
|
||||
|
||||
section('Add read-only asset');
|
||||
|
||||
final File readonlyTxtAssetFile = await File(path.join(
|
||||
@@ -63,8 +89,6 @@ Future<void> main() async {
|
||||
]);
|
||||
}
|
||||
|
||||
final File pubspec = File(path.join(projectDir.path, 'pubspec.yaml'));
|
||||
String content = await pubspec.readAsString();
|
||||
content = content.replaceFirst(
|
||||
'$platformLineSep # assets:$platformLineSep',
|
||||
'$platformLineSep assets:$platformLineSep - assets/read-only.txt$platformLineSep',
|
||||
@@ -73,7 +97,6 @@ Future<void> main() async {
|
||||
|
||||
section('Add plugins');
|
||||
|
||||
content = await pubspec.readAsString();
|
||||
content = content.replaceFirst(
|
||||
'${platformLineSep}dependencies:$platformLineSep',
|
||||
'${platformLineSep}dependencies:$platformLineSep device_info: 2.0.3$platformLineSep package_info: 2.0.2$platformLineSep',
|
||||
@@ -86,6 +109,45 @@ Future<void> main() async {
|
||||
);
|
||||
});
|
||||
|
||||
// TODO(dacoharkes): Implement Add2app. https://github.com/flutter/flutter/issues/129757
|
||||
section('Check native assets error');
|
||||
|
||||
await inDirectory(Directory(path.join(projectDir.path, '.android')),
|
||||
() async {
|
||||
final StringBuffer stderr = StringBuffer();
|
||||
final int exitCode = await exec(
|
||||
gradlewExecutable,
|
||||
<String>['flutter:assembleDebug'],
|
||||
environment: <String, String>{'JAVA_HOME': javaHome},
|
||||
canFail: true,
|
||||
stderr: stderr,
|
||||
);
|
||||
const String errorString =
|
||||
'Native assets are not yet supported in Android add2app.';
|
||||
if (!stderr.toString().contains(errorString) || exitCode == 0) {
|
||||
throw TaskResult.failure(
|
||||
'''
|
||||
Expected to find `$errorString` in stderr and nonZero exit code.
|
||||
$stderr
|
||||
exitCode: $exitCode
|
||||
''');
|
||||
}
|
||||
});
|
||||
|
||||
section('Remove FFI package');
|
||||
|
||||
content = content.replaceFirst(
|
||||
' $ffiPackageName:$platformLineSep path: ..${Platform.pathSeparator}$ffiPackageName$platformLineSep',
|
||||
'',
|
||||
);
|
||||
await pubspec.writeAsString(content, flush: true);
|
||||
await inDirectory(projectDir, () async {
|
||||
await flutter(
|
||||
'packages',
|
||||
options: <String>['get'],
|
||||
);
|
||||
});
|
||||
|
||||
section('Build Flutter module library archive');
|
||||
|
||||
await inDirectory(Directory(path.join(projectDir.path, '.android')), () async {
|
||||
|
||||
@@ -67,7 +67,7 @@ Future<void> main() async {
|
||||
);
|
||||
|
||||
const String ffiPackageName = 'ffi_package';
|
||||
await _createFfiPackage(ffiPackageName, tempDir);
|
||||
await createFfiPackage(ffiPackageName, tempDir);
|
||||
|
||||
section('Add FFI package');
|
||||
|
||||
@@ -730,30 +730,3 @@ class $dartPluginClass {
|
||||
// Remove the native plugin code.
|
||||
await Directory(path.join(pluginDir, 'ios')).delete(recursive: true);
|
||||
}
|
||||
|
||||
Future<void> _createFfiPackage(String name, Directory parent) async {
|
||||
await inDirectory(parent, () async {
|
||||
await flutter(
|
||||
'create',
|
||||
options: <String>[
|
||||
'--no-pub',
|
||||
'--org',
|
||||
'io.flutter.devicelab',
|
||||
'--template=package_ffi',
|
||||
name,
|
||||
],
|
||||
);
|
||||
await _pinDependencies(
|
||||
File(path.join(parent.path, name, 'pubspec.yaml')),
|
||||
);
|
||||
await _pinDependencies(
|
||||
File(path.join(parent.path, name, 'example', 'pubspec.yaml')),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _pinDependencies(File pubspecFile) async {
|
||||
final String oldPubspec = await pubspecFile.readAsString();
|
||||
final String newPubspec = oldPubspec.replaceAll(': ^', ': ');
|
||||
await pubspecFile.writeAsString(newPubspec);
|
||||
}
|
||||
|
||||
14
dev/devicelab/bin/tasks/native_assets_android.dart
Normal file
14
dev/devicelab/bin/tasks/native_assets_android.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
// 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:flutter_devicelab/framework/devices.dart';
|
||||
import 'package:flutter_devicelab/framework/framework.dart';
|
||||
import 'package:flutter_devicelab/tasks/native_assets_test.dart';
|
||||
|
||||
Future<void> main() async {
|
||||
await task(() async {
|
||||
deviceOperatingSystem = DeviceOperatingSystem.android;
|
||||
return createNativeAssetsTest()();
|
||||
});
|
||||
}
|
||||
@@ -339,6 +339,8 @@ Future<int> exec(
|
||||
Map<String, String>? environment,
|
||||
bool canFail = false, // as in, whether failures are ok. False means that they are fatal.
|
||||
String? workingDirectory,
|
||||
StringBuffer? output, // if not null, the stdout will be written here
|
||||
StringBuffer? stderr, // if not null, the stderr will be written here
|
||||
}) async {
|
||||
return _execute(
|
||||
executable,
|
||||
@@ -346,6 +348,8 @@ Future<int> exec(
|
||||
environment: environment,
|
||||
canFail : canFail,
|
||||
workingDirectory: workingDirectory,
|
||||
output: output,
|
||||
stderr: stderr,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -898,3 +902,30 @@ Future<T> retry<T>(
|
||||
await Future<void>.delayed(delayDuration);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> createFfiPackage(String name, Directory parent) async {
|
||||
await inDirectory(parent, () async {
|
||||
await flutter(
|
||||
'create',
|
||||
options: <String>[
|
||||
'--no-pub',
|
||||
'--org',
|
||||
'io.flutter.devicelab',
|
||||
'--template=package_ffi',
|
||||
name,
|
||||
],
|
||||
);
|
||||
await _pinDependencies(
|
||||
File(path.join(parent.path, name, 'pubspec.yaml')),
|
||||
);
|
||||
await _pinDependencies(
|
||||
File(path.join(parent.path, name, 'example', 'pubspec.yaml')),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _pinDependencies(File pubspecFile) async {
|
||||
final String oldPubspec = await pubspecFile.readAsString();
|
||||
final String newPubspec = oldPubspec.replaceAll(': ^', ': ');
|
||||
await pubspecFile.writeAsString(newPubspec);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user