Skip to content

Commit

Permalink
rdy
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminRomano committed Mar 13, 2024
1 parent 60c98ea commit 624ab07
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
29 changes: 26 additions & 3 deletions rules/android_binary_internal/r8.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def process_resource_shrinking_r8(ctx, r8_ctx, packaged_resources_ctx, **_unused
)

resource_shrinking_usage_log = ctx.actions.declare_file(ctx.label.name + "_resource_shrinking_usage.log")
resource_shrinking_res_config = ctx.actions.declare_file(ctx.label.name + "_resources.cfg")
args = ctx.actions.args()
args.add("--input", proto_resource_apk)
args.add("--dex_input", final_classes_dex_zip)
Expand All @@ -180,14 +181,15 @@ def process_resource_shrinking_r8(ctx, r8_ctx, packaged_resources_ctx, **_unused
args.add("--output", proto_resource_apk_shrunk)
args.add("--precise_shrinking", "true")
args.add("--print_usage_log", resource_shrinking_usage_log)
args.add("--print_config", resource_shrinking_res_config)

java.run(
ctx = ctx,
host_javabase = common.get_host_javabase(ctx),
executable = android_toolchain.resource_shrinker.files_to_run,
arguments = [args],
inputs = [proto_resource_apk, final_classes_dex_zip, final_proguard_output_map],
outputs = [proto_resource_apk_shrunk, resource_shrinking_usage_log],
outputs = [proto_resource_apk_shrunk, resource_shrinking_usage_log, resource_shrinking_res_config],
mnemonic = "ResourceShrinkerForR8",
progress_message = "Shrinking resources %{label}",
)
Expand All @@ -207,12 +209,33 @@ def process_resource_shrinking_r8(ctx, r8_ctx, packaged_resources_ctx, **_unused
toolchain = ANDROID_TOOLCHAIN_TYPE,
)

aari = packaged_resources_ctx.android_application_resource
# 4. Optimize resources (shorten resource path names, remove resource names, collapse resource values)
resource_apk_optimized = ctx.actions.declare_file(ctx.label.name + "_resource_apk_optimized.ap_")
resource_obfuscation_map = ctx.actions.declare_file(ctx.label.name + "_resource_obfuscation.map")
ctx.actions.run(
arguments = [ctx.actions.args()
.add("optimize")
.add(resource_apk_shrunk)
.add("-o", resource_apk_optimized)
.add("--resources-config-path", resource_shrinking_res_config)
.add("--collapse-resource-names")
.add("--shorten-resource-paths")
.add("--deduplicate-entry-values")
.add("--save-obfuscation-map", resource_obfuscation_map)
],
executable = android_toolchain.aapt2.files_to_run,
inputs = [resource_apk_shrunk, resource_shrinking_res_config],
mnemonic = "Aapt2OptimizeForResourceShrinkerR8",
outputs = [resource_apk_optimized, resource_obfuscation_map],
toolchain = ANDROID_TOOLCHAIN_TYPE,
)

aari = android_application_resource

# Replace the resource apk in the AndroidApplicationResourceInfo provider from resource
# processing.
new_aari = AndroidApplicationResourceInfo(
resource_apk = resource_apk_shrunk,
resource_apk = resource_apk_optimized,
resource_java_src_jar = aari.resource_java_src_jar,
resource_java_class_jar = aari.resource_java_class_jar,
manifest = aari.manifest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.devtools.build.android.shrinker;

import com.android.build.shrinker.ResourceShrinkerImpl;
import com.android.build.shrinker.ResourceShrinkerModel;
import com.android.build.shrinker.FileReporter;
import com.android.build.shrinker.NoDebugReporter;
import com.android.build.shrinker.LinkedResourcesFormat;
Expand All @@ -30,9 +31,13 @@
import com.android.build.shrinker.usages.ToolsAttributeUsageRecorder;
import com.android.utils.FileUtils;
import java.io.IOException;
import java.lang.IllegalAccessException;
import java.lang.NoSuchFieldException;
import java.io.PrintStream;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -50,6 +55,7 @@ public class BazelResourceShrinkerCli {
private static final String PROGUARD_MAPPING_ARG = "--proguard_mapping";
private static final String HELP_ARG = "--help";
private static final String PRINT_USAGE_LOG = "--print_usage_log";
private static final String PRINT_CONFIG = "--print_config";

private static final String ANDROID_MANIFEST_XML = "AndroidManifest.xml";
private static final String RESOURCES_PB = "resources.pb";
Expand All @@ -60,6 +66,7 @@ private static class Options {
private final List<String> dex_inputs = new ArrayList<>();
private String output;
private String usageLog;
private String printConfig;
private Boolean preciseShrinking = Boolean.FALSE;
private final List<String> rawResources = new ArrayList<>();
private String proguardMapping;
Expand Down Expand Up @@ -127,6 +134,17 @@ public static Options parseOptions(String[] args) {
"More than usage log not supported");
}
options.usageLog = args[i];
} else if (arg.startsWith(PRINT_CONFIG)) {
i++;
if (i == args.length) {
throw new ResourceShrinkingFailedException(
"No argument given for print config");
}
if (options.printConfig != null) {
throw new ResourceShrinkingFailedException(
"More than print config not supported");
}
options.printConfig = args[i];
} else if (arg.startsWith(RES_ARG)) {
i++;
if (i == args.length) {
Expand Down Expand Up @@ -186,14 +204,14 @@ protected static ResourceShrinkerImpl run(String[] args) {
validateOptions(options);
ResourceShrinkerImpl resourceShrinker = runResourceShrinking(options);
return resourceShrinker;
} catch (IOException | ParserConfigurationException | SAXException e) {
} catch (IOException | ParserConfigurationException | SAXException | IllegalAccessException | NoSuchFieldException e) {
throw new ResourceShrinkingFailedException(
"Failed running resource shrinking: " + e.getMessage(), e);
}
}

private static ResourceShrinkerImpl runResourceShrinking(Options options)
throws IOException, ParserConfigurationException, SAXException {
throws IOException, ParserConfigurationException, SAXException, IllegalAccessException, NoSuchFieldException {
validateInput(options.getInput());
List<ResourceUsageRecorder> resourceUsageRecorders = new ArrayList<>();
for (String dexInput : options.dex_inputs) {
Expand Down Expand Up @@ -245,6 +263,16 @@ private static ResourceShrinkerImpl runResourceShrinking(Options options)
options.getPreciseShrinking());
resourceShrinker.analyze();

// Dump resource config for use in `aapt2 optimize` command.
if (options.printConfig != null) {
try (PrintWriter out = new PrintWriter(Paths.get(options.printConfig).toFile())) {
Field modelField = resourceShrinker.getClass().getDeclaredField("model");
modelField.setAccessible(true);
ResourceShrinkerModel model = (ResourceShrinkerModel) modelField.get(resourceShrinker);
out.println(model.getResourceStore().dumpConfig());
}
}

resourceShrinker.rewriteResourcesInApkFormat(
protoApk.toFile(), protoApkOut.toFile(), LinkedResourcesFormat.PROTO);
return resourceShrinker;
Expand Down

0 comments on commit 624ab07

Please sign in to comment.