[flutter_tools] support code size tooling on iOS, linux, windows, macOS, and Android on Windows (#63610)

Adds support for size analysis on iOS, macOS, linux, and Windows - using an uncompressed directory based approach. The output format is not currently specified.

Adds support for size analysis on android on windows, switching to package:archive

Updates the console format to display as a tree, allowing longer paths. Increases the number of dart libraries shown (to avoid only ever printing the flutter/dart:ui libraries, which dominate the size)
This commit is contained in:
Jonah Williams
2020-08-25 10:00:24 -07:00
committed by GitHub
parent dd0c881275
commit 059de1537e
30 changed files with 1013 additions and 398 deletions

View File

@@ -11,6 +11,13 @@ const String kSupportedAbis = 'https://flutter.dev/docs/deployment/android#what-
/// Validates that the build mode and build number are valid for a given build.
void validateBuild(AndroidBuildInfo androidBuildInfo) {
final BuildInfo buildInfo = androidBuildInfo.buildInfo;
if (buildInfo.codeSizeDirectory != null && androidBuildInfo.targetArchs.length > 1) {
throwToolExit(
'Cannot perform code size analysis when building for multiple ABIs. '
'Specify one of android-arm, android-arm64, or android-x64 in the '
'--target-plaform flag.'
);
}
if (buildInfo.mode.isPrecompiled && androidBuildInfo.targetArchs.contains(AndroidArch.x86)) {
throwToolExit(
'Cannot build ${androidBuildInfo.buildInfo.mode.name} mode for x86 ABI.\n'

View File

@@ -357,6 +357,9 @@ Future<void> buildGradleApp({
if (androidBuildInfo.buildInfo.performanceMeasurementFile != null) {
command.add('-Pperformance-measurement-file=${androidBuildInfo.buildInfo.performanceMeasurementFile}');
}
if (buildInfo.codeSizeDirectory != null) {
command.add('-Pcode-size-directory=${buildInfo.codeSizeDirectory}');
}
command.add(assembleTask);
GradleHandledError detectedGradleError;
@@ -467,6 +470,10 @@ Future<void> buildGradleApp({
? '' // Don't display the size when building a debug variant.
: ' (${getSizeAsMB(bundleFile.lengthSync())})';
if (buildInfo.codeSizeDirectory != null) {
await _performCodeSizeAnalysis('aab', bundleFile, androidBuildInfo);
}
globals.printStatus(
'$successMark Built ${globals.fs.path.relative(bundleFile.path)}$appSize.',
color: TerminalColor.green,
@@ -502,26 +509,41 @@ Future<void> buildGradleApp({
color: TerminalColor.green,
);
// Call size analyzer if --analyze-size flag was provided.
if (buildInfo.analyzeSize != null && !globals.platform.isWindows) {
final SizeAnalyzer sizeAnalyzer = SizeAnalyzer(
fileSystem: globals.fs,
logger: globals.logger,
processUtils: ProcessUtils.instance,
);
final Map<String, Object> output = await sizeAnalyzer.analyzeApkSizeAndAotSnapshot(
apk: apkFile,
aotSnapshot: globals.fs.file(buildInfo.analyzeSize),
);
final File outputFile = globals.fsUtils.getUniqueFile(globals.fs.currentDirectory, 'apk-analysis', 'json')
..writeAsStringSync(jsonEncode(output));
// This message is used as a sentinel in analyze_apk_size_test.dart
globals.printStatus(
'A summary of your APK analysis can be found at: ${outputFile.path}',
);
if (buildInfo.codeSizeDirectory != null) {
await _performCodeSizeAnalysis('apk', apkFile, androidBuildInfo);
}
}
Future<void> _performCodeSizeAnalysis(
String kind,
File zipFile,
AndroidBuildInfo androidBuildInfo,
) async {
final SizeAnalyzer sizeAnalyzer = SizeAnalyzer(
fileSystem: globals.fs,
logger: globals.logger,
);
final String archName = getNameForAndroidArch(androidBuildInfo.targetArchs.single);
final BuildInfo buildInfo = androidBuildInfo.buildInfo;
final File aotSnapshot = globals.fs.directory(buildInfo.codeSizeDirectory)
.childFile('snapshot.$archName.json');
final File precompilerTrace = globals.fs.directory(buildInfo.codeSizeDirectory)
.childFile('trace.$archName.json');
final Map<String, Object> output = await sizeAnalyzer.analyzeZipSizeAndAotSnapshot(
zipFile: zipFile,
aotSnapshot: aotSnapshot,
precompilerTrace: precompilerTrace,
kind: kind,
);
final File outputFile = globals.fsUtils.getUniqueFile(
globals.fs.directory(getBuildDirectory()),'$kind-code-size-analysis', 'json',
)..writeAsStringSync(jsonEncode(output));
// This message is used as a sentinel in analyze_apk_size_test.dart
globals.printStatus(
'A summary of your ${kind.toUpperCase()} analysis can be found at: ${outputFile.path}',
);
}
/// Builds AAR and POM files.
///
/// * [project] is typically [FlutterProject.current()].