Skip to content

Commit d87e715

Browse files
A Googlercopybara-github
authored andcommitted
Internal change
PiperOrigin-RevId: 764446575 Change-Id: I4bd602f91a3efb969604af7583a3465eca1df09f
1 parent 6d4a7aa commit d87e715

5 files changed

Lines changed: 53 additions & 1 deletion

File tree

providers/providers.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ AndroidNativeLibsInfo = provider(
218218
),
219219
)
220220

221+
AndroidOptimizationInfo = provider(
222+
doc = "Contains data about which optimizations were applied to the APK with which settings",
223+
fields = dict(
224+
d8_optimization_info = "Metadata produced by D8 about the applied optimizations in a JSON format.",
225+
),
226+
)
227+
221228
AndroidIdlInfo = provider(
222229
doc = "AndroidIdlInfo",
223230
fields = dict(

rules/acls.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ load("//rules/acls:android_test_lockdown.bzl", "ANDROID_TEST_LOCKDOWN_GENERATOR_
5555
load("//rules/acls:b122039567.bzl", "B122039567")
5656
load("//rules/acls:baseline_profiles_optimizer_integration.bzl", "BASELINE_PROFILES_OPTIMIZER_INTEGRATION", "BASELINE_PROFILES_OPTIMIZER_INTEGRATION_FALLBACK")
5757
load("//rules/acls:baseline_profiles_rollout.bzl", "BASELINE_PROFILES_ROLLOUT")
58+
load("//rules/acls:d8_optimization_metadata.bzl", "D8_OPTIMIZATION_METADATA")
5859
load("//rules/acls:databinding.bzl", "DATABINDING_ALLOWED", "DATABINDING_DISALLOWED")
5960
load("//rules/acls:desugaring_runtime_jar_classpath.bzl", "DESUGAR_USE_RUNTIME_JARS")
6061
load("//rules/acls:dex2oat_opts.bzl", "CAN_USE_DEX2OAT_OPTIONS")
@@ -215,6 +216,9 @@ def _in_desugaring_runtime_jar_classpath_rollout():
215216
def _in_resource_translation_merging_rollout(fqn):
216217
return matches(fqn, RESOURCE_TRANSLATION_MERGING_ROLLOUT_DICT) and not matches(fqn, RESOURCE_TRANSLATION_MERGING_FALLBACK_DICT)
217218

219+
def _in_d8_optimization_metadata(fqn):
220+
return matches(fqn, D8_OPTIMIZATION_METADATA_DICT)
221+
218222
def make_dict(lst):
219223
"""Do not use this method outside of acls directory."""
220224
return {t: True for t in lst}
@@ -275,6 +279,7 @@ ANDROID_BINARY_RAW_ACCESS_TO_RESOURCE_PATHS_ALLOWLIST_DICT = make_dict(ANDROID_B
275279
ANDROID_BINARY_RESOURCE_NAME_OBFUSCATION_OPT_OUT_ALLOWLIST_DICT = make_dict(ANDROID_BINARY_RESOURCE_NAME_OBFUSCATION_OPT_OUT_ALLOWLIST)
276280
ALLOW_PROGUARD_APPLY_MAPPING_DICT = make_dict(ALLOW_PROGUARD_APPLY_MAPPING)
277281
USE_R8_DICT = make_dict(USE_R8)
282+
D8_OPTIMIZATION_METADATA_DICT = make_dict(D8_OPTIMIZATION_METADATA)
278283
RESOURCE_SHRINKING_IN_OPTIMIZER_ROLLOUT_DICT = make_dict(RESOURCE_SHRINKING_IN_OPTIMIZER_ROLLOUT)
279284
RESOURCE_SHRINKING_IN_OPTIMIZER_FALLBACK_DICT = make_dict(RESOURCE_SHRINKING_IN_OPTIMIZER_FALLBACK)
280285
DISABLE_OPTIMIZING_DEXER_DICT = make_dict(DISABLE_OPTIMIZING_DEXER)
@@ -373,6 +378,7 @@ acls = struct(
373378
in_android_binary_resource_name_obfuscation_opt_out_allowlist = _in_android_binary_resource_name_obfuscation_opt_out_allowlist,
374379
in_allow_proguard_apply_mapping = _in_allow_proguard_apply_mapping,
375380
use_r8 = _use_r8,
381+
in_d8_optimization_metadata = _in_d8_optimization_metadata,
376382
in_disable_optimizing_dexer = _in_disable_optimizing_dexer,
377383
in_force_final_android_binary_resources = _in_force_final_android_binary_resources,
378384
in_resource_shrinking_in_optimizer = _in_resource_shrinking_in_optimizer,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
"""Allow list for emitting d8 optimization metadata."""
15+
16+
load("//rules:visibility.bzl", "PROJECT_VISIBILITY")
17+
18+
visibility(PROJECT_VISIBILITY)
19+
20+
# keep sorted
21+
D8_OPTIMIZATION_METADATA = [
22+
"//tools/build_defs/android/test/dev:__subpackages__",
23+
]

rules/android_binary/impl.bzl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
"""Implementation."""
1515

16-
load("//providers:providers.bzl", "AndroidDexInfo", "AndroidFeatureFlagSet", "AndroidIdlInfo", "AndroidInstrumentationInfo", "AndroidLibraryResourceClassJarProvider", "AndroidPreDexJarInfo", "ApkInfo", "BaselineProfileProvider", "DataBindingV2Info", "ProguardMappingInfo", "StarlarkAndroidDexInfo", "StarlarkAndroidResourcesInfo", "StarlarkApkInfo")
16+
load("//providers:providers.bzl", "AndroidDexInfo", "AndroidFeatureFlagSet", "AndroidIdlInfo", "AndroidInstrumentationInfo", "AndroidLibraryResourceClassJarProvider", "AndroidOptimizationInfo", "AndroidPreDexJarInfo", "ApkInfo", "BaselineProfileProvider", "DataBindingV2Info", "ProguardMappingInfo", "StarlarkAndroidDexInfo", "StarlarkAndroidResourcesInfo", "StarlarkApkInfo")
1717
load("//rules:acls.bzl", "acls")
1818
load("//rules:apk_packaging.bzl", _apk_packaging = "apk_packaging")
1919
load("//rules:baseline_profiles.bzl", _baseline_profiles = "baseline_profiles")
@@ -315,6 +315,11 @@ def _process_dex(ctx, validation_ctx, packaged_resources_ctx, deploy_ctx, bp_ctx
315315
)
316316

317317
should_optimize_dex = optimizing_dexer and proguarded_jar and not acls.in_disable_optimizing_dexer(str(ctx.label))
318+
319+
build_metadata_output = None
320+
if should_optimize_dex and acls.in_d8_optimization_metadata(str(ctx.label)):
321+
build_metadata_output = ctx.actions.declare_file(ctx.label.name + "_d8_optimization_info.json")
322+
318323
if proguard_output_map:
319324
# Proguard map from preprocessing will be merged with Proguard map for desugared
320325
# library.
@@ -362,6 +367,7 @@ def _process_dex(ctx, validation_ctx, packaged_resources_ctx, deploy_ctx, bp_ctx
362367
proguard_output_map = proguard_output_map,
363368
postprocessing_output_map = postprocessing_output_map,
364369
startup_profile = optimize_ctx.proguard_output.startup_profile_rewritten,
370+
build_metadata_output = build_metadata_output,
365371
inclusion_filter_jar = binary_jar if is_instrumentation(ctx) and not is_binary_optimized else None,
366372
transitive_runtime_jars_for_archive = deploy_ctx.transitive_runtime_jars_for_archive,
367373
desugar_dict = deploy_ctx.desugar_dict,
@@ -430,6 +436,9 @@ def _process_dex(ctx, validation_ctx, packaged_resources_ctx, deploy_ctx, bp_ctx
430436
)
431437
providers.append(AndroidPreDexJarInfo(pre_dex_jar = binary_jar))
432438

439+
if build_metadata_output != None:
440+
providers.append(AndroidOptimizationInfo(d8_optimization_info = build_metadata_output))
441+
433442
if postprocessing_output_map:
434443
providers.append(ProguardMappingInfo(proguard_mapping = postprocessing_output_map))
435444

rules/dex.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def _process_incremental_dexing(
5151
proguard_output_map = None,
5252
postprocessing_output_map = None,
5353
startup_profile = None,
54+
build_metadata_output = None,
5455
inclusion_filter_jar = None,
5556
desugar_dict = {},
5657
transitive_runtime_jars_for_archive = [],
@@ -171,6 +172,7 @@ def _process_incremental_dexing(
171172
proguard_output_map = proguard_output_map,
172173
postprocessing_output_map = postprocessing_output_map,
173174
startup_profile = startup_profile,
175+
build_metadata_output = build_metadata_output,
174176
optimizing_dexer = optimizing_dexer,
175177
min_sdk_config = min_sdk_config,
176178
toolchain_type = toolchain_type,
@@ -615,6 +617,7 @@ def _optimized_dex_merge(
615617
proguard_output_map = None,
616618
postprocessing_output_map = None,
617619
startup_profile = None,
620+
build_metadata_output = None,
618621
optimizing_dexer = None,
619622
min_sdk_config = None,
620623
toolchain_type = None):
@@ -641,6 +644,10 @@ def _optimized_dex_merge(
641644
inputs.append(proguard_output_map)
642645
param_file_args.add("--pg-map", proguard_output_map)
643646

647+
if build_metadata_output:
648+
outputs.append(build_metadata_output)
649+
args.add("--build_metadata_output", build_metadata_output)
650+
644651
if postprocessing_output_map:
645652
param_file_args.add("--pg-map-output", postprocessing_output_map)
646653
outputs.append(postprocessing_output_map)

0 commit comments

Comments
 (0)