Enable -Othroughput for aapt2 commands in android_local_test actions.

This change introduces an --optimize_throughput option to the resource packaging actions. When enabled, it passes the -Othroughput flag to aapt2 commands, which uses Level 1 APK compression for faster action execution in testing. This option is set to true for android_local_test targets.

PiperOrigin-RevId: 948586045
Change-Id: I59c16cab116ce524bbf124c4116f59f4d3b8963e
diff --git a/rules/android_local_test/impl.bzl b/rules/android_local_test/impl.bzl
index 3bbe206..fe61058 100644
--- a/rules/android_local_test/impl.bzl
+++ b/rules/android_local_test/impl.bzl
@@ -126,6 +126,7 @@
         # TODO(b/140582167): Throwing on resource conflict need to be rolled
         # out to android_local_test.
         should_throw_on_conflict = False,
+        optimize_throughput = True,
     )
 
     return ProviderInfo(
diff --git a/rules/busybox.bzl b/rules/busybox.bzl
index 64f5c8a..6ce5fda 100644
--- a/rules/busybox.bzl
+++ b/rules/busybox.bzl
@@ -232,6 +232,7 @@
         additional_apks_to_link_against = [],
         resource_apks = depset(),
         nocompress_extensions = [],
+        optimize_throughput = False,
         proto_format = False,
         shrink_resource_cycles = False,
         version_name = None,
@@ -290,6 +291,7 @@
       resource_apks: Depset of resource only apk files to link against.
       nocompress_extensions: A list of strings. File extension to leave uncompressed
         in the apk.
+      optimize_throughput: A boolean. Whether to instruct aapt to lower the compression level in exchange for faster execution time.
       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.
@@ -410,6 +412,8 @@
         input_files.extend(additional_apks_to_link_against)
     if nocompress_extensions:
         args.add_joined("--uncompressedExtensions", nocompress_extensions, join_with = ",")
+    if optimize_throughput and AAPT2_COMPAT_FLAGS["aapt2_othroughput_supported"]:
+        args.add("--optimize_throughput")
     if proto_format:
         args.add("--resourceTableAsProto")
     if shrink_resource_cycles:
diff --git a/rules/busybox_compat.bzl b/rules/busybox_compat.bzl
index b1aa1b3..fded9b8 100644
--- a/rules/busybox_compat.bzl
+++ b/rules/busybox_compat.bzl
@@ -15,4 +15,5 @@
 
 AAPT2_COMPAT_FLAGS = {
     "aapt2_skip_flat_files_fix": False,
+    "aapt2_othroughput_supported": False,
 }
diff --git a/rules/resources.bzl b/rules/resources.bzl
index 98d824b..ee1c615 100644
--- a/rules/resources.bzl
+++ b/rules/resources.bzl
@@ -480,6 +480,7 @@
         densities = [],
         resource_files = [],
         nocompress_extensions = [],
+        optimize_throughput = False,
         java_package = None,
         package_id = None,
         use_r_package = False,
@@ -530,6 +531,7 @@
         to be processed.
       nocompress_extensions: sequence of Strings. File extension to leave uncompressed
         in the apk.
+      optimize_throughput: A boolean. Whether to instruct aapt to lower the compression level in exchange for faster execution time.
       java_package: String. Java package for which java sources will be
         generated. By default the package is inferred from the directory where
         the BUILD file containing the rule is.
@@ -766,6 +768,7 @@
         resource_configs = resource_configs,
         densities = densities,
         nocompress_extensions = nocompress_extensions,
+        optimize_throughput = optimize_throughput,
         java_package = java_package,
         shrink_resource_cycles = shrink_resource_cycles,
         version_name = manifest_values[_VERSION_NAME] if _VERSION_NAME in manifest_values else None,
diff --git a/src/tools/java/com/google/devtools/build/android/Aapt2ResourcePackagingAction.java b/src/tools/java/com/google/devtools/build/android/Aapt2ResourcePackagingAction.java
index 95afbb4..22f8243 100644
--- a/src/tools/java/com/google/devtools/build/android/Aapt2ResourcePackagingAction.java
+++ b/src/tools/java/com/google/devtools/build/android/Aapt2ResourcePackagingAction.java
@@ -412,6 +412,7 @@
               .includeOnlyConfigs(aaptConfigOptions.resourceConfigs)
               .includeProguardLocationReferences(options.includeProguardLocationReferences)
               .featureFlags(aaptConfigOptions.featureFlags)
+              .optimizeThroughput(aaptConfigOptions.optimizeThroughput)
               .link(compiled);
       profiler.recordEndOf("link").startTask("validate");
 
diff --git a/src/tools/java/com/google/devtools/build/android/aapt2/Aapt2ConfigOptions.java b/src/tools/java/com/google/devtools/build/android/aapt2/Aapt2ConfigOptions.java
index 28b1e30..0fdf363 100644
--- a/src/tools/java/com/google/devtools/build/android/aapt2/Aapt2ConfigOptions.java
+++ b/src/tools/java/com/google/devtools/build/android/aapt2/Aapt2ConfigOptions.java
@@ -73,6 +73,14 @@
       description = "A list of file extensions not to compress.")
   public List<String> uncompressedExtensions = ImmutableList.of();
 
+  @Parameter(
+      names = "--optimize_throughput",
+      arity = 1,
+      description =
+          "Whether to instruct aapt to lower the compression level in exchange for faster execution"
+              + " time.")
+  public boolean optimizeThroughput;
+
   @Parameter(names = "--debug", description = "Indicates if it is a debug build.", arity = 1)
   public boolean debug;
 
diff --git a/src/tools/java/com/google/devtools/build/android/aapt2/ResourceLinker.java b/src/tools/java/com/google/devtools/build/android/aapt2/ResourceLinker.java
index 0b76039..6a342d4 100644
--- a/src/tools/java/com/google/devtools/build/android/aapt2/ResourceLinker.java
+++ b/src/tools/java/com/google/devtools/build/android/aapt2/ResourceLinker.java
@@ -167,6 +167,7 @@
   private boolean includeProguardLocationReferences = false;
   private List<StaticLibrary> resourceApks = ImmutableList.of();
   private String featureFlags = "";
+  private boolean optimizeThroughput = false;
 
   private ResourceLinker(
       Path aapt2, ListeningExecutorService executorService, Path workingDirectory) {
@@ -267,6 +268,12 @@
     return this;
   }
 
+  @CanIgnoreReturnValue
+  public ResourceLinker optimizeThroughput(boolean optimizeThroughput) {
+    this.optimizeThroughput = optimizeThroughput;
+    return this;
+  }
+
   /**
    * Statically links the {@link CompiledResources} with the dependencies to produce a {@link
    * StaticLibrary}.
@@ -288,6 +295,8 @@
               .forBuildToolsVersion(buildToolsVersion)
               .forVariantType(VariantTypeImpl.LIBRARY)
               .add("link")
+              .when(optimizeThroughput)
+              .thenAdd("-Othroughput")
               .when(outputAsProto) // Used for testing: aapt2 does not output static libraries in
               // proto format.
               .thenAdd("--proto-format")
@@ -446,6 +455,8 @@
             .forBuildToolsVersion(buildToolsVersion)
             .forVariantType(VariantTypeImpl.BASE_APK)
             .add("link")
+            .when(optimizeThroughput)
+            .thenAdd("-Othroughput")
             .whenVersionIsAtLeast(new Revision(23))
             .thenAdd("--no-version-vectors")
             // Turn off namespaced resources
@@ -657,6 +668,8 @@
             .forBuildToolsVersion(buildToolsVersion)
             .forVariantType(VariantTypeImpl.BASE_APK)
             .add("optimize")
+            .when(optimizeThroughput)
+            .thenAdd("-Othroughput")
             .when(Objects.equals(logger.getLevel(), Level.FINE))
             .thenAdd("-v")
             // TODO(b/138166830): Simplify behavior specific to number of densities. There's likely
@@ -714,6 +727,8 @@
               .forBuildToolsVersion(buildToolsVersion)
               .forVariantType(VariantTypeImpl.BASE_APK)
               .add("convert")
+              .when(optimizeThroughput)
+              .thenAdd("-Othroughput")
               .when(Objects.equals(logger.getLevel(), Level.FINE))
               .thenAdd("-v")
               .add("-o", apk.toString())