Turn on the instrumentation by default for the java code in APEXes

Bug: 149353192
Test:  DIST_DIR=dist_dir TARGET_BUILD_VARIANT=userdebug
EMMA_INSTRUMENT=true NATIVE_COVERAGE=true
TARGET_BUILD_APPS="com.android.media"
vendor/google/build/build_unbundled_coverage_mainline_module.sh and
check that jacoco-report-classes.jar is created.
Change-Id: I28e3dc82b7913f60b2278126fca255306e48a424
diff --git a/java/java.go b/java/java.go
index dd44d06..182dbc3 100644
--- a/java/java.go
+++ b/java/java.go
@@ -1459,6 +1459,11 @@
 		j.headerJarFile = j.implementationJarFile
 	}
 
+	// Force enable the instrumentation for java code that is built for APEXes
+	if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !j.IsForPlatform() {
+		j.properties.Instrument = true
+	}
+
 	if j.shouldInstrument(ctx) {
 		outputFile = j.instrument(ctx, flags, outputFile, jarName)
 	}