Reland double gzip wrapping NOTICES to reduce on-disk installed space (#80897)

This commit is contained in:
xster
2021-04-26 22:35:45 -07:00
committed by GitHub
parent a8e41f8206
commit ff2dde2c7f
19 changed files with 202 additions and 13 deletions

View File

@@ -10,6 +10,7 @@ import 'package:flutter_tools/src/base/context.dart';
import 'package:flutter_tools/src/base/file_system.dart' as libfs;
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/bundle.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/context_runner.dart';
import 'package:flutter_tools/src/devfs.dart';
@@ -61,6 +62,7 @@ Future<void> run(List<String> args) async {
manifestPath: argResults[_kOptionManifest] as String ?? defaultManifestPath,
assetDirPath: assetDir,
packagesPath: argResults[_kOptionPackages] as String,
targetPlatform: TargetPlatform.fuchsia_arm64 // This is not arch specific.
);
if (assets == null) {

View File

@@ -82,6 +82,7 @@ abstract class AssetBundle {
String assetDirPath,
@required String packagesPath,
bool deferredComponentsEnabled = false,
TargetPlatform targetPlatform,
});
}
@@ -141,6 +142,13 @@ class ManifestAssetBundle implements AssetBundle {
static const String _kAssetManifestJson = 'AssetManifest.json';
static const String _kNoticeFile = 'NOTICES';
// Comically, this can't be name with the more common .gz file extension
// because when it's part of an AAR and brought into another APK via gradle,
// gradle individually traverses all the files of the AAR and unzips .gz
// files (b/37117906). A less common .Z extension still describes how the
// file is formatted if users want to manually inspect the application
// bundle and is recognized by default file handlers on OS such as macOS.˚
static const String _kNoticeZippedFile = 'NOTICES.Z';
@override
bool wasBuiltOnce() => _lastBuildTimestamp != null;
@@ -180,6 +188,7 @@ class ManifestAssetBundle implements AssetBundle {
String assetDirPath,
@required String packagesPath,
bool deferredComponentsEnabled = false,
TargetPlatform targetPlatform,
}) async {
assetDirPath ??= getAssetBuildDirectory();
FlutterProject flutterProject;
@@ -407,7 +416,6 @@ class ManifestAssetBundle implements AssetBundle {
return 1;
}
final DevFSStringContent licenses = DevFSStringContent(licenseResult.combinedLicenses);
additionalDependencies = licenseResult.dependencies;
if (wildcardDirectories.isNotEmpty) {
@@ -425,7 +433,7 @@ class ManifestAssetBundle implements AssetBundle {
_setIfChanged(_kAssetManifestJson, assetManifest);
_setIfChanged(kFontManifestJson, fontManifest);
_setIfChanged(_kNoticeFile, licenses);
_setLicenseIfChanged(licenseResult.combinedLicenses, targetPlatform);
return 0;
}
@@ -443,6 +451,35 @@ class ManifestAssetBundle implements AssetBundle {
}
}
void _setLicenseIfChanged(
String combinedLicenses,
TargetPlatform targetPlatform,
) {
// On the web, don't compress the NOTICES file since the client doesn't have
// dart:io to decompress it. So use the standard _setIfChanged to check if
// the strings still match.
if (targetPlatform == TargetPlatform.web_javascript) {
_setIfChanged(_kNoticeFile, DevFSStringContent(combinedLicenses));
return;
}
// On other platforms, let the NOTICES file be compressed. But use a
// specialized DevFSStringCompressingBytesContent class to compare
// the uncompressed strings to not incur decompression/decoding while making
// the comparison.
if (!entries.containsKey(_kNoticeZippedFile) ||
!(entries[_kNoticeZippedFile] as DevFSStringCompressingBytesContent)
.equals(combinedLicenses)) {
entries[_kNoticeZippedFile] = DevFSStringCompressingBytesContent(
combinedLicenses,
// A zlib dictionary is a hinting string sequence with the most
// likely string occurrences at the end. This ends up just being
// common English words with domain specific words like copyright.
hintString: 'copyrightsoftwaretothisinandorofthe',
);
}
}
List<_Asset> _getMaterialAssets() {
final List<_Asset> result = <_Asset>[];
for (final Map<String, Object> family in kMaterialFonts) {

View File

@@ -102,7 +102,8 @@ export 'dart:io'
systemEncoding,
WebSocket,
WebSocketException,
WebSocketTransformer;
WebSocketTransformer,
ZLibEncoder;
/// Exits the process with the given [exitCode].
typedef ExitFunction = void Function(int exitCode);

View File

@@ -58,6 +58,7 @@ Future<Depfile> copyAssets(Environment environment, Directory outputDirectory, {
packagesPath: environment.projectDir.childFile('.packages').path,
assetDirPath: null,
deferredComponentsEnabled: environment.defines[kDeferredComponents] == 'true',
targetPlatform: targetPlatform,
);
if (resultCode != 0) {
throw Exception('Failed to bundle asset files.');

View File

@@ -182,6 +182,7 @@ Future<AssetBundle> buildAssets({
String manifestPath,
String assetDirPath,
@required String packagesPath,
TargetPlatform targetPlatform,
}) async {
assetDirPath ??= getAssetBuildDirectory();
packagesPath ??= globals.fs.path.absolute(packagesPath);
@@ -192,6 +193,7 @@ Future<AssetBundle> buildAssets({
manifestPath: manifestPath,
assetDirPath: assetDirPath,
packagesPath: packagesPath,
targetPlatform: targetPlatform,
);
if (result != 0) {
return null;

View File

@@ -203,6 +203,61 @@ class DevFSStringContent extends DevFSByteContent {
}
}
/// A string compressing DevFSContent.
///
/// A specialized DevFSContent similar to DevFSByteContent where the contents
/// are the compressed bytes of a string. Its difference is that the original
/// uncompressed string can be compared with directly without the indirection
/// of a compute-expensive uncompress/decode and compress/encode to compare
/// the strings.
///
/// The `hintString` parameter is a zlib dictionary hinting mechanism to suggest
/// the most common string occurrences to potentially assist with compression.
class DevFSStringCompressingBytesContent extends DevFSContent {
DevFSStringCompressingBytesContent(this._string, { String hintString })
: _compressor = ZLibEncoder(
dictionary: hintString == null
? null
: utf8.encode(hintString),
gzip: true,
level: 9,
);
final String _string;
final ZLibEncoder _compressor;
final DateTime _modificationTime = DateTime.now();
List<int> _bytes;
bool _isModified = true;
List<int> get bytes => _bytes ??= _compressor.convert(utf8.encode(_string));
/// Return true only once so that the content is written to the device only once.
@override
bool get isModified {
final bool modified = _isModified;
_isModified = false;
return modified;
}
@override
bool isModifiedAfter(DateTime time) {
return time == null || _modificationTime.isAfter(time);
}
@override
int get size => bytes.length;
@override
Future<List<int>> contentsAsBytes() async => bytes;
@override
Stream<List<int>> contentsAsStream() => Stream<List<int>>.value(bytes);
/// This checks the source string with another string.
bool equals(String string) => _string == string;
}
class DevFSException implements Exception {
DevFSException(this.message, [this.error, this.stackTrace]);
final String message;

View File

@@ -483,7 +483,10 @@ class ResidentWebRunner extends ResidentRunner {
final bool rebuildBundle = assetBundle.needsBuild();
if (rebuildBundle) {
_logger.printTrace('Updating assets');
final int result = await assetBundle.build(packagesPath: debuggingOptions.buildInfo.packagesPath);
final int result = await assetBundle.build(
packagesPath: debuggingOptions.buildInfo.packagesPath,
targetPlatform: TargetPlatform.web_javascript,
);
if (result != 0) {
return UpdateFSReport(success: false);
}

View File

@@ -88,7 +88,7 @@ flutter:
expect(fileSystem.file('${environment.buildDir.path}/flutter_assets/AssetManifest.json'), exists);
expect(fileSystem.file('${environment.buildDir.path}/flutter_assets/FontManifest.json'), exists);
expect(fileSystem.file('${environment.buildDir.path}/flutter_assets/NOTICES'), exists);
expect(fileSystem.file('${environment.buildDir.path}/flutter_assets/NOTICES.Z'), exists);
// See https://github.com/flutter/flutter/issues/35293
expect(fileSystem.file('${environment.buildDir.path}/flutter_assets/assets/foo/bar.png'), exists);
// See https://github.com/flutter/flutter/issues/46163

View File

@@ -111,6 +111,16 @@ void main() {
expect(content.isModified, isFalse);
});
testWithoutContext('DevFSStringCompressingBytesContent', () {
final DevFSStringCompressingBytesContent content =
DevFSStringCompressingBytesContent('uncompressed string');
expect(content.equals('uncompressed string'), isTrue);
expect(content.bytes, isNotNull);
expect(content.isModified, isTrue);
expect(content.isModified, isFalse);
});
testWithoutContext('DevFS create throws a DevFSException when vmservice disconnects unexpectedly', () async {
final FileSystem fileSystem = MemoryFileSystem.test();
final OperatingSystemUtils osUtils = MockOperatingSystemUtils();

View File

@@ -132,7 +132,7 @@ flutter:
'C:/build/flutter_assets/assets/foo.png',
'C:/build/flutter_assets/AssetManifest.json',
'C:/build/flutter_assets/FontManifest.json',
'C:/build/flutter_assets/NOTICES',
'C:/build/flutter_assets/NOTICES.Z',
'C:/winuwp/flutter/ephemeral/flutter_windows_winuwp.dll',
'C:/winuwp/flutter/ephemeral/flutter_windows_winuwp.dll.pdb',
'C:/winuwp/flutter/ephemeral/icudtl.dat'