Include java resources in app bundles

The java resources are merged into the dex jar, split them back
out into the root/ directory for merging into the bundle.
Also avoid running zip2zip when dexJarFile is nil by splitting
each munge step out into its own rule.

Bug: 117295826
Test: tapas RecoveryLocalizer && m dist
Change-Id: Ia22c8768bf2c3c6884792e923caf7624b4f76664
diff --git a/java/app_builder.go b/java/app_builder.go
index 476eb60..b0983bd 100644
--- a/java/app_builder.go
+++ b/java/app_builder.go
@@ -63,7 +63,7 @@
 	})
 
 func CreateAppPackage(ctx android.ModuleContext, outputFile android.WritablePath,
-	resJarFile, jniJarFile, dexJarFile android.Path, certificates []Certificate) {
+	packageFile, jniJarFile, dexJarFile android.Path, certificates []Certificate) {
 
 	unsignedApk := android.PathForModuleOut(ctx, "unsigned.apk")
 
@@ -71,7 +71,7 @@
 	if dexJarFile != nil {
 		inputs = append(inputs, dexJarFile)
 	}
-	inputs = append(inputs, resJarFile)
+	inputs = append(inputs, packageFile)
 	if jniJarFile != nil {
 		inputs = append(inputs, jniJarFile)
 	}
@@ -136,45 +136,65 @@
 
 var buildBundleModule = pctx.AndroidStaticRule("buildBundleModule",
 	blueprint.RuleParams{
-		Command: `${config.Zip2ZipCmd} -i ${in} -o ${out}.res.zip AndroidManifest.xml:manifest/AndroidManifest.xml resources.pb "res/**/*" "assets/**/*" &&` +
-			`${config.Zip2ZipCmd} -i ${dexJar} -o ${out}.dex.zip "classes*.dex:dex/" && ` +
-			`${config.MergeZipsCmd} ${out} ${out}.res.zip ${out}.dex.zip ${jniJar} && ` +
-			`rm ${out}.res.zip ${out}.dex.zip`,
-		CommandDeps: []string{
-			"${config.Zip2ZipCmd}",
-			"${config.MergeZipsCmd}",
-		},
-	}, "dexJar", "jniJar")
+		Command:     `${config.MergeZipsCmd} ${out} ${in}`,
+		CommandDeps: []string{"${config.MergeZipsCmd}"},
+	})
+
+var bundleMungePackage = pctx.AndroidStaticRule("bundleMungePackage",
+	blueprint.RuleParams{
+		Command:     `${config.Zip2ZipCmd} -i ${in} -o ${out} AndroidManifest.xml:manifest/AndroidManifest.xml resources.pb "res/**/*" "assets/**/*"`,
+		CommandDeps: []string{"${config.Zip2ZipCmd}"},
+	})
+
+var bundleMungeDexJar = pctx.AndroidStaticRule("bundleMungeDexJar",
+	blueprint.RuleParams{
+		Command: `${config.Zip2ZipCmd} -i ${in} -o ${out} "classes*.dex:dex/" && ` +
+			`${config.Zip2ZipCmd} -i ${in} -o ${resJar} -x "classes*.dex" "**/*:root/"`,
+		CommandDeps: []string{"${config.Zip2ZipCmd}"},
+	}, "resJar")
 
 // Builds an app into a module suitable for input to bundletool
 func BuildBundleModule(ctx android.ModuleContext, outputFile android.WritablePath,
-	resJarFile, jniJarFile, dexJarFile android.Path) {
+	packageFile, jniJarFile, dexJarFile android.Path) {
 
 	protoResJarFile := android.PathForModuleOut(ctx, "package-res.pb.apk")
-	aapt2Convert(ctx, protoResJarFile, resJarFile)
+	aapt2Convert(ctx, protoResJarFile, packageFile)
 
-	var deps android.Paths
-	var jniPath string
-	var dexPath string
+	var zips android.Paths
+
+	mungedPackage := android.PathForModuleOut(ctx, "bundle", "apk.zip")
+	ctx.Build(pctx, android.BuildParams{
+		Rule:        bundleMungePackage,
+		Input:       protoResJarFile,
+		Output:      mungedPackage,
+		Description: "bundle apk",
+	})
+	zips = append(zips, mungedPackage)
+
 	if dexJarFile != nil {
-		deps = append(deps, dexJarFile)
-		dexPath = dexJarFile.String()
+		mungedDexJar := android.PathForModuleOut(ctx, "bundle", "dex.zip")
+		mungedResJar := android.PathForModuleOut(ctx, "bundle", "res.zip")
+		ctx.Build(pctx, android.BuildParams{
+			Rule:           bundleMungeDexJar,
+			Input:          dexJarFile,
+			Output:         mungedDexJar,
+			ImplicitOutput: mungedResJar,
+			Description:    "bundle dex",
+			Args: map[string]string{
+				"resJar": mungedResJar.String(),
+			},
+		})
+		zips = append(zips, mungedDexJar, mungedResJar)
 	}
 	if jniJarFile != nil {
-		deps = append(deps, jniJarFile)
-		jniPath = jniJarFile.String()
+		zips = append(zips, jniJarFile)
 	}
 
 	ctx.Build(pctx, android.BuildParams{
 		Rule:        buildBundleModule,
-		Implicits:   deps,
-		Input:       protoResJarFile,
+		Inputs:      zips,
 		Output:      outputFile,
 		Description: "bundle",
-		Args: map[string]string{
-			"dexJar": dexPath,
-			"jniJar": jniPath,
-		},
 	})
 }