Skip to content

Commit

Permalink
Wire up proguard actions + initial tests.
Browse files Browse the repository at this point in the history
There is one outstanding issue with generate_mapping that needs resolving before tests can be added for this.

This change also removes some unused fields / outputs from these actions.

PiperOrigin-RevId: 557258029
Change-Id: I95c11a101569d7a5254041290ddd489588a943b3
  • Loading branch information
timpeut authored and copybara-github committed Aug 15, 2023
1 parent b0b5616 commit 5615075
Show file tree
Hide file tree
Showing 5 changed files with 403 additions and 19 deletions.
1 change: 1 addition & 0 deletions rules/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bzl_library(
srcs = [
"aapt.bzl",
"acls.bzl",
"android_neverlink_aspect.bzl",
"attrs.bzl",
"bundletool.bzl",
"busybox.bzl",
Expand Down
23 changes: 23 additions & 0 deletions rules/android_binary_internal/attrs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ ATTRS = _attrs.replace(
incremental_dexing = _attrs.tristate.create(
default = _attrs.tristate.auto,
),
proguard_generate_mapping = attr.bool(default = False),
proguard_optimization_passes = attr.int(),
proguard_apply_mapping = attr.label(allow_single_file = True),
_java_toolchain = attr.label(
default = Label("//tools/jdk:toolchain_android_only"),
),
Expand All @@ -113,6 +116,26 @@ ATTRS = _attrs.replace(
default = "@bazel_tools//tools/cpp:current_cc_toolchain",
aspects = [split_config_aspect],
),
_optimizing_dexer = attr.label(
cfg = "exec",
allow_single_file = True,
default = configuration_field(
fragment = "android",
name = "optimizing_dexer",
),
),
_desugared_java8_legacy_apis = attr.label(
default = Label("//tools/android:desugared_java8_legacy_apis"),
allow_single_file = True,
),
_bytecode_optimizer = attr.label(
default = configuration_field(
fragment = "java",
name = "bytecode_optimizer",
),
cfg = "exec",
executable = True,
),
),
_attrs.COMPILATION,
_attrs.DATA_CONTEXT,
Expand Down
89 changes: 89 additions & 0 deletions rules/android_binary_internal/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ load("//rules:baseline_profiles.bzl", _baseline_profiles = "baseline_profiles")
load("//rules:common.bzl", "common")
load("//rules:data_binding.bzl", "data_binding")
load("//rules:java.bzl", "java")
load("//rules:proguard.bzl", "proguard", proguard_testing = "testing")
load(
"//rules:processing_pipeline.bzl",
"ProviderInfo",
Expand Down Expand Up @@ -433,6 +434,93 @@ def _process_baseline_profiles(ctx, dex_ctx, **_unused_ctxs):
value = struct(providers = providers),
)

def _process_optimize(ctx, deploy_ctx, packaged_resources_ctx, **_unused_ctxs):
if not acls.in_android_binary_starlark_dex_desugar_proguard(str(ctx.label)):
return ProviderInfo(
name = "optimize_ctx",
value = struct(),
)

# Validate attributes and lockdown lists
if ctx.file.proguard_apply_mapping and not acls.in_allow_proguard_apply_mapping(ctx.label):
fail("proguard_apply_mapping is not supported")
if ctx.file.proguard_apply_mapping and not ctx.files.proguard_specs:
fail("proguard_apply_mapping can only be used when proguard_specs is set")

proguard_specs = proguard.get_proguard_specs(
ctx,
packaged_resources_ctx.resource_proguard_config,
proguard_specs_for_manifest = [packaged_resources_ctx.resource_minsdk_proguard_config] if packaged_resources_ctx.resource_minsdk_proguard_config else [],
)
has_proguard_specs = bool(proguard_specs)
proguard_output = struct()

proguard_output_map = None
generate_proguard_map = (
ctx.attr.proguard_generate_mapping or
_resources.is_resource_shrinking_enabled(
ctx.attr.shrink_resources,
ctx.fragments.android.use_android_resource_shrinking,
)
)
desugar_java8_libs_generates_map = ctx.fragments.android.desugar_java8
optimizing_dexing = bool(ctx.attr._optimizing_dexer)

# TODO(b/261110876): potentially add codepaths below to support rex (postprocessingRewritesMap)
if generate_proguard_map:
# Determine the output of the Proguard map from shrinking the app. This depends on the
# additional steps which can process the map before the final Proguard map artifact is
# generated.
if not has_proguard_specs:
# When no shrinking happens a generating rule for the output map artifact is still needed.
proguard_output_map = proguard.get_proguard_output_map(ctx)
elif optimizing_dexing:
proguard_output_map = proguard.get_proguard_temp_artifact(ctx, "pre_dexing.map")
elif desugar_java8_libs_generates_map:
# Proguard map from shrinking will be merged with desugared library proguard map.
proguard_output_map = _dex.get_dx_artifact(ctx, "_proguard_output_for_desugared_library.map")
else:
# Proguard map from shrinking is the final output.
proguard_output_map = proguard.get_proguard_output_map(ctx)

proguard_output_jar = ctx.actions.declare_file(ctx.label.name + "_migrated_proguard.jar")
proguard_seeds = ctx.actions.declare_file(ctx.label.name + "_migrated_proguard.seeds")
proguard_usage = ctx.actions.declare_file(ctx.label.name + "_migrated_proguard.usage")

proguard_output = proguard.apply_proguard(
ctx,
input_jar = deploy_ctx.deploy_jar,
proguard_specs = proguard_specs,
proguard_optimization_passes = getattr(ctx.attr, "proguard_optimization_passes", None),
proguard_output_jar = proguard_output_jar,
proguard_mapping = ctx.file.proguard_apply_mapping,
proguard_output_map = proguard_output_map,
proguard_seeds = proguard_seeds,
proguard_usage = proguard_usage,
proguard_tool = get_android_sdk(ctx).proguard,
)

providers = []
if proguard_output:
providers.append(proguard_testing.ProguardOutputInfo(
input_jar = deploy_ctx.deploy_jar,
output_jar = proguard_output.output_jar,
mapping = proguard_output.mapping,
proto_mapping = proguard_output.proto_mapping,
seeds = proguard_output.seeds,
usage = proguard_output.usage,
library_jar = proguard_output.library_jar,
config = proguard_output.config,
))

return ProviderInfo(
name = "optimize_ctx",
value = struct(
proguard_output = proguard_output,
providers = providers,
),
)

# Order dependent, as providers will not be available to downstream processors
# that may depend on the provider. Iteration order for a dictionary is based on
# insertion.
Expand All @@ -448,6 +536,7 @@ PROCESSORS = dict(
BuildInfoProcessor = _process_build_info,
ProtoProcessor = _process_proto,
DeployJarProcessor = _process_deploy_jar,
OptimizeProcessor = _process_optimize,
DexProcessor = _process_dex,
BaselineProfilesProcessor = _process_baseline_profiles,
)
Expand Down
Loading

0 comments on commit 5615075

Please sign in to comment.