Fix overlaying android resources from static libraries

Match the make logic for combining app resources with static library
resources.

Bug: 112822358
Test: app_test.go
Test: aapt2 dump resources $OUT/system/priv-app/SystemUIGoogle/SystemUIGoogle.apk | grep -A1 ' string/config_systemUIVendorServiceComponent'
Change-Id: I565404e7ffb726dab952c72ab23600d6f2ee4ad4
(cherry picked from commit 4aaa84af86b4f5c4add0b6370fd3491bf592bb08)
diff --git a/java/aar.go b/java/aar.go
index e90f984..e6a412c 100644
--- a/java/aar.go
+++ b/java/aar.go
@@ -186,16 +186,39 @@
 	// This file isn't used by Soong, but is generated for exporting
 	extraPackages := android.PathForModuleOut(ctx, "extra_packages")
 
-	var compiledRes, compiledOverlay android.Paths
+	var compiledResDirs []android.Paths
 	for _, dir := range resDirs {
-		compiledRes = append(compiledRes, aapt2Compile(ctx, dir.dir, dir.files).Paths()...)
+		compiledResDirs = append(compiledResDirs, aapt2Compile(ctx, dir.dir, dir.files).Paths())
 	}
+
+	var compiledRes, compiledOverlay android.Paths
+
+	compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
+
+	if a.isLibrary {
+		// For a static library we treat all the resources equally with no overlay.
+		for _, compiledResDir := range compiledResDirs {
+			compiledRes = append(compiledRes, compiledResDir...)
+		}
+	} else if len(transitiveStaticLibs) > 0 {
+		// If we are using static android libraries, every source file becomes an overlay.
+		// This is to emulate old AAPT behavior which simulated library support.
+		for _, compiledResDir := range compiledResDirs {
+			compiledOverlay = append(compiledOverlay, compiledResDir...)
+		}
+	} else if len(compiledResDirs) > 0 {
+		// Without static libraries, the first directory is our directory, which can then be
+		// overlaid by the rest.
+		compiledRes = append(compiledRes, compiledResDirs[0]...)
+		for _, compiledResDir := range compiledResDirs[1:] {
+			compiledOverlay = append(compiledOverlay, compiledResDir...)
+		}
+	}
+
 	for _, dir := range overlayDirs {
 		compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files).Paths()...)
 	}
 
-	compiledOverlay = append(compiledOverlay, transitiveStaticLibs...)
-
 	aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
 		linkFlags, linkDeps, compiledRes, compiledOverlay)