[RPBB] Output the map file as produced by aapt2-optimize when resource path shortening is enabled.

PiperOrigin-RevId: 261967685
diff --git a/src/tools/android/java/com/google/devtools/build/android/Aapt2ResourcePackagingAction.java b/src/tools/android/java/com/google/devtools/build/android/Aapt2ResourcePackagingAction.java
index 4a687e6..05d37db 100644
--- a/src/tools/android/java/com/google/devtools/build/android/Aapt2ResourcePackagingAction.java
+++ b/src/tools/android/java/com/google/devtools/build/android/Aapt2ResourcePackagingAction.java
@@ -208,6 +208,11 @@
       if (options.resourcesOutput != null) {
         packagedResources.asArchive().writeTo(options.resourcesOutput, /* compress= */ false);
       }
+      if (aaptConfigOptions.resourcePathShorteningMapOutput != null) {
+        copy(
+            packagedResources.resourcePathShorteningMap(),
+            aaptConfigOptions.resourcePathShorteningMapOutput);
+      }
     }
   }
 
diff --git a/src/tools/android/java/com/google/devtools/build/android/aapt2/Aapt2ConfigOptions.java b/src/tools/android/java/com/google/devtools/build/android/aapt2/Aapt2ConfigOptions.java
index c0ae124..93a3fc8 100644
--- a/src/tools/android/java/com/google/devtools/build/android/aapt2/Aapt2ConfigOptions.java
+++ b/src/tools/android/java/com/google/devtools/build/android/aapt2/Aapt2ConfigOptions.java
@@ -17,6 +17,7 @@
 
 import com.android.repository.Revision;
 import com.google.devtools.build.android.Converters.ExistingPathConverter;
+import com.google.devtools.build.android.Converters.PathConverter;
 import com.google.devtools.build.android.Converters.RevisionConverter;
 import com.google.devtools.common.options.Converters.CommaSeparatedOptionListConverter;
 import com.google.devtools.common.options.Option;
@@ -27,7 +28,7 @@
 import java.nio.file.Path;
 import java.util.List;
 
-/** Aaprt2 specific configuration options. */
+/** Aapt2 specific configuration options. */
 public class Aapt2ConfigOptions extends OptionsBase {
   @Option(
     name = "aapt2",
@@ -88,14 +89,24 @@
   public TriState conditionalKeepRules;
 
   @Option(
-    name = "uncompressedExtensions",
-    defaultValue = "",
-    converter = CommaSeparatedOptionListConverter.class,
-    category = "config",
-    documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
-    effectTags = {OptionEffectTag.UNKNOWN},
-    help = "A list of file extensions not to compress."
-  )
+      name = "resourcePathShorteningMapOutput",
+      defaultValue = "null",
+      converter = PathConverter.class,
+      documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
+      effectTags = {OptionEffectTag.UNKNOWN},
+      help =
+          "Path to write the map of old resource paths to shortened paths, if resource path"
+              + " shortening is enabled.")
+  public Path resourcePathShorteningMapOutput;
+
+  @Option(
+      name = "uncompressedExtensions",
+      defaultValue = "",
+      converter = CommaSeparatedOptionListConverter.class,
+      category = "config",
+      documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
+      effectTags = {OptionEffectTag.UNKNOWN},
+      help = "A list of file extensions not to compress.")
   public List<String> uncompressedExtensions;
 
   @Option(
diff --git a/src/tools/android/java/com/google/devtools/build/android/aapt2/PackagedResources.java b/src/tools/android/java/com/google/devtools/build/android/aapt2/PackagedResources.java
index 7a388e0..eb54b7b 100644
--- a/src/tools/android/java/com/google/devtools/build/android/aapt2/PackagedResources.java
+++ b/src/tools/android/java/com/google/devtools/build/android/aapt2/PackagedResources.java
@@ -31,6 +31,8 @@
 
   public abstract Path mainDexProguard();
 
+  public abstract Path resourcePathShorteningMap();
+
   public abstract Path javaSourceDirectory();
 
   abstract Path resourceIds();
@@ -45,6 +47,7 @@
       Path rTxt,
       Path proguardConfig,
       Path mainDexProguard,
+      Path resourcePathShorteningMap,
       Path javaSourceDirectory,
       Path resourceIds,
       Path attributes,
@@ -55,6 +58,7 @@
         rTxt,
         proguardConfig,
         mainDexProguard,
+        resourcePathShorteningMap,
         javaSourceDirectory,
         resourceIds,
         attributes,
diff --git a/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceLinker.java b/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceLinker.java
index ece53da..2480c06 100644
--- a/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceLinker.java
+++ b/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceLinker.java
@@ -134,6 +134,7 @@
 
   private final ListeningExecutorService executorService;
   private final Path workingDirectory;
+  private final Path resourcePathShorteningMap;
 
   private List<StaticLibrary> linkAgainst = ImmutableList.of();
 
@@ -160,6 +161,7 @@
     this.aapt2 = aapt2;
     this.executorService = executorService;
     this.workingDirectory = workingDirectory;
+    this.resourcePathShorteningMap = workingDirectory.resolve("resource_path_shortening.map");
   }
 
   public static ResourceLinker create(
@@ -544,6 +546,8 @@
             .thenAdd("--target-densities", densities.stream().collect(Collectors.joining(",")))
             .when(enableResourcePathShortening)
             .thenAdd("--enable-resource-path-shortening")
+            .when(enableResourcePathShortening)
+            .thenAdd("--resource-path-shortening-map", resourcePathShorteningMap)
             .add("-o", optimized)
             .add(binary.toString())
             .execute(String.format("Optimizing %s", compiled.getManifest())));
@@ -559,6 +563,12 @@
       Path mainDexProguard = workingDirectory.resolve("proguard.maindex.cfg");
       Path javaSourceDirectory = Files.createDirectories(workingDirectory.resolve("java"));
       Path resourceIds = workingDirectory.resolve("ids.txt");
+      if (Files.notExists(resourcePathShorteningMap)) {
+        // We need to produce a path shortening map file regardless of whether shortening was
+        // activated with the --define flag. If we've reached here, it means the optimization was
+        // not performed, so output an empty file.
+        Files.createFile(resourcePathShorteningMap);
+      }
       try (ProtoApk protoApk =
           linkProtoApk(
               compiled, rTxt, proguardConfig, mainDexProguard, javaSourceDirectory, resourceIds)) {
@@ -570,6 +580,7 @@
             rTxt,
             proguardConfig,
             mainDexProguard,
+            resourcePathShorteningMap,
             javaSourceDirectory,
             resourceIds,
             extractAttributes(compiled),
diff --git a/third_party/ijar/test/BUILD b/third_party/ijar/test/BUILD
index f6c0433..151e59e 100644
--- a/third_party/ijar/test/BUILD
+++ b/third_party/ijar/test/BUILD
@@ -1,5 +1,3 @@
-load("@rules_java//java:defs.bzl", "java_binary", "java_library", "java_test")
-
 package(default_visibility = ["//visibility:__pkg__"])
 
 licenses(["notice"])  # Apache License 2.0