Add EMMA_INSTRUMENT_STATIC support

Add jacocoagent when instrumenting with EMMA_INSTRUMENT_STATIC.

Test: m EMMA_INSTRUMENT=true EMMA_INSTRUMENT_STATIC=true
Change-Id: I451bb9d16b7f0a66fd06c2da576062b28830e470
diff --git a/java/dex.go b/java/dex.go
index 2beb2ac..66e71b5 100644
--- a/java/dex.go
+++ b/java/dex.go
@@ -185,6 +185,11 @@
 		android.PathForSource(ctx, "build/make/core/proguard.flags"),
 	}
 
+	if j.shouldInstrumentStatic(ctx) {
+		flagFiles = append(flagFiles,
+			android.PathForSource(ctx, "build/make/core/proguard.jacoco.flags"))
+	}
+
 	flagFiles = append(flagFiles, j.extraProguardFlagFiles...)
 	// TODO(ccross): static android library proguard files
 
diff --git a/java/java.go b/java/java.go
index 24debac..8f58c6e 100644
--- a/java/java.go
+++ b/java/java.go
@@ -319,6 +319,16 @@
 	}
 }
 
+func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
+	return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
+}
+
+func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
+	return j.shouldInstrument(ctx) &&
+		(ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
+			ctx.Config().UnbundledBuild())
+}
+
 func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
 	i := sdkStringToNumber(ctx, v)
 	if i == -1 {
@@ -443,6 +453,10 @@
 		// Kotlin files
 		ctx.AddDependency(ctx.Module(), kotlinStdlibTag, "kotlin-stdlib")
 	}
+
+	if j.shouldInstrumentStatic(ctx) {
+		ctx.AddDependency(ctx.Module(), staticLibTag, "jacocoagent")
+	}
 }
 
 func hasSrcExt(srcs []string, ext string) bool {
@@ -871,7 +885,7 @@
 		}
 	}
 
-	if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") && j.properties.Instrument {
+	if j.shouldInstrument(ctx) {
 		outputFile = j.instrument(ctx, flags, outputFile, jarName)
 	}