forked from firka/flutter
Enable R8 (#40453)
This commit is contained in:
@@ -306,6 +306,10 @@ Future<String> _initializeGradle(FlutterProject project) async {
|
||||
final Status status = logger.startProgress('Initializing gradle...',
|
||||
timeout: timeoutConfiguration.slowOperation);
|
||||
|
||||
|
||||
// Update the project if needed.
|
||||
// TODO(egarciad): https://github.com/flutter/flutter/issues/40460.
|
||||
migrateToR8(android);
|
||||
injectGradleWrapperIfNeeded(android);
|
||||
|
||||
final String gradle = _locateGradlewExecutable(android);
|
||||
@@ -335,6 +339,31 @@ Future<String> _initializeGradle(FlutterProject project) async {
|
||||
return gradle;
|
||||
}
|
||||
|
||||
/// Migrates the Android's [directory] to R8.
|
||||
/// https://developer.android.com/studio/build/shrink-code
|
||||
@visibleForTesting
|
||||
void migrateToR8(Directory directory) {
|
||||
final File gradleProperties = directory.childFile('gradle.properties');
|
||||
if (!gradleProperties.existsSync()) {
|
||||
throwToolExit('Expected file ${gradleProperties.path}.');
|
||||
}
|
||||
final String propertiesContent = gradleProperties.readAsStringSync();
|
||||
if (propertiesContent.contains('android.enableR8')) {
|
||||
printTrace('gradle.properties already sets `android.enableR8`');
|
||||
return;
|
||||
}
|
||||
printTrace('set `android.enableR8=true` in gradle.properties');
|
||||
try {
|
||||
gradleProperties
|
||||
.writeAsStringSync('android.enableR8=true\n', mode: FileMode.append);
|
||||
} on FileSystemException {
|
||||
throwToolExit(
|
||||
'The tool failed to add `android.enableR8=true` to ${gradleProperties.path}. '
|
||||
'Please update the file manually and try this command again.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Injects the Gradle wrapper files if any of these files don't exist in [directory].
|
||||
void injectGradleWrapperIfNeeded(Directory directory) {
|
||||
copyDirectorySync(
|
||||
@@ -754,8 +783,8 @@ Future<void> _buildGradleProjectV2(
|
||||
if (androidBuildInfo.splitPerAbi) {
|
||||
command.add('-Psplit-per-abi=true');
|
||||
}
|
||||
if (androidBuildInfo.proguard) {
|
||||
command.add('-Pproguard=true');
|
||||
if (androidBuildInfo.shrink) {
|
||||
command.add('-Pshrink=true');
|
||||
}
|
||||
if (androidBuildInfo.targetArchs.isNotEmpty) {
|
||||
final String targetPlatforms = androidBuildInfo.targetArchs
|
||||
@@ -772,7 +801,7 @@ Future<void> _buildGradleProjectV2(
|
||||
}
|
||||
command.add(assembleTask);
|
||||
bool potentialAndroidXFailure = false;
|
||||
bool potentialProguardFailure = false;
|
||||
bool potentialR8Failure = false;
|
||||
final Stopwatch sw = Stopwatch()..start();
|
||||
int exitCode = 1;
|
||||
try {
|
||||
@@ -789,10 +818,10 @@ Future<void> _buildGradleProjectV2(
|
||||
if (!isAndroidXPluginWarning && androidXFailureRegex.hasMatch(line)) {
|
||||
potentialAndroidXFailure = true;
|
||||
}
|
||||
// Proguard errors include this url.
|
||||
if (!potentialProguardFailure && androidBuildInfo.proguard &&
|
||||
line.contains('http://proguard.sourceforge.net')) {
|
||||
potentialProguardFailure = true;
|
||||
// R8 errors include references to this package.
|
||||
if (!potentialR8Failure && androidBuildInfo.shrink &&
|
||||
line.contains('com.android.tools.r8')) {
|
||||
potentialR8Failure = true;
|
||||
}
|
||||
// Always print the full line in verbose mode.
|
||||
if (logger.isVerbose) {
|
||||
@@ -808,12 +837,12 @@ Future<void> _buildGradleProjectV2(
|
||||
}
|
||||
|
||||
if (exitCode != 0) {
|
||||
if (potentialProguardFailure) {
|
||||
if (potentialR8Failure) {
|
||||
final String exclamationMark = terminal.color('[!]', TerminalColor.red);
|
||||
printStatus('$exclamationMark Proguard may have failed to optimize the Java bytecode.', emphasis: true);
|
||||
printStatus('To disable proguard, pass the `--no-proguard` flag to this command.', indent: 4);
|
||||
printStatus('To learn more about Proguard, see: https://flutter.dev/docs/deployment/android#enabling-proguard', indent: 4);
|
||||
BuildEvent('proguard-failure').send();
|
||||
printStatus('$exclamationMark The shrinker may have failed to optimize the Java bytecode.', emphasis: true);
|
||||
printStatus('To disable the shrinker, pass the `--no-shrink` flag to this command.', indent: 4);
|
||||
printStatus('To learn more, see: https://developer.android.com/studio/build/shrink-code', indent: 4);
|
||||
BuildEvent('r8-failure').send();
|
||||
} else if (potentialAndroidXFailure) {
|
||||
printStatus('AndroidX incompatibilities may have caused this build to fail. See https://goo.gl/CP92wY.');
|
||||
BuildEvent('android-x-failure').send();
|
||||
|
||||
@@ -92,7 +92,7 @@ class AndroidBuildInfo {
|
||||
AndroidArch.arm64_v8a,
|
||||
],
|
||||
this.splitPerAbi = false,
|
||||
this.proguard = false,
|
||||
this.shrink = false,
|
||||
});
|
||||
|
||||
// The build info containing the mode and flavor.
|
||||
@@ -105,8 +105,8 @@ class AndroidBuildInfo {
|
||||
/// will be produced.
|
||||
final bool splitPerAbi;
|
||||
|
||||
/// Whether to enable Proguard on release mode.
|
||||
final bool proguard;
|
||||
/// Whether to enable code shrinking on release mode.
|
||||
final bool shrink;
|
||||
|
||||
/// The target platforms for the build.
|
||||
final Iterable<AndroidArch> targetArchs;
|
||||
|
||||
@@ -21,6 +21,7 @@ class BuildApkCommand extends BuildSubCommand {
|
||||
usesPubOption();
|
||||
usesBuildNumberOption();
|
||||
usesBuildNameOption();
|
||||
addShrinkingFlag();
|
||||
|
||||
argParser
|
||||
..addFlag('split-per-abi',
|
||||
@@ -28,12 +29,6 @@ class BuildApkCommand extends BuildSubCommand {
|
||||
help: 'Whether to split the APKs per ABIs. '
|
||||
'To learn more, see: https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split',
|
||||
)
|
||||
..addFlag('proguard',
|
||||
negatable: true,
|
||||
defaultsTo: false,
|
||||
help: 'Whether to enable Proguard on release mode. '
|
||||
'To learn more, see: https://flutter.dev/docs/deployment/android#enabling-proguard',
|
||||
)
|
||||
..addMultiOption('target-platform',
|
||||
splitCommas: true,
|
||||
defaultsTo: <String>['android-arm', 'android-arm64'],
|
||||
@@ -83,10 +78,11 @@ class BuildApkCommand extends BuildSubCommand {
|
||||
@override
|
||||
Future<FlutterCommandResult> runCommand() async {
|
||||
final BuildInfo buildInfo = getBuildInfo();
|
||||
final AndroidBuildInfo androidBuildInfo = AndroidBuildInfo(buildInfo,
|
||||
final AndroidBuildInfo androidBuildInfo = AndroidBuildInfo(
|
||||
buildInfo,
|
||||
splitPerAbi: argResults['split-per-abi'],
|
||||
targetArchs: argResults['target-platform'].map<AndroidArch>(getAndroidArchForName),
|
||||
proguard: argResults['proguard'],
|
||||
shrink: argResults['shrink'],
|
||||
);
|
||||
|
||||
if (buildInfo.isRelease && !androidBuildInfo.splitPerAbi && androidBuildInfo.targetArchs.length > 1) {
|
||||
|
||||
@@ -19,15 +19,10 @@ class BuildAppBundleCommand extends BuildSubCommand {
|
||||
usesPubOption();
|
||||
usesBuildNumberOption();
|
||||
usesBuildNameOption();
|
||||
addShrinkingFlag();
|
||||
|
||||
argParser
|
||||
..addFlag('track-widget-creation', negatable: false, hide: !verboseHelp)
|
||||
..addFlag('proguard',
|
||||
negatable: true,
|
||||
defaultsTo: false,
|
||||
help: 'Whether to enable Proguard on release mode. '
|
||||
'To learn more, see: https://flutter.dev/docs/deployment/android#enabling-proguard',
|
||||
)
|
||||
..addMultiOption('target-platform',
|
||||
splitCommas: true,
|
||||
defaultsTo: <String>['android-arm', 'android-arm64'],
|
||||
@@ -70,7 +65,7 @@ class BuildAppBundleCommand extends BuildSubCommand {
|
||||
Future<FlutterCommandResult> runCommand() async {
|
||||
final AndroidBuildInfo androidBuildInfo = AndroidBuildInfo(getBuildInfo(),
|
||||
targetArchs: argResults['target-platform'].map<AndroidArch>(getAndroidArchForName),
|
||||
proguard: argResults['proguard'],
|
||||
shrink: argResults['shrink'],
|
||||
);
|
||||
await androidBuilder.buildAab(
|
||||
project: FlutterProject.current(),
|
||||
|
||||
@@ -277,6 +277,19 @@ abstract class FlutterCommand extends Command<void> {
|
||||
help: 'Build a release version of your app${defaultToRelease ? ' (default mode)' : ''}.');
|
||||
}
|
||||
|
||||
void addShrinkingFlag() {
|
||||
argParser.addFlag('shrink',
|
||||
negatable: true,
|
||||
defaultsTo: true,
|
||||
help: 'Whether to enable code shrinking on release mode.'
|
||||
'When enabling shrinking, you also benefit from obfuscation, '
|
||||
'which shortens the names of your app’s classes and members, '
|
||||
'and optimization, which applies more aggressive strategies to '
|
||||
'further reduce the size of your app.'
|
||||
'To learn more, see: https://developer.android.com/studio/build/shrink-code'
|
||||
);
|
||||
}
|
||||
|
||||
void usesFuchsiaOptions({ bool hide = false }) {
|
||||
argParser.addOption(
|
||||
'target-model',
|
||||
|
||||
Reference in New Issue
Block a user