Support adding additional raw xml files to R8 resource shrinking

To ensure that we can parse all xml keep rules we introduce a new Kind for passing in rules. Rules are explicitly not output again, and only used for extracting keep rules.

This will ensure that we are compatible with the old resource shrinker
and additionally the setup can ensure that we will not hit cases where
aapt have removed duplicates (AGP can pass all files from the
individual res folders, even when there are name clashes)

Bug: b/287398085
Change-Id: I167c75e3b244d96870524e8a67141a22dd463d68
diff --git a/src/resourceshrinker/java/com/android/build/shrinker/r8integration/R8ResourceShrinkerState.java b/src/resourceshrinker/java/com/android/build/shrinker/r8integration/R8ResourceShrinkerState.java
index 2dd02e8..5ac9755 100644
--- a/src/resourceshrinker/java/com/android/build/shrinker/r8integration/R8ResourceShrinkerState.java
+++ b/src/resourceshrinker/java/com/android/build/shrinker/r8integration/R8ResourceShrinkerState.java
@@ -53,6 +53,7 @@
   private final Function<Exception, RuntimeException> errorHandler;
   private final R8ResourceShrinkerModel r8ResourceShrinkerModel;
   private final Map<String, Supplier<InputStream>> xmlFileProviders = new HashMap<>();
+  private final List<Supplier<InputStream>> keepRuleFileProviders = new ArrayList<>();
 
   private final List<Supplier<InputStream>> manifestProviders = new ArrayList<>();
   private final Map<String, Supplier<InputStream>> resfileProviders = new HashMap<>();
@@ -141,6 +142,10 @@
     this.xmlFileProviders.put(location, inputStreamSupplier);
   }
 
+  public void addKeepRuleRileProvider(Supplier<InputStream> inputStreamSupplier) {
+    this.keepRuleFileProviders.add(inputStreamSupplier);
+  }
+
   public void addResFileProvider(Supplier<InputStream> inputStreamSupplier, String location) {
     this.resfileProviders.put(location, inputStreamSupplier);
   }
@@ -308,11 +313,9 @@
   }
 
   public void updateModelWithKeepXmlReferences() throws IOException {
-    for (Map.Entry<String, Supplier<InputStream>> entry : xmlFileProviders.entrySet()) {
-      if (entry.getKey().startsWith("res/raw")) {
-        ToolsAttributeUsageRecorderKt.processRawXml(
-            getUtfReader(entry.getValue().get().readAllBytes()), r8ResourceShrinkerModel);
-      }
+    for (Supplier<InputStream> keepRuleFileProvider : keepRuleFileProviders) {
+      ToolsAttributeUsageRecorderKt.processRawXml(
+          getUtfReader(keepRuleFileProvider.get().readAllBytes()), r8ResourceShrinkerModel);
     }
   }