add jacocoagent by default to Java modules
On coverage builds, R8 will fail to properly optimize and fail the build
if ignore_warnings: false, because jacoco injects dependencies on
jacocoagent classes, but the jacocoagent library is not part of the
classpath libraries passed in to R8 in its arguments.
Instead we can add jacocoagent as a libs dependency for these modules so
that it will get pulled into the r8 flags.
Bug: 243903417
Test: m
Change-Id: Icc24cc260b896fc800125a0318308d823ccf7a83
diff --git a/java/jacoco.go b/java/jacoco.go
index e11c2ce..f8012b8 100644
--- a/java/jacoco.go
+++ b/java/jacoco.go
@@ -47,6 +47,34 @@
"strippedJar", "stripSpec", "tmpDir", "tmpJar")
)
+func jacocoDepsMutator(ctx android.BottomUpMutatorContext) {
+ type instrumentable interface {
+ shouldInstrument(ctx android.BaseModuleContext) bool
+ shouldInstrumentInApex(ctx android.BaseModuleContext) bool
+ setInstrument(value bool)
+ }
+
+ j, ok := ctx.Module().(instrumentable)
+ if !ctx.Module().Enabled() || !ok {
+ return
+ }
+
+ if j.shouldInstrumentInApex(ctx) {
+ j.setInstrument(true)
+ }
+
+ if j.shouldInstrument(ctx) && ctx.ModuleName() != "jacocoagent" {
+ // We can use AddFarVariationDependencies here because, since this dep
+ // is added as libs only (i.e. a compiletime CLASSPATH entry only),
+ // the first variant of jacocoagent is sufficient to prevent
+ // compile time errors.
+ // At this stage in the build, AddVariationDependencies is not always
+ // able to procure a variant of jacocoagent that matches the calling
+ // module.
+ ctx.AddFarVariationDependencies(ctx.Module().Target().Variations(), libTag, "jacocoagent")
+ }
+}
+
// Instruments a jar using the Jacoco command line interface. Uses stripSpec to extract a subset
// of the classes in inputJar into strippedJar, instruments strippedJar into tmpJar, and then
// combines the classes in tmpJar with inputJar (preferring the instrumented classes in tmpJar)