Find matching variant of `required` for `common_first`

Both 32-bit and 64-bit variants of native deps were getting installed in
Soong-built system image if they were listed in `required` of
`java_binary`.

java_binary(s) have two variants, a "common" and an arch
variant (the first arch). Previously the common variant will create a
dependency to both 32-bit and 64-bit variants of their dependencies.
With this CL, common variant will not create a dependency on required
unless the requested `target` is also the common target.

Test: Ran the filelistdiff tool
Test: go test ./java -run TestNativeRequiredDepOfJavaBinary
Bug: 369678122

Change-Id: Ica97e12eefb45929ca653ec57c3339e4a3b72a76
diff --git a/java/java_test.go b/java/java_test.go
index 641bf40..e976b08 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -3132,3 +3132,37 @@
 		t.Errorf("top-level: Expected but not found: %v, Found but not expected: %v", left, right)
 	}
 }
+
+// Test that a dependency edge is created to the "first" variant of a native library listed in `required` of java_binary
+func TestNativeRequiredDepOfJavaBinary(t *testing.T) {
+	findDepsOfModule := func(ctx *android.TestContext, module android.Module, depName string) []blueprint.Module {
+		var ret []blueprint.Module
+		ctx.VisitDirectDeps(module, func(dep blueprint.Module) {
+			if dep.Name() == depName {
+				ret = append(ret, dep)
+			}
+		})
+		return ret
+	}
+
+	bp := cc.GatherRequiredDepsForTest(android.Android) + `
+java_binary {
+	name: "myjavabin",
+	main_class: "com.android.MyJava",
+	required: ["mynativelib"],
+}
+cc_library_shared {
+	name: "mynativelib",
+}
+`
+	res, _ := testJava(t, bp)
+	// The first variant installs the native library via the common variant, so check the deps of both variants.
+	nativeVariantDepsWithDups := findDepsOfModule(res, res.ModuleForTests("myjavabin", "android_arm64_armv8-a").Module(), "mynativelib")
+	nativeVariantDepsWithDups = append(nativeVariantDepsWithDups, findDepsOfModule(res, res.ModuleForTests("myjavabin", "android_common").Module(), "mynativelib")...)
+
+	nativeVariantDepsUnique := map[blueprint.Module]bool{}
+	for _, dep := range nativeVariantDepsWithDups {
+		nativeVariantDepsUnique[dep] = true
+	}
+	android.AssertIntEquals(t, "Create a dep on the first variant", 1, len(nativeVariantDepsUnique))
+}