Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rules/android_binary/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def _process_resources(ctx, manifest_ctx, java_package, **unused_ctxs):
shrink_resources = ctx.attr.shrink_resources,
use_android_resource_shrinking = ctx.fragments.android.use_android_resource_shrinking,
use_android_resource_cycle_shrinking = ctx.fragments.android.use_android_resource_cycle_shrinking,
use_minimal_keep_rules = _flags.get(ctx).use_minimal_keep_rules,
use_legacy_manifest_merger = use_legacy_manifest_merger(ctx),
should_throw_on_conflict = not acls.in_allow_resource_conflicts(str(ctx.label)),
enable_data_binding = ctx.attr.enable_data_binding,
Expand Down
6 changes: 6 additions & 0 deletions rules/busybox.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ def _package(
nocompress_extensions = [],
proto_format = False,
shrink_resource_cycles = False,
use_minimal_keep_rules = False,
version_name = None,
version_code = None,
feature_flags = "",
Expand Down Expand Up @@ -293,6 +294,9 @@ def _package(
proto_format: Boolean, whether to generate the resource table in proto format.
shrink_resource_cycles: Boolean, flag that enables more shrinking of
code and resources by instructing AAPT2 to emit conditional Proguard keep rules.
use_minimal_keep_rules: Boolean, flag that instructs AAPT2 to emit minimal Proguard
keep rules with precise member signatures instead of broad <init>(...) rules,
matching Android Gradle Plugin behavior.
version_name: A string. The version name to stamp the generated manifest with. Optional.
version_code: A string. The version code to stamp the generated manifest with. Optional.
crunch_png: A boolean. Determines whether `aapt2 compile` should crunch PNG files.
Expand Down Expand Up @@ -414,6 +418,8 @@ def _package(
args.add("--resourceTableAsProto")
if shrink_resource_cycles:
args.add("--conditionalKeepRules=yes")
if use_minimal_keep_rules:
args.add("--minimalKeepRules=yes")
if version_name:
args.add("--versionName", version_name)
if version_code:
Expand Down
9 changes: 9 additions & 0 deletions rules/flags/flag_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,12 @@ def define_flags():
description = "When enabled, aar_import extracts R8-targeted proguard rules from " +
"META-INF/com.android.tools/ inside classes.jar in addition to proguard.txt.",
)

flags.DEFINE_bool(
name = "use_minimal_keep_rules",
default = False,
description = "When enabled, aapt2 generates minimal proguard keep rules " +
"(--proguard-minimal-keep-rules) for resources: precise member " +
"signatures (e.g. constructor argument types) instead of broad " +
"<init>(...) rules, matching Android Gradle Plugin behavior.",
)
5 changes: 5 additions & 0 deletions rules/resources.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ def _package(
shrink_resources = None,
use_android_resource_shrinking = None,
use_android_resource_cycle_shrinking = None,
use_minimal_keep_rules = False,
use_legacy_manifest_merger = False,
should_throw_on_conflict = True,
enable_data_binding = False,
Expand Down Expand Up @@ -547,6 +548,9 @@ def _package(
shrink_resources if the tristate value is auto (-1).
use_android_resource_cycle_shrinking: Bool. Flag that enables more shrinking of
code and resources by instructing AAPT2 to emit conditional Proguard keep rules.
use_minimal_keep_rules: Bool. Flag that instructs AAPT2 to emit minimal Proguard
keep rules with precise member signatures instead of broad <init>(...) rules,
matching Android Gradle Plugin behavior.
use_legacy_manifest_merger: A boolean. Whether to use the legacy manifest merger
instead of the android manifest merger.
should_throw_on_conflict: A boolean. Determines whether an error should be thrown
Expand Down Expand Up @@ -768,6 +772,7 @@ def _package(
nocompress_extensions = nocompress_extensions,
java_package = java_package,
shrink_resource_cycles = shrink_resource_cycles,
use_minimal_keep_rules = use_minimal_keep_rules,
version_name = manifest_values[_VERSION_NAME] if _VERSION_NAME in manifest_values else None,
version_code = manifest_values[_VERSION_CODE] if _VERSION_CODE in manifest_values else None,
feature_flags = feature_flags,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ public static void main(String[] args) throws Exception {
.withAssets(assetDirs)
.buildVersion(aaptConfigOptions.buildToolsVersion)
.conditionalKeepRules(aaptConfigOptions.conditionalKeepRules == TriState.YES)
.minimalKeepRules(aaptConfigOptions.minimalKeepRules == TriState.YES)
.filterToDensity(options.densities)
.storeUncompressed(aaptConfigOptions.uncompressedExtensions)
.debug(aaptConfigOptions.debug)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public class Aapt2ConfigOptions {
description = "Have AAPT2 produce conditional keep rules.")
public TriState conditionalKeepRules = TriState.AUTO;

@Parameter(
names = "--minimalKeepRules",
description =
"Have AAPT2 produce minimal proguard keep rules, emitting precise member "
+ "signatures (e.g. constructor argument types) instead of broad <init>(...) "
+ "rules. Matches Android Gradle Plugin behavior.")
public TriState minimalKeepRules = TriState.AUTO;

@Parameter(
names = "--uncompressedExtensions",
description = "A list of file extensions not to compress.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public static LinkError of(Throwable e) {
private List<CompiledResources> include = ImmutableList.of();
private List<Path> assetDirs = ImmutableList.of();
private boolean conditionalKeepRules = false;
private boolean minimalKeepRules = false;
private boolean includeProguardLocationReferences = false;
private List<StaticLibrary> resourceApks = ImmutableList.of();
private String featureFlags = "";
Expand Down Expand Up @@ -233,6 +234,12 @@ public ResourceLinker conditionalKeepRules(boolean conditionalKeepRules) {
return this;
}

@CanIgnoreReturnValue
public ResourceLinker minimalKeepRules(boolean minimalKeepRules) {
this.minimalKeepRules = minimalKeepRules;
return this;
}

@CanIgnoreReturnValue
public ResourceLinker customPackage(String customPackage) {
this.customPackage = customPackage;
Expand Down Expand Up @@ -529,6 +536,8 @@ private ProtoApk linkProtoApk(
.add("--no-proguard-location-reference")
.when(conditionalKeepRules)
.thenAdd("--proguard-conditional-keep-rules")
.when(minimalKeepRules)
.thenAdd("--proguard-minimal-keep-rules")
.add("-o", linked)
.add("--feature-flags", featureFlags)
.execute(String.format("Linking %s", compiled.getManifest())));
Expand Down