Skip to content

Commit 4157bc6

Browse files
timpeutcopybara-github
authored andcommitted
Pipe --feature-flags through to some aapt2 actions.
We may need to wire this through elsewhere in the future. PiperOrigin-RevId: 820391042 Change-Id: I5224553f5172e6d6e3bb4214e477ba1f4533b773
1 parent 9564634 commit 4157bc6

11 files changed

Lines changed: 58 additions & 0 deletions

File tree

rules/acls.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ To update a list:
2727
"""
2828

2929
load("//rules:visibility.bzl", "PROJECT_VISIBILITY")
30+
load("//rules/acls:aapt2_feature_flags.bzl", "AAPT2_FEATURE_FLAGS")
3031
load("//rules/acls:aar_import_deps_checker.bzl", "AAR_IMPORT_DEPS_CHECKER_FALLBACK", "AAR_IMPORT_DEPS_CHECKER_ROLLOUT")
3132
load("//rules/acls:aar_import_explicit_exports_manifest.bzl", "AAR_IMPORT_EXPLICIT_EXPORTS_MANIFEST")
3233
load("//rules/acls:aar_import_exports_r_java.bzl", "AAR_IMPORT_EXPORTS_R_JAVA")
@@ -223,6 +224,9 @@ def _in_d8_optimization_metadata(fqn):
223224
def _in_enable_exported_lint_checks(fqn):
224225
return matches(fqn, ENABLE_EXPORTED_LINT_CHECKS_DICT)
225226

227+
def _get_aapt2_feature_flags(_):
228+
return AAPT2_FEATURE_FLAGS
229+
226230
def make_dict(lst):
227231
"""Do not use this method outside of acls directory."""
228232
return {t: True for t in lst}
@@ -391,6 +395,7 @@ acls = struct(
391395
in_desugaring_runtime_jar_classpath_rollout = _in_desugaring_runtime_jar_classpath_rollout,
392396
in_resource_translation_merging_rollout = _in_resource_translation_merging_rollout,
393397
in_enable_exported_lint_checks = _in_enable_exported_lint_checks,
398+
get_aapt2_feature_flags = _get_aapt2_feature_flags,
394399
)
395400

396401
# Visible for testing

rules/acls/aapt2_feature_flags.bzl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2025 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Feature flags to pass to the aapt2 link command."""
15+
16+
AAPT2_FEATURE_FLAGS = ""

rules/android_binary/impl.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def _process_resources(ctx, manifest_ctx, java_package, **unused_ctxs):
124124
busybox = get_android_toolchain(ctx).android_resources_busybox.files_to_run,
125125
host_javabase = ctx.attr._host_javabase,
126126
use_r_package = ctx.attr.use_r_package,
127+
feature_flags = acls.get_aapt2_feature_flags(str(ctx.label)),
127128
)
128129
return ProviderInfo(
129130
name = "packaged_resources_ctx",

rules/android_library/impl.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ def _process_resources(ctx, java_package, manifest_ctx, localized_ctx, **unused_
175175
deps = ctx.attr.deps,
176176
resource_apks = resource_apks,
177177
exports = ctx.attr.exports,
178+
feature_flags = acls.get_aapt2_feature_flags(str(ctx.label)),
178179

179180
# Processing behavior changing flags.
180181
enable_res_v3 = _flags.get(ctx).android_enable_res_v3,

rules/busybox.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ def _package(
226226
shrink_resource_cycles = False,
227227
version_name = None,
228228
version_code = None,
229+
feature_flags = "",
229230
android_jar = None,
230231
aapt = None,
231232
busybox = None,
@@ -406,6 +407,8 @@ def _package(
406407
args.add("--versionCode", version_code)
407408
if java_package:
408409
args.add("--packageForR", java_package)
410+
if feature_flags:
411+
args.add("--featureFlags", feature_flags)
409412

410413
args.add_joined(
411414
"--resourceApks",
@@ -569,6 +572,7 @@ def _validate_and_link(
569572
transitive_compiled_resources = depset(),
570573
java_package = None,
571574
manifest = None,
575+
feature_flags = "",
572576
resource_apks = [],
573577
android_jar = None,
574578
busybox = None,
@@ -630,6 +634,8 @@ def _validate_and_link(
630634
join_with = ":",
631635
)
632636
input_files.extend(resource_apks)
637+
if feature_flags:
638+
args.add("--featureFlags", feature_flags)
633639

634640
_set_warning_level(ctx, args)
635641

rules/resources.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ def _package(
495495
should_compile_java_srcs = True,
496496
generate_minsdk_proguard_config = False,
497497
build_java_with_final_resources = False,
498+
feature_flags = "",
498499
aapt = None,
499500
has_local_proguard_specs = False,
500501
android_jar = None,
@@ -764,6 +765,7 @@ def _package(
764765
shrink_resource_cycles = shrink_resource_cycles,
765766
version_name = manifest_values[_VERSION_NAME] if _VERSION_NAME in manifest_values else None,
766767
version_code = manifest_values[_VERSION_CODE] if _VERSION_CODE in manifest_values else None,
768+
feature_flags = feature_flags,
767769
android_jar = android_jar,
768770
aapt = aapt,
769771
busybox = busybox,
@@ -1188,6 +1190,7 @@ def _process_starlark(
11881190
deps = [],
11891191
resource_apks = [],
11901192
exports = [],
1193+
feature_flags = "",
11911194
resource_files = None,
11921195
neverlink = False,
11931196
enable_data_binding = False,
@@ -1228,6 +1231,7 @@ def _process_starlark(
12281231
attributes are considered direct dependencies of any rule that directly
12291232
depends on the target with exports. The exports are not direct deps of
12301233
the rule they belong to (TODO(b/144134042): make this so).
1234+
feature_flags: string. The string value for --feature-flags to pass to aapt2.
12311235
resource_files: sequence of Files. A list of Android resource files to be
12321236
processed.
12331237
neverlink: boolean. Only use this library for compilation and not runtime.
@@ -1406,6 +1410,7 @@ def _process_starlark(
14061410
transitive_compiled_resources = transitive_compiled_resources,
14071411
transitive_manifests = transitive_manifests,
14081412
transitive_r_txts = transitive_r_txts,
1413+
feature_flags = feature_flags,
14091414
package_type = "LIBRARY",
14101415
java_package = java_package,
14111416
android_jar = android_jar,
@@ -1582,6 +1587,7 @@ def _process_starlark(
15821587
),
15831588
java_package = java_package,
15841589
manifest = processed_manifest,
1590+
feature_flags = feature_flags,
15851591
android_jar = android_jar,
15861592
aapt = aapt,
15871593
busybox = busybox,
@@ -1795,6 +1801,7 @@ def _process(
17951801
deps = [],
17961802
resource_apks = [],
17971803
exports = [],
1804+
feature_flags = "",
17981805
android_jar = None,
17991806
android_kit = None,
18001807
aapt = None,
@@ -1821,6 +1828,7 @@ def _process(
18211828
deps = deps,
18221829
resource_apks = resource_apks,
18231830
exports = exports,
1831+
feature_flags = feature_flags,
18241832
resource_files = resource_files,
18251833
enable_data_binding = enable_data_binding,
18261834
fix_resource_transitivity = fix_resource_transitivity,

src/tools/java/com/google/devtools/build/android/Aapt2ResourcePackagingAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ public static void main(String[] args) throws Exception {
405405
.includeGeneratedLocales(aaptConfigOptions.generatePseudoLocale)
406406
.includeOnlyConfigs(aaptConfigOptions.resourceConfigs)
407407
.includeProguardLocationReferences(options.includeProguardLocationReferences)
408+
.featureFlags(aaptConfigOptions.featureFlags)
408409
.link(compiled);
409410
profiler.recordEndOf("link").startTask("validate");
410411

src/tools/java/com/google/devtools/build/android/ValidateAndLinkResourcesAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ public static void main(String[] args) throws Exception {
178178
.resourceApks(resourceApks)
179179
.buildVersion(aapt2Options.buildToolsVersion)
180180
.outputAsProto(aapt2Options.resourceTableAsProto)
181+
.featureFlags(aapt2Options.featureFlags)
181182
.linkStatically(resources)
182183
.copyLibraryTo(options.staticLibraryOut)
183184
.copySourceJarTo(options.sourceJarOut)

src/tools/java/com/google/devtools/build/android/aapt2/Aapt2ConfigOptions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,9 @@ public class Aapt2ConfigOptions {
117117
arity = 1,
118118
description = "Indicates whether databinding generated files should depend on AndroidX.")
119119
public boolean useDataBindingAndroidX;
120+
121+
@Parameter(
122+
names = "--featureFlags",
123+
description = "Optional --feature-flags to pass to certain aapt2 commands.")
124+
public String featureFlags = "";
120125
}

src/tools/java/com/google/devtools/build/android/aapt2/ResourceLinker.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ public static LinkError of(Throwable e) {
157157
private boolean conditionalKeepRules = false;
158158
private boolean includeProguardLocationReferences = false;
159159
private List<StaticLibrary> resourceApks = ImmutableList.of();
160+
private String featureFlags = "";
160161

161162
private ResourceLinker(
162163
Path aapt2, ListeningExecutorService executorService, Path workingDirectory) {
@@ -251,6 +252,12 @@ public ResourceLinker resourceApks(List<StaticLibrary> resourceApks) {
251252
return this;
252253
}
253254

255+
@CanIgnoreReturnValue
256+
public ResourceLinker featureFlags(String featureFlags) {
257+
this.featureFlags = featureFlags;
258+
return this;
259+
}
260+
254261
/**
255262
* Statically links the {@link CompiledResources} with the dependencies to produce a {@link
256263
* StaticLibrary}.
@@ -278,6 +285,7 @@ public StaticLibrary linkStatically(CompiledResources compiled) {
278285
.add("--manifest", compiled.getManifest())
279286
.add("--no-static-lib-packages")
280287
.add("--custom-package", customPackage)
288+
.add("--feature-flags", featureFlags)
281289
.whenVersionIsAtLeast(new Revision(23))
282290
.thenAdd("--no-version-vectors")
283291
.addParameterableRepeated(
@@ -308,6 +316,7 @@ public StaticLibrary linkStatically(CompiledResources compiled) {
308316
.thenAdd("--no-version-vectors")
309317
.when(outputAsProto)
310318
.thenAdd("--proto-format")
319+
.add("--feature-flags", featureFlags)
311320
// only link against jars
312321
.addRepeated("-I", pathsToLinkAgainst.stream().filter(IS_JAR).collect(toList()))
313322
.addRepeated("-I", resourceApkPathsToLinkAgainst)
@@ -480,6 +489,7 @@ private ProtoApk linkProtoApk(
480489
.when(conditionalKeepRules)
481490
.thenAdd("--proguard-conditional-keep-rules")
482491
.add("-o", linked)
492+
.add("--feature-flags", featureFlags)
483493
.execute(String.format("Linking %s", compiled.getManifest())));
484494
profiler.recordEndOf("fulllink");
485495
return ProtoApk.readFrom(optimize(compiled, linked));

0 commit comments

Comments
 (0)