Add support for JNI libraries to android_app modules

Make android_app modules a MultiTargets module, which means the
common variant will have a list of Targets that it needs to handle.
Collect JNI libraries for each Target, and package them into or
alongside the APK.

Bug: 80095087
Test: app_test.go
Change-Id: Iabd3921e1d4c4b4cfcc7e131a0b0d9ab83b0ebbb
diff --git a/java/app_builder.go b/java/app_builder.go
index e27b1b7..b9b5f43 100644
--- a/java/app_builder.go
+++ b/java/app_builder.go
@@ -19,9 +19,11 @@
 // functions.
 
 import (
+	"path/filepath"
 	"strings"
 
 	"github.com/google/blueprint"
+	"github.com/google/blueprint/proptools"
 
 	"android/soong/android"
 )
@@ -61,16 +63,18 @@
 	})
 
 func CreateAppPackage(ctx android.ModuleContext, outputFile android.WritablePath,
-	resJarFile, dexJarFile android.Path, certificates []certificate) {
-
-	// TODO(ccross): JNI libs
+	resJarFile, jniJarFile, dexJarFile android.Path, certificates []certificate) {
 
 	unsignedApk := android.PathForModuleOut(ctx, "unsigned.apk")
 
-	inputs := android.Paths{resJarFile}
+	var inputs android.Paths
 	if dexJarFile != nil {
 		inputs = append(inputs, dexJarFile)
 	}
+	inputs = append(inputs, resJarFile)
+	if jniJarFile != nil {
+		inputs = append(inputs, jniJarFile)
+	}
 
 	ctx.Build(pctx, android.BuildParams{
 		Rule:   combineApk,
@@ -132,3 +136,37 @@
 		},
 	})
 }
+
+func TransformJniLibsToJar(ctx android.ModuleContext, outputFile android.WritablePath,
+	jniLibs []jniLib) {
+
+	var deps android.Paths
+	jarArgs := []string{
+		"-j", // junk paths, they will be added back with -P arguments
+	}
+
+	if !ctx.Config().UnbundledBuild() {
+		jarArgs = append(jarArgs, "-L 0")
+	}
+
+	for _, j := range jniLibs {
+		deps = append(deps, j.path)
+		jarArgs = append(jarArgs,
+			"-P "+targetToJniDir(j.target),
+			"-f "+j.path.String())
+	}
+
+	ctx.Build(pctx, android.BuildParams{
+		Rule:        zip,
+		Description: "zip jni libs",
+		Output:      outputFile,
+		Implicits:   deps,
+		Args: map[string]string{
+			"jarArgs": strings.Join(proptools.NinjaAndShellEscape(jarArgs), " "),
+		},
+	})
+}
+
+func targetToJniDir(target android.Target) string {
+	return filepath.Join("lib", target.Arch.Abi[0])
+}