Regenerate R.java files from LOCAL_STATIC_ANDROID_LIBRARIES

If a static android library lib1 has static_libs: ["lib2"] then the
R.class files for packages in lib2 will be merged into the jar for
lib1.  If an app has lib1 in its static_libs it will get the R.class
files from lib2 through lib1, instead of regenerating the R.java
files with numbering that matches the resource table of the app.

Pass transtive static android library dependencies on the aapt2
command line so that aapt2 will always regenerate the R.java
constants for those packages.

Also extract the packages that have R.java files after each aapt2
invocation.  This is not necessary for Soong, but is passed to
make to let it force regenerating the packages using
--extra-packages.

Bug: 78300023
Test: m checkbuild
Change-Id: I0f3444af44d2a9f370d1f156c908972f8cc3a1ee
diff --git a/java/aar.go b/java/aar.go
index 47676fd..9e5cddb 100644
--- a/java/aar.go
+++ b/java/aar.go
@@ -26,6 +26,7 @@
 	Dependency
 	ExportPackage() android.Path
 	ExportedProguardFlagFiles() android.Paths
+	ExportedStaticPackages() android.Paths
 }
 
 func init() {
@@ -58,12 +59,13 @@
 }
 
 type aapt struct {
-	aaptSrcJar          android.Path
-	exportPackage       android.Path
-	manifestPath        android.Path
-	proguardOptionsFile android.Path
-	rroDirs             android.Paths
-	rTxt                android.Path
+	aaptSrcJar            android.Path
+	exportPackage         android.Path
+	manifestPath          android.Path
+	proguardOptionsFile   android.Path
+	rroDirs               android.Paths
+	rTxt                  android.Path
+	extraAaptPackagesFile android.Path
 
 	aaptProperties aaptProperties
 }
@@ -123,9 +125,9 @@
 	linkFlags = append(linkFlags, android.JoinWithPrefix(assetDirs.Strings(), "-A "))
 	linkDeps = append(linkDeps, assetFiles...)
 
-	staticLibs, libDeps, libFlags := aaptLibs(ctx, sdkVersion)
+	transitiveStaticLibs, libDeps, libFlags := aaptLibs(ctx, sdkVersion)
 
-	overlayFiles = append(overlayFiles, staticLibs...)
+	overlayFiles = append(overlayFiles, transitiveStaticLibs...)
 	linkDeps = append(linkDeps, libDeps...)
 	linkFlags = append(linkFlags, libFlags...)
 
@@ -178,6 +180,8 @@
 	srcJar := android.PathForModuleGen(ctx, "R.jar")
 	proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
 	rTxt := android.PathForModuleOut(ctx, "R.txt")
+	// This file isn't used by Soong, but is generated for exporting
+	extraPackages := android.PathForModuleOut(ctx, "extra_packages")
 
 	var compiledRes, compiledOverlay android.Paths
 	for _, dir := range resDirs {
@@ -189,7 +193,7 @@
 
 	compiledOverlay = append(compiledOverlay, overlayFiles...)
 
-	aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt,
+	aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
 		linkFlags, linkDeps, compiledRes, compiledOverlay)
 
 	a.aaptSrcJar = srcJar
@@ -197,11 +201,14 @@
 	a.manifestPath = manifestPath
 	a.proguardOptionsFile = proguardOptionsFile
 	a.rroDirs = rroDirs
+	a.extraAaptPackagesFile = extraPackages
 	a.rTxt = rTxt
 }
 
 // aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
-func aaptLibs(ctx android.ModuleContext, sdkVersion string) (staticLibs, deps android.Paths, flags []string) {
+func aaptLibs(ctx android.ModuleContext, sdkVersion string) (transitiveStaticLibs, deps android.Paths,
+	flags []string) {
+
 	var sharedLibs android.Paths
 
 	sdkDep := decodeSdkDep(ctx, sdkVersion)
@@ -211,7 +218,8 @@
 
 	ctx.VisitDirectDeps(func(module android.Module) {
 		var exportPackage android.Path
-		if aarDep, ok := module.(AndroidLibraryDependency); ok {
+		aarDep, _ := module.(AndroidLibraryDependency)
+		if aarDep != nil {
 			exportPackage = aarDep.ExportPackage()
 		}
 
@@ -222,15 +230,16 @@
 			}
 		case staticLibTag:
 			if exportPackage != nil {
-				staticLibs = append(staticLibs, exportPackage)
+				transitiveStaticLibs = append(transitiveStaticLibs, exportPackage)
+				transitiveStaticLibs = append(transitiveStaticLibs, aarDep.ExportedStaticPackages()...)
 			}
 		}
 	})
 
 	deps = append(deps, sharedLibs...)
-	deps = append(deps, staticLibs...)
+	deps = append(deps, transitiveStaticLibs...)
 
-	if len(staticLibs) > 0 {
+	if len(transitiveStaticLibs) > 0 {
 		flags = append(flags, "--auto-add-overlay")
 	}
 
@@ -238,7 +247,9 @@
 		flags = append(flags, "-I "+sharedLib.String())
 	}
 
-	return staticLibs, deps, flags
+	transitiveStaticLibs = android.FirstUniquePaths(transitiveStaticLibs)
+
+	return transitiveStaticLibs, deps, flags
 }
 
 type AndroidLibrary struct {
@@ -250,12 +261,17 @@
 	aarFile android.WritablePath
 
 	exportedProguardFlagFiles android.Paths
+	exportedStaticPackages    android.Paths
 }
 
 func (a *AndroidLibrary) ExportedProguardFlagFiles() android.Paths {
 	return a.exportedProguardFlagFiles
 }
 
+func (a *AndroidLibrary) ExportedStaticPackages() android.Paths {
+	return a.exportedStaticPackages
+}
+
 var _ AndroidLibraryDependency = (*AndroidLibrary)(nil)
 
 func (a *AndroidLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
@@ -290,10 +306,13 @@
 	ctx.VisitDirectDeps(func(m android.Module) {
 		if lib, ok := m.(AndroidLibraryDependency); ok && ctx.OtherModuleDependencyTag(m) == staticLibTag {
 			a.exportedProguardFlagFiles = append(a.exportedProguardFlagFiles, lib.ExportedProguardFlagFiles()...)
+			a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportPackage())
+			a.exportedStaticPackages = append(a.exportedStaticPackages, lib.ExportedStaticPackages()...)
 		}
 	})
 
 	a.exportedProguardFlagFiles = android.FirstUniquePaths(a.exportedProguardFlagFiles)
+	a.exportedStaticPackages = android.FirstUniquePaths(a.exportedStaticPackages)
 }
 
 func AndroidLibraryFactory() android.Module {
@@ -331,9 +350,12 @@
 
 	properties AARImportProperties
 
-	classpathFile android.WritablePath
-	proguardFlags android.WritablePath
-	exportPackage android.WritablePath
+	classpathFile         android.WritablePath
+	proguardFlags         android.WritablePath
+	exportPackage         android.WritablePath
+	extraAaptPackagesFile android.WritablePath
+
+	exportedStaticPackages android.Paths
 }
 
 var _ AndroidLibraryDependency = (*AARImport)(nil)
@@ -346,6 +368,10 @@
 	return android.Paths{a.proguardFlags}
 }
 
+func (a *AARImport) ExportedStaticPackages() android.Paths {
+	return a.exportedStaticPackages
+}
+
 func (a *AARImport) Prebuilt() *android.Prebuilt {
 	return &a.prebuilt
 }
@@ -362,7 +388,7 @@
 		}
 	}
 
-	ctx.AddDependency(ctx.Module(), staticLibTag, a.properties.Libs...)
+	ctx.AddDependency(ctx.Module(), libTag, a.properties.Libs...)
 	ctx.AddDependency(ctx.Module(), staticLibTag, a.properties.Static_libs...)
 }
 
@@ -410,6 +436,7 @@
 	srcJar := android.PathForModuleGen(ctx, "R.jar")
 	proguardOptionsFile := android.PathForModuleGen(ctx, "proguard.options")
 	rTxt := android.PathForModuleOut(ctx, "R.txt")
+	a.extraAaptPackagesFile = android.PathForModuleOut(ctx, "extra_packages")
 
 	var linkDeps android.Paths
 
@@ -422,14 +449,14 @@
 	linkFlags = append(linkFlags, "--manifest "+manifest.String())
 	linkDeps = append(linkDeps, manifest)
 
-	staticLibs, libDeps, libFlags := aaptLibs(ctx, String(a.properties.Sdk_version))
+	transitiveStaticLibs, libDeps, libFlags := aaptLibs(ctx, String(a.properties.Sdk_version))
 
 	linkDeps = append(linkDeps, libDeps...)
 	linkFlags = append(linkFlags, libFlags...)
 
-	overlayRes := append(android.Paths{flata}, staticLibs...)
+	overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
 
-	aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt,
+	aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
 		linkFlags, linkDeps, nil, overlayRes)
 }