Skip to content

Commit b47da6c

Browse files
authored
Add support for binary compression of dynamic patches by the flutter tool. (#27754)
This reduces the typical dynamic patch size by more than 10x. Also see flutter/engine#7777 for decompression support in the runtime.
1 parent 8101862 commit b47da6c

File tree

3 files changed

+476
-2
lines changed

3 files changed

+476
-2
lines changed

packages/flutter_tools/lib/src/android/gradle.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:meta/meta.dart';
1010
import '../android/android_sdk.dart';
1111
import '../application_package.dart';
1212
import '../artifacts.dart';
13+
import '../base/bsdiff.dart';
1314
import '../base/common.dart';
1415
import '../base/file_system.dart';
1516
import '../base/logger.dart';
@@ -498,6 +499,7 @@ Future<void> _buildGradleProjectV2(
498499
throwToolExit('Error: Could not find baseline package ${baselineApkFile.path}.');
499500

500501
printStatus('Found baseline package ${baselineApkFile.path}.');
502+
printStatus('Creating dynamic patch...');
501503
final Archive newApk = ZipDecoder().decodeBytes(apkFile.readAsBytesSync());
502504
final Archive oldApk = ZipDecoder().decodeBytes(baselineApkFile.readAsBytesSync());
503505

@@ -519,7 +521,14 @@ Future<void> _buildGradleProjectV2(
519521
throwToolExit("Error: Dynamic patching doesn't support changes to ${newFile.name}.");
520522

521523
final String name = fs.path.relative(newFile.name, from: 'assets/');
522-
update.addFile(ArchiveFile(name, newFile.content.length, newFile.content));
524+
if (name.contains('_snapshot_')) {
525+
final List<int> diff = bsdiff(oldFile.content, newFile.content);
526+
final int ratio = 100 * diff.length ~/ newFile.content.length;
527+
printStatus('Deflated $name by ${ratio == 0 ? 99 : 100 - ratio}%');
528+
update.addFile(ArchiveFile(name + '.bzdiff40', diff.length, diff));
529+
} else {
530+
update.addFile(ArchiveFile(name, newFile.content.length, newFile.content));
531+
}
523532
}
524533

525534
File updateFile;
@@ -567,7 +576,8 @@ Future<void> _buildGradleProjectV2(
567576

568577
updateFile.parent.createSync(recursive: true);
569578
updateFile.writeAsBytesSync(ZipEncoder().encode(update), flush: true);
570-
printStatus('Created dynamic patch ${updateFile.path}.');
579+
final String patchSize = getSizeAsMB(updateFile.lengthSync());
580+
printStatus('Created dynamic patch ${updateFile.path} ($patchSize).');
571581
}
572582
} else {
573583
final File bundleFile = _findBundleFile(project, buildInfo);

0 commit comments

Comments
 (0)