Don't use DirectlyInAnyApex to enable dex and coverage

DirectlyInAnyApex was being used to automatically enable dexing
and to enable coverage.  There is only one module in an apex that
isn't already being dexed, so set compile_dex in that module and
remove the automatic dexing.  Coverage is only useful on device
modules if they are dexed, so convert the shouldInstrumentInApex
test to check if it is dexed instead.

This reomves the last usages of DirectlyInAnyApex from the java
package.

Bug: 372543712
Test: No change to build.ninja
Change-Id: I1d1738305ed97d85669945def75e9916c2b41f3c
diff --git a/java/base.go b/java/base.go
index 7630ca5..d4d5f62 100644
--- a/java/base.go
+++ b/java/base.go
@@ -766,7 +766,8 @@
 	apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
 	isJacocoAgent := ctx.ModuleName() == "jacocoagent"
 
-	if j.DirectlyInAnyApex() && !isJacocoAgent && !apexInfo.IsForPlatform() {
+	compileDex := Bool(j.dexProperties.Compile_dex) || Bool(j.properties.Installable)
+	if compileDex && !isJacocoAgent && !apexInfo.IsForPlatform() {
 		if !inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
 			return true
 		} else if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
@@ -1735,20 +1736,10 @@
 
 	completeStaticLibsImplementationJarsToCombine := completeStaticLibsImplementationJars
 
-	// Enable dex compilation for the APEX variants, unless it is disabled explicitly
-	compileDex := Bool(j.dexProperties.Compile_dex)
 	apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
-	if j.DirectlyInAnyApex() && !apexInfo.IsForPlatform() {
-		if j.dexProperties.Compile_dex == nil {
-			compileDex = true
-		}
-		if j.deviceProperties.Hostdex == nil {
-			j.deviceProperties.Hostdex = proptools.BoolPtr(true)
-		}
-	}
-	if Bool(j.properties.Installable) {
-		compileDex = true
-	}
+
+	// Enable dex compilation for the APEX variants, unless it is disabled explicitly
+	compileDex := Bool(j.dexProperties.Compile_dex) || Bool(j.properties.Installable)
 
 	if j.shouldInstrument(ctx) && (!ctx.Device() || compileDex) {
 		instrumentedOutputFile := j.instrument(ctx, flags, outputFile, jarName, specs)
@@ -2331,7 +2322,10 @@
 		"stable.core.platform.api.stubs",
 		"stub-annotations", "private-stub-annotations-jar",
 		"core-lambda-stubs",
-		"core-generated-annotation-stubs":
+		"core-generated-annotation-stubs",
+		// jacocoagent only uses core APIs, but has to specify a non-core sdk_version so it can use
+		// a prebuilt SDK to avoid circular dependencies when it statically included in the bootclasspath.
+		"jacocoagent":
 		return javaCore, true
 	case android.SdkPublic.DefaultJavaLibraryName():
 		return javaSdk, true
diff --git a/java/testing.go b/java/testing.go
index 988514d..cb3245b 100644
--- a/java/testing.go
+++ b/java/testing.go
@@ -190,6 +190,7 @@
 				"//apex_available:anyapex",
 				"//apex_available:platform",
 			],
+			compile_dex: true,
 		}
 	`)),
 )