Support package_splits

Pass the package_splits list from the property to aapt2 as
--split arguments, sign the extra outputs, install them, and
add them as extra output files for SourceFileProducer.

Bug: 127921149
Test: TestAppSplits
Change-Id: Id94a53ae6a8a68ec81e98abba2fefc9c23feaa7a
diff --git a/java/aar.go b/java/aar.go
index 3f13e59..29578e2 100644
--- a/java/aar.go
+++ b/java/aar.go
@@ -83,9 +83,18 @@
 	useEmbeddedDex        bool
 	usesNonSdkApis        bool
 
+	splitNames []string
+	splits     []split
+
 	aaptProperties aaptProperties
 }
 
+type split struct {
+	name   string
+	suffix string
+	path   android.Path
+}
+
 func (a *aapt) ExportPackage() android.Path {
 	return a.exportPackage
 }
@@ -248,8 +257,23 @@
 		compiledOverlay = append(compiledOverlay, aapt2Compile(ctx, dir.dir, dir.files).Paths()...)
 	}
 
+	var splitPackages android.WritablePaths
+	var splits []split
+
+	for _, s := range a.splitNames {
+		suffix := strings.Replace(s, ",", "_", -1)
+		path := android.PathForModuleOut(ctx, "package_"+suffix+".apk")
+		linkFlags = append(linkFlags, "--split", path.String()+":"+s)
+		splitPackages = append(splitPackages, path)
+		splits = append(splits, split{
+			name:   s,
+			suffix: suffix,
+			path:   path,
+		})
+	}
+
 	aapt2Link(ctx, packageRes, srcJar, proguardOptionsFile, rTxt, extraPackages,
-		linkFlags, linkDeps, compiledRes, compiledOverlay)
+		linkFlags, linkDeps, compiledRes, compiledOverlay, splitPackages)
 
 	a.aaptSrcJar = srcJar
 	a.exportPackage = packageRes
@@ -258,6 +282,7 @@
 	a.rroDirs = rroDirs
 	a.extraAaptPackagesFile = extraPackages
 	a.rTxt = rTxt
+	a.splits = splits
 }
 
 // aaptLibs collects libraries from dependencies and sdk_version and converts them into paths
@@ -564,7 +589,7 @@
 	overlayRes := append(android.Paths{flata}, transitiveStaticLibs...)
 
 	aapt2Link(ctx, a.exportPackage, srcJar, proguardOptionsFile, rTxt, a.extraAaptPackagesFile,
-		linkFlags, linkDeps, nil, overlayRes)
+		linkFlags, linkDeps, nil, overlayRes, nil)
 }
 
 var _ Dependency = (*AARImport)(nil)