From 59d2ece50d7998e355afff8fd7cabd4215fc070d Mon Sep 17 00:00:00 2001 From: Googler Date: Tue, 21 Nov 2017 10:48:16 -0800 Subject: Add option to enable resource cycle shrinking. This will instruct AAPT2 to produce conditional keep rules to allow for more aggressive code and resource shrinking. RELNOTES[NEW]: Add --experimental_android_resource_cycle_shrinking option to allow for more aggressive code and resource shrinking. PiperOrigin-RevId: 176530749 --- .../devtools/build/lib/rules/android/AndroidBinary.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java') diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java index 1bfcac40af..4d389e94d8 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java @@ -217,6 +217,8 @@ public abstract class AndroidBinary implements RuleConfiguredTargetFactory { configurationMap, cppSemantics); + boolean shrinkResources = shouldShrinkResources(ruleContext); + // TODO(bazel-team): Resolve all the different cases of resource handling so this conditional // can go away: recompile from android_resources, and recompile from android_binary attributes. ApplicationManifest applicationManifest; @@ -250,6 +252,7 @@ public abstract class AndroidBinary implements RuleConfiguredTargetFactory { ruleContext.attributes().get("crunch_png", Type.BOOLEAN), ProguardHelper.getProguardConfigArtifact(ruleContext, ""), createMainDexProguardSpec(ruleContext), + shouldShrinkResourceCycles(ruleContext, shrinkResources), ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_PROCESSED_MANIFEST), ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_RESOURCES_ZIP), DataBinding.isEnabled(ruleContext) @@ -294,8 +297,6 @@ public abstract class AndroidBinary implements RuleConfiguredTargetFactory { } - boolean shrinkResources = shouldShrinkResources(ruleContext); - // Remove the library resource JARs from the binary's runtime classpath. // Resource classes from android_library dependencies are replaced by the binary's resource // class. We remove them only at the top level so that resources included by a library that is @@ -785,6 +786,18 @@ public abstract class AndroidBinary implements RuleConfiguredTargetFactory { return (state == TriState.YES); } + /** Returns {@code true} if resource shrinking should be performed. */ + private static boolean shouldShrinkResourceCycles( + RuleContext ruleContext, boolean shrinkResources) throws RuleErrorException { + boolean global = + ruleContext.getFragment(AndroidConfiguration.class).useAndroidResourceCycleShrinking(); + if (global && !shrinkResources) { + throw ruleContext.throwWithRuleError( + "resource cycle shrinking can only be enabled when resource shrinking is enabled"); + } + return global; + } + private static ResourceApk shrinkResources( RuleContext ruleContext, ResourceApk resourceApk, -- cgit v1.2.3