Native assets support for Android Add2app (#140802)
Support for FFI calls with @Native external functions through Native assets on Android add to app. 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 add2app The `.so` files are bundled with the same mechanism that bundles `libapp.so`.
This commit is contained in:
@@ -171,61 +171,43 @@ void main() {
|
||||
},
|
||||
);
|
||||
|
||||
for (final bool hasAssets in <bool>[true, false]) {
|
||||
final String withOrWithout = hasAssets ? 'with' : 'without';
|
||||
testUsingContext(
|
||||
'flutter build $withOrWithout native assets',
|
||||
overrides: <Type, Generator>{
|
||||
FileSystem: () => fileSystem,
|
||||
ProcessManager: () => processManager,
|
||||
FeatureFlags: () => TestFeatureFlags(
|
||||
isNativeAssetsEnabled: true,
|
||||
),
|
||||
},
|
||||
() async {
|
||||
await createPackageConfig(androidEnvironment);
|
||||
await fileSystem.file('libfoo.so').create();
|
||||
|
||||
for (final bool isAndroidLibrary in <bool>[true, false]) {
|
||||
for (final bool hasAssets in <bool>[true, false]) {
|
||||
final String buildType = isAndroidLibrary ? 'aar' : 'not-aar';
|
||||
final String withOrWithout = hasAssets ? 'with' : 'without';
|
||||
final String throwsOrDoesntThrow =
|
||||
(isAndroidLibrary && hasAssets) ? 'throws' : 'does not throw';
|
||||
testUsingContext(
|
||||
'flutter build $buildType $withOrWithout native assets $throwsOrDoesntThrow',
|
||||
overrides: <Type, Generator>{
|
||||
FileSystem: () => fileSystem,
|
||||
ProcessManager: () => processManager,
|
||||
FeatureFlags: () => TestFeatureFlags(
|
||||
isNativeAssetsEnabled: true,
|
||||
),
|
||||
},
|
||||
() async {
|
||||
await createPackageConfig(androidEnvironment);
|
||||
await fileSystem.file('libfoo.so').create();
|
||||
|
||||
final NativeAssetsBuildRunner buildRunner =
|
||||
FakeNativeAssetsBuildRunner(
|
||||
packagesWithNativeAssetsResult: <Package>[
|
||||
Package('foo', androidEnvironment.buildDir.uri)
|
||||
],
|
||||
buildResult:
|
||||
FakeNativeAssetsBuilderResult(assets: <native_assets_cli.Asset>[
|
||||
if (hasAssets)
|
||||
native_assets_cli.Asset(
|
||||
id: 'package:foo/foo.dart',
|
||||
linkMode: native_assets_cli.LinkMode.dynamic,
|
||||
target: native_assets_cli.Target.androidArm64,
|
||||
path: native_assets_cli.AssetAbsolutePath(
|
||||
Uri.file('libfoo.so'),
|
||||
),
|
||||
)
|
||||
], dependencies: <Uri>[
|
||||
Uri.file('src/foo.c'),
|
||||
]),
|
||||
);
|
||||
if (isAndroidLibrary) {
|
||||
androidEnvironment.defines[kIsAndroidLibrary] = 'true';
|
||||
}
|
||||
if (hasAssets && isAndroidLibrary) {
|
||||
expect(
|
||||
NativeAssets(buildRunner: buildRunner).build(androidEnvironment),
|
||||
throwsToolExit(),
|
||||
);
|
||||
} else {
|
||||
await NativeAssets(buildRunner: buildRunner)
|
||||
.build(androidEnvironment);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
final NativeAssetsBuildRunner buildRunner = FakeNativeAssetsBuildRunner(
|
||||
packagesWithNativeAssetsResult: <Package>[
|
||||
Package('foo', androidEnvironment.buildDir.uri)
|
||||
],
|
||||
buildResult:
|
||||
FakeNativeAssetsBuilderResult(assets: <native_assets_cli.Asset>[
|
||||
if (hasAssets)
|
||||
native_assets_cli.Asset(
|
||||
id: 'package:foo/foo.dart',
|
||||
linkMode: native_assets_cli.LinkMode.dynamic,
|
||||
target: native_assets_cli.Target.androidArm64,
|
||||
path: native_assets_cli.AssetAbsolutePath(
|
||||
Uri.file('libfoo.so'),
|
||||
),
|
||||
)
|
||||
], dependencies: <Uri>[
|
||||
Uri.file('src/foo.c'),
|
||||
]),
|
||||
);
|
||||
await NativeAssets(buildRunner: buildRunner).build(androidEnvironment);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user