Fix resource overlay order for static libraries

If a static library has static library dependencies then all resources
need to be moved to an overlay to maintain the correct ordering so
that a static library resource overlays the same resource in a
dependency.

Also fix the ordering of transitive static dependencies, a direct
dependency should override a transitive dependency.

Expand TestEnforceRRO to include a transitive static library and
verify both the direct resources and overlays, and rename it to
TestAndroidResources.

Bug: 124108931
Test: TestAndroidResources
Change-Id: I355f835a2ffb728af28aa208d951794c609e7409
diff --git a/java/aar.go b/java/aar.go
index 60fbe29..583a6fc 100644
--- a/java/aar.go
+++ b/java/aar.go
@@ -215,17 +215,17 @@
 
 	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 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 a.isLibrary {
+		// Otherwise, for a static library we treat all the resources equally with no overlay.
+		for _, compiledResDir := range compiledResDirs {
+			compiledRes = append(compiledRes, compiledResDir...)
+		}
 	} else if len(compiledResDirs) > 0 {
 		// Without static libraries, the first directory is our directory, which can then be
 		// overlaid by the rest.
@@ -278,8 +278,8 @@
 			}
 		case staticLibTag:
 			if exportPackage != nil {
-				transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
 				transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
+				transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
 				staticLibManifests = append(staticLibManifests, aarDep.ExportedManifest())
 				staticRRODirs = append(staticRRODirs, aarDep.ExportedRRODirs()...)
 			}