Add TestApexes to ApexInfo

If any of apexes in apex_available is an apex_test, then that name will
be propagated down from that apex to each apex variant. This metadata
will be used to enforce that stub libraries cannot have more than one
apex_available.

This logic is necessary so that bp2build can select the correct
stub/impl.

(To avoid replicating this complexity in Bazel, we should consider
dropping the test apexes in Bazel BUILD files, next CL)

Bug: 277651159
Test: go build ./apex
Change-Id: I63617c1dc2a2d5c9cd7758c416fec7b4db1f10a7
diff --git a/apex/apex.go b/apex/apex.go
index 5451a04..e3f53b8 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -1064,6 +1064,10 @@
 
 	apexVariationName := mctx.ModuleName() // could be com.android.foo
 	a.properties.ApexVariationName = apexVariationName
+	testApexes := []string{}
+	if a.testApex {
+		testApexes = []string{apexVariationName}
+	}
 	apexInfo := android.ApexInfo{
 		ApexVariationName: apexVariationName,
 		MinSdkVersion:     minSdkVersion,
@@ -1072,6 +1076,7 @@
 		InApexVariants:    []string{apexVariationName},
 		InApexModules:     []string{a.Name()}, // could be com.mycompany.android.foo
 		ApexContents:      []*android.ApexContents{apexContents},
+		TestApexes:        testApexes,
 	}
 	mctx.WalkDeps(func(child, parent android.Module) bool {
 		if !continueApexDepsWalk(child, parent) {