Throw contextual error when compiling value xmls with >1 period in filename with aapt2, instead of just failing with "<file>.arsc.flat does not exist after aapt2 ran":

SEVERE: aapt2 does not support compiling resource xmls with multiple periods in the filename: Style.Cell.xml

Closes https://github.com/bazelbuild/bazel/issues/6042

Change-Id: Ic99de2179630fb3f92c2850621e1f067902b5de9
PiperOrigin-RevId: 211825750
diff --git a/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceCompiler.java b/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceCompiler.java
index 92ca14b..8f7fedf 100644
--- a/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceCompiler.java
+++ b/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceCompiler.java
@@ -172,10 +172,29 @@
     }
 
     static String interpolateAapt2Filename(Qualifiers qualifiers, String filename) {
-      return qualifiers.asFolderType().equals(ResourceFolderType.VALUES)
-          ? (filename.indexOf('.') != -1 ? filename.substring(0, filename.indexOf('.')) : filename)
-              + ".arsc"
-          : filename;
+      // res/<not values>/foo.bar -> foo.bar
+      if (!qualifiers.asFolderType().equals(ResourceFolderType.VALUES)) {
+        return filename;
+      }
+
+      int periodIndex = filename.indexOf('.');
+
+      // res/values/foo -> foo.arsc
+      if (periodIndex == -1) {
+        return filename + ".arsc";
+      }
+
+      // res/values/foo.bar.baz -> throw error.
+      if (filename.lastIndexOf('.') != periodIndex) {
+        throw new CompileError(
+            new IllegalArgumentException(
+                "aapt2 does not support compiling resource xmls with multiple periods in the "
+                    + "filename: "
+                    + filename));
+      }
+
+      // res/values/foo.xml -> foo.arsc
+      return filename.substring(0, periodIndex) + ".arsc";
     }
 
     private void compile(