Fix code coverage for apps

When building apps with EMMA_INSTRUMENT=true, we must ensure that
jacoco is at least on the classpath (or even embedded for unbundled
branches) and that proguard will ignore jacoco classes. Otherwise
the Jack compilation would fail.

For unbundled build, we include the proguard configuration file
build/core/proguard.jacoco.flags to ignore jacoco classes.

For full build, we ensure jacoco is on the classpath during the
jack compilation.

Bug: 27607712
Change-Id: I0ad37d74e63028a8144a125f052dc1c5fa10fc66
diff --git a/core/package_internal.mk b/core/package_internal.mk
index 032dfbb..551f18e 100644
--- a/core/package_internal.mk
+++ b/core/package_internal.mk
@@ -241,21 +241,34 @@
 else
 ifdef LOCAL_SDK_VERSION
 ifdef TARGET_BUILD_APPS
-# In unbundled build merge the emma library into the apk.
+# In unbundled build, merge the coverage library into the apk.
 ifdef LOCAL_JACK_ENABLED
 # Jack supports coverage with Jacoco
 ifneq ($(LOCAL_SRC_FILES)$(LOCAL_STATIC_JAVA_LIBRARIES)$(LOCAL_SOURCE_FILES_ALL_GENERATED),)
 # Only add jacocoagent if the package contains some java code
 LOCAL_STATIC_JAVA_LIBRARIES += jacocoagent
+# Exclude jacoco classes from proguard
+LOCAL_PROGUARD_FLAGS += -include $(BUILD_SYSTEM)/proguard.jacoco.flags
+LOCAL_JACK_PROGUARD_FLAGS += -include $(BUILD_SYSTEM)/proguard.jacoco.flags
 endif # Contains java code
 else
 LOCAL_STATIC_JAVA_LIBRARIES += emma
 endif # LOCAL_JACK_ENABLED
 else
-# If build against the SDK in full build, core.jar is not used,
-# we have to use prebiult emma.jar to make Proguard happy;
+# If build against the SDK in full build, core.jar is not used
+# so coverage classes are not present.
+ifdef LOCAL_JACK_ENABLED
+# Jack needs jacoco on the classpath but we do not want it to be in
+# the final apk. While it is a static library, we add it to the
+# LOCAL_JAVA_LIBRARIES which are only present on the classpath.
+# Note: we have nothing to do for proguard since jacoco will be
+# on the classpath only, thus not modified during the compilation.
+LOCAL_JAVA_LIBRARIES += jacocoagent
+else
+# We have to use prebuilt emma.jar to make Proguard happy;
 # Otherwise emma classes are included in core.jar.
 LOCAL_PROGUARD_FLAGS += -libraryjars $(EMMA_JAR)
+endif # LOCAL_JACK_ENABLED
 endif # full build
 endif # LOCAL_SDK_VERSION
 endif # EMMA_INSTRUMENT_STATIC