Add the transitive dependencies of boot_image to apex

This avoids having to specify boot libraries in both the boot_image
and separately as java_libs on the apex. Simply add them to the
boot_image (happens automatically ATM when using image_name: "art")
and add the boot_image to the apex.

Bug: 177892522
Test: m nothing
Change-Id: I7e0c41665604b73780cdf0dc555067497b1e6ef0
diff --git a/apex/apex.go b/apex/apex.go
index 80d9615..9d06e1c 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -1702,6 +1702,9 @@
 							filesInfo = append(filesInfo, af)
 						}
 					}
+
+					// Track transitive dependencies.
+					return true
 				}
 			case javaLibTag:
 				switch child.(type) {
@@ -1910,6 +1913,21 @@
 						filesInfo = append(filesInfo, af)
 						return true // track transitive dependencies
 					}
+				} else if java.IsbootImageContentDepTag(depTag) {
+					// Add the contents of the boot image to the apex.
+					switch child.(type) {
+					case *java.Library, *java.SdkLibrary:
+						af := apexFileForJavaModule(ctx, child.(javaModule))
+						if !af.ok() {
+							ctx.PropertyErrorf("boot_images", "boot image content %q is not configured to be compiled into dex", depName)
+							return false
+						}
+						filesInfo = append(filesInfo, af)
+						return true // track transitive dependencies
+					default:
+						ctx.PropertyErrorf("boot_images", "boot image content %q of type %q is not supported", depName, ctx.OtherModuleType(child))
+					}
+
 				} else if _, ok := depTag.(android.CopyDirectlyInAnyApexTag); ok {
 					// nothing
 				} else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
diff --git a/apex/boot_image_test.go b/apex/boot_image_test.go
index 8d91f5a..574166a 100644
--- a/apex/boot_image_test.go
+++ b/apex/boot_image_test.go
@@ -177,8 +177,10 @@
 			boot_images: [
 				"mybootimage",
 			],
+			// bar (like foo) should be transitively included in this apex because it is part of the
+			// mybootimage boot_image. However, it is kept here to ensure that the apex dedups the files
+			// correctly.
 			java_libs: [
-				"foo",
 				"bar",
 			],
 			updatable: false,
@@ -247,7 +249,6 @@
 	java.CheckModuleDependencies(t, result.TestContext, "com.android.art", "android_common_com.android.art_image", []string{
 		`bar`,
 		`com.android.art.key`,
-		`foo`,
 		`mybootimage`,
 	})
 }
@@ -365,8 +366,12 @@
 		}
 	`)
 
-	// The apex is empty because the contents of boot_image are not transitively included, yet!
-	ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex_image", []string{})
+	ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
+		// This does not include art, oat or vdex files as they are only included for the art boot
+		// image.
+		"javalib/bar.jar",
+		"javalib/foo.jar",
+	})
 
 	java.CheckModuleDependencies(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
 		`myapex.key`,
diff --git a/java/boot_image.go b/java/boot_image.go
index 0fc9c6c..0c47976 100644
--- a/java/boot_image.go
+++ b/java/boot_image.go
@@ -70,6 +70,10 @@
 
 var _ android.ExcludeFromVisibilityEnforcementTag = bootImageContentDepTag
 
+func IsbootImageContentDepTag(tag blueprint.DependencyTag) bool {
+	return tag == bootImageContentDepTag
+}
+
 type bootImageProperties struct {
 	// The name of the image this represents.
 	//
@@ -182,8 +186,8 @@
 func (b *BootImageModule) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
 	tag := ctx.OtherModuleDependencyTag(dep)
 	if tag == bootImageContentDepTag {
-		// Boot image contents are not automatically added to apex, yet.
-		return false
+		// Boot image contents are automatically added to apex.
+		return true
 	}
 	if android.IsMetaDependencyTag(tag) {
 		// Cross-cutting metadata dependencies are metadata.