Track styleable and attr accurately in optimized shrinking

Instantiate the resource model including styleables
Ignore the resource model returned unused resources and explicitly only keep those that are reachable

Bug: b/319802261
Change-Id: I08d995c8e7715bdf3281667253021eebd94d109b
diff --git a/src/resourceshrinker/java/com/android/build/shrinker/r8integration/LegacyResourceShrinker.java b/src/resourceshrinker/java/com/android/build/shrinker/r8integration/LegacyResourceShrinker.java
index 2dbcf84..ab97ce2 100644
--- a/src/resourceshrinker/java/com/android/build/shrinker/r8integration/LegacyResourceShrinker.java
+++ b/src/resourceshrinker/java/com/android/build/shrinker/r8integration/LegacyResourceShrinker.java
@@ -131,12 +131,13 @@
     R8ResourceShrinkerModel model = new R8ResourceShrinkerModel(NoDebugReporter.INSTANCE, true);
     for (PathAndBytes pathAndBytes : resourceTables.keySet()) {
       ResourceTable loadedResourceTable = ResourceTable.parseFrom(pathAndBytes.bytes);
-      model.instantiateFromResourceTable(loadedResourceTable);
+      model.instantiateFromResourceTable(loadedResourceTable, false);
     }
-    return shrinkModel(model);
+    return shrinkModel(model, false);
   }
 
-  public ShrinkerResult shrinkModel(R8ResourceShrinkerModel model) throws IOException {
+  public ShrinkerResult shrinkModel(
+      R8ResourceShrinkerModel model, boolean exactMatchingOfStyleablesAndAttr) throws IOException {
     if (proguardMapStrings != null) {
       new ProguardMappingsRecorder(proguardMapStrings).recordObfuscationMappings(model);
       proguardMapStrings = null;
@@ -193,7 +194,7 @@
       }
     }
     List<Integer> resourceIdsToRemove =
-        unusedResources.stream().map(resource -> resource.value).collect(Collectors.toList());
+        getResourceIdsToRemove(unusedResources, model, exactMatchingOfStyleablesAndAttr);
     Map<FeatureSplit, ResourceTable> shrunkenTables = new HashMap<>();
     for (Entry<PathAndBytes, FeatureSplit> entry : resourceTables.entrySet()) {
       ResourceTable shrunkenResourceTable =
@@ -204,6 +205,19 @@
     return new ShrinkerResult(resEntriesToKeep.build(), shrunkenTables);
   }
 
+  private static List<Integer> getResourceIdsToRemove(
+      List<Resource> unusedResources,
+      R8ResourceShrinkerModel model,
+      boolean exactMatchingOfStyleablesAndAttr) {
+    if (!exactMatchingOfStyleablesAndAttr) {
+      return unusedResources.stream().map(resource -> resource.value).collect(Collectors.toList());
+    }
+    return model.getResourceStore().getResources().stream()
+        .filter(r -> !r.isReachable())
+        .map(r -> r.value)
+        .collect(Collectors.toList());
+  }
+
   // Lifted from com/android/utils/XmlUtils.java which we can't easily update internal dependency
   // for.
   /**