Unverified Commit 402a5455 authored by Daco Harkes's avatar Daco Harkes Committed by GitHub

Don't build native assets in `flutter build bundle` (#136641)

Closes: https://github.com/flutter/flutter/issues/136547
parent 4b7b4d41
...@@ -967,6 +967,20 @@ const String kSdkRoot = 'SdkRoot'; ...@@ -967,6 +967,20 @@ const String kSdkRoot = 'SdkRoot';
/// Whether to enable Dart obfuscation and where to save the symbol map. /// Whether to enable Dart obfuscation and where to save the symbol map.
const String kDartObfuscation = 'DartObfuscation'; const String kDartObfuscation = 'DartObfuscation';
/// Whether to enable Native Assets.
///
/// If true, native assets are built and the mapping for native assets lookup
/// at runtime is embedded in the kernel file.
///
/// If false, native assets are not built, and an empty mapping is embedded in
/// the kernel file. Used for targets that trigger kernel builds but
/// are not OS/architecture specific.
///
/// Supported values are 'true' and 'false'.
///
/// Defaults to 'true'.
const String kNativeAssets = 'NativeAssets';
/// An output directory where one or more code-size measurements may be written. /// An output directory where one or more code-size measurements may be written.
const String kCodeSizeDirectory = 'CodeSizeDirectory'; const String kCodeSizeDirectory = 'CodeSizeDirectory';
......
...@@ -48,14 +48,20 @@ class NativeAssets extends Target { ...@@ -48,14 +48,20 @@ class NativeAssets extends Target {
@override @override
Future<void> build(Environment environment) async { Future<void> build(Environment environment) async {
final String? nativeAssetsEnvironment = environment.defines[kNativeAssets];
final List<Uri> dependencies;
final FileSystem fileSystem = environment.fileSystem;
final File nativeAssetsFile = environment.buildDir.childFile('native_assets.yaml');
if (nativeAssetsEnvironment == 'false') {
dependencies = <Uri>[];
await writeNativeAssetsYaml(<Asset>[], environment.buildDir.uri, fileSystem);
} else {
final String? targetPlatformEnvironment = environment.defines[kTargetPlatform]; final String? targetPlatformEnvironment = environment.defines[kTargetPlatform];
if (targetPlatformEnvironment == null) { if (targetPlatformEnvironment == null) {
throw MissingDefineException(kTargetPlatform, name); throw MissingDefineException(kTargetPlatform, name);
} }
final TargetPlatform targetPlatform = getTargetPlatformForName(targetPlatformEnvironment); final TargetPlatform targetPlatform = getTargetPlatformForName(targetPlatformEnvironment);
final Uri projectUri = environment.projectDir.uri; final Uri projectUri = environment.projectDir.uri;
final FileSystem fileSystem = environment.fileSystem;
final File packagesFile = fileSystem final File packagesFile = fileSystem
.directory(projectUri) .directory(projectUri)
.childDirectory('.dart_tool') .childDirectory('.dart_tool')
...@@ -72,7 +78,7 @@ class NativeAssets extends Target { ...@@ -72,7 +78,7 @@ class NativeAssets extends Target {
environment.logger, environment.logger,
); );
final List<Uri> dependencies;
switch (targetPlatform) { switch (targetPlatform) {
case TargetPlatform.ios: case TargetPlatform.ios:
final String? iosArchsEnvironment = environment.defines[kIosArchs]; final String? iosArchsEnvironment = environment.defines[kIosArchs];
...@@ -197,8 +203,8 @@ class NativeAssets extends Target { ...@@ -197,8 +203,8 @@ class NativeAssets extends Target {
await writeNativeAssetsYaml(<Asset>[], environment.buildDir.uri, fileSystem); await writeNativeAssetsYaml(<Asset>[], environment.buildDir.uri, fileSystem);
dependencies = <Uri>[]; dependencies = <Uri>[];
} }
}
final File nativeAssetsFile = environment.buildDir.childFile('native_assets.yaml');
final Depfile depfile = Depfile( final Depfile depfile = Depfile(
<File>[ <File>[
for (final Uri dependency in dependencies) fileSystem.file(dependency), for (final Uri dependency in dependencies) fileSystem.file(dependency),
......
...@@ -26,7 +26,11 @@ class BundleBuilder { ...@@ -26,7 +26,11 @@ class BundleBuilder {
/// Builds the bundle for the given target platform. /// Builds the bundle for the given target platform.
/// ///
/// The default `mainPath` is `lib/main.dart`. /// The default `mainPath` is `lib/main.dart`.
/// The default `manifestPath` is `pubspec.yaml` /// The default `manifestPath` is `pubspec.yaml`.
///
/// If [buildNativeAssets], native assets are built and the mapping for native
/// assets lookup at runtime is embedded in the kernel file, otherwise an
/// empty native assets mapping is embedded in the kernel file.
Future<void> build({ Future<void> build({
required TargetPlatform platform, required TargetPlatform platform,
required BuildInfo buildInfo, required BuildInfo buildInfo,
...@@ -36,6 +40,7 @@ class BundleBuilder { ...@@ -36,6 +40,7 @@ class BundleBuilder {
String? applicationKernelFilePath, String? applicationKernelFilePath,
String? depfilePath, String? depfilePath,
String? assetDirPath, String? assetDirPath,
bool buildNativeAssets = true,
@visibleForTesting BuildSystem? buildSystem, @visibleForTesting BuildSystem? buildSystem,
}) async { }) async {
project ??= FlutterProject.current(); project ??= FlutterProject.current();
...@@ -60,6 +65,7 @@ class BundleBuilder { ...@@ -60,6 +65,7 @@ class BundleBuilder {
kTargetFile: mainPath, kTargetFile: mainPath,
kDeferredComponents: 'false', kDeferredComponents: 'false',
...buildInfo.toBuildSystemEnvironment(), ...buildInfo.toBuildSystemEnvironment(),
if (!buildNativeAssets) kNativeAssets: 'false'
}, },
artifacts: globals.artifacts!, artifacts: globals.artifacts!,
fileSystem: globals.fs, fileSystem: globals.fs,
......
...@@ -132,6 +132,7 @@ class BuildBundleCommand extends BuildSubCommand { ...@@ -132,6 +132,7 @@ class BuildBundleCommand extends BuildSubCommand {
mainPath: targetFile, mainPath: targetFile,
depfilePath: stringArg('depfile'), depfilePath: stringArg('depfile'),
assetDirPath: stringArg('asset-dir'), assetDirPath: stringArg('asset-dir'),
buildNativeAssets: false,
); );
return FlutterCommandResult.success(); return FlutterCommandResult.success();
} }
......
...@@ -252,6 +252,7 @@ void main() { ...@@ -252,6 +252,7 @@ void main() {
kIconTreeShakerFlag: 'false', kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false', kDeferredComponents: 'false',
kDartObfuscation: 'false', kDartObfuscation: 'false',
kNativeAssets: 'false',
}); });
}), }),
FileSystem: fsFactory, FileSystem: fsFactory,
...@@ -285,6 +286,7 @@ void main() { ...@@ -285,6 +286,7 @@ void main() {
kIconTreeShakerFlag: 'false', kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false', kDeferredComponents: 'false',
kDartObfuscation: 'false', kDartObfuscation: 'false',
kNativeAssets: 'false',
}); });
}), }),
FileSystem: fsFactory, FileSystem: fsFactory,
...@@ -317,6 +319,7 @@ void main() { ...@@ -317,6 +319,7 @@ void main() {
kIconTreeShakerFlag: 'false', kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false', kDeferredComponents: 'false',
kDartObfuscation: 'false', kDartObfuscation: 'false',
kNativeAssets: 'false',
}); });
}), }),
FileSystem: fsFactory, FileSystem: fsFactory,
...@@ -350,6 +353,7 @@ void main() { ...@@ -350,6 +353,7 @@ void main() {
kIconTreeShakerFlag: 'false', kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false', kDeferredComponents: 'false',
kDartObfuscation: 'false', kDartObfuscation: 'false',
kNativeAssets: 'false',
}); });
}), }),
FileSystem: fsFactory, FileSystem: fsFactory,
...@@ -383,6 +387,7 @@ void main() { ...@@ -383,6 +387,7 @@ void main() {
kIconTreeShakerFlag: 'false', kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false', kDeferredComponents: 'false',
kDartObfuscation: 'false', kDartObfuscation: 'false',
kNativeAssets: 'false',
}); });
}), }),
FileSystem: fsFactory, FileSystem: fsFactory,
...@@ -416,6 +421,7 @@ void main() { ...@@ -416,6 +421,7 @@ void main() {
kIconTreeShakerFlag: 'false', kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false', kDeferredComponents: 'false',
kDartObfuscation: 'false', kDartObfuscation: 'false',
kNativeAssets: 'false',
}); });
}), }),
FileSystem: fsFactory, FileSystem: fsFactory,
...@@ -457,6 +463,7 @@ void main() { ...@@ -457,6 +463,7 @@ void main() {
kIconTreeShakerFlag: 'false', kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false', kDeferredComponents: 'false',
kDartObfuscation: 'false', kDartObfuscation: 'false',
kNativeAssets: 'false',
}); });
}), }),
FileSystem: fsFactory, FileSystem: fsFactory,
...@@ -498,6 +505,7 @@ void main() { ...@@ -498,6 +505,7 @@ void main() {
kIconTreeShakerFlag: 'false', kIconTreeShakerFlag: 'false',
kDeferredComponents: 'false', kDeferredComponents: 'false',
kDartObfuscation: 'false', kDartObfuscation: 'false',
kNativeAssets: 'false',
}); });
}), }),
FileSystem: fsFactory, FileSystem: fsFactory,
...@@ -516,7 +524,7 @@ class FakeBundleBuilder extends Fake implements BundleBuilder { ...@@ -516,7 +524,7 @@ class FakeBundleBuilder extends Fake implements BundleBuilder {
String? applicationKernelFilePath, String? applicationKernelFilePath,
String? depfilePath, String? depfilePath,
String? assetDirPath, String? assetDirPath,
Uri? nativeAssets, bool buildNativeAssets = true,
@visibleForTesting BuildSystem? buildSystem, @visibleForTesting BuildSystem? buildSystem,
}) async {} }) async {}
} }
...@@ -645,6 +645,7 @@ class FakeBundleBuilder extends Fake implements BundleBuilder { ...@@ -645,6 +645,7 @@ class FakeBundleBuilder extends Fake implements BundleBuilder {
String? depfilePath, String? depfilePath,
String? assetDirPath, String? assetDirPath,
Uri? nativeAssets, Uri? nativeAssets,
bool buildNativeAssets = true,
@visibleForTesting BuildSystem? buildSystem @visibleForTesting BuildSystem? buildSystem
}) async {} }) async {}
} }
...@@ -196,7 +196,7 @@ class FakeBundleBuilder extends Fake implements BundleBuilder { ...@@ -196,7 +196,7 @@ class FakeBundleBuilder extends Fake implements BundleBuilder {
String? applicationKernelFilePath, String? applicationKernelFilePath,
String? depfilePath, String? depfilePath,
String? assetDirPath, String? assetDirPath,
Uri? nativeAssets, bool buildNativeAssets = true,
@visibleForTesting BuildSystem? buildSystem @visibleForTesting BuildSystem? buildSystem
}) async { }) async {
final Directory assetDirectory = fileSystem final Directory assetDirectory = fileSystem
......
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