How to exclude byte code from 3rd party library from being instrumented by Jacoco gradle plugin?

For an Android application project with dependency on a 3rd party library: App A → Lib B (AAR file), by doing ‘assembleDebug’ gradle command, byte code from the 3rd party library is instrumented by Jacoco when the plugin is applied and ‘testCoverageEnabled’ is set to true. The behavior can be verified by de-assembling the APK and inspecting byte code belonging the 3rd party library.

Due to an unresolved issue with Jacoco: Jacoco inserting instructions between catch handlers in monitor blocks · Issue #1381 · jacoco/jacoco · GitHub, I wonder if it’s possible to exclude 3rd party library from being instrumented by Jacoco gradle plugin. Either a way to exclude the entire AAR file or a specific file or package within the archive would be helpful.

Thanks,

Sophat

I suspect this is not actually coming from the built-in JaCoCo plugin, as it does not even support offline-instrumentation for Java code, let alone Android code.
I would suspect that the Android Gradle Plugin does this, or that you apply some other plugin that does this JaCoCo offline instrumentation.

Not sue what you mean saying

I suspect this is not actually coming from the built-in JaCoCo plugin, as it does not even support offline-instrumentation for Java code, let alone Android code.

According to this: JaCoCo - Offline Instrumentation, Jacoco supports offline byte code instrument via ant task.

JaCoCo, yes.
The built-in JaCoCo Gradle plugin, no.

1 Like

By just building the APK with ‘testCoverageEnabled’ set to true without running any test, and then looking at the byte code, I can see that 2 members are added to each class which matches the description below from Jacoco FAQ.

To collect execution data JaCoCo instruments the classes under test which adds two members to the classes: A private static field $jacocoData and a private static method $jacocoInit(). Both members are marked as synthetic.
JaCoCo - FAQ

My questions are:

  1. Isn’t that considered offline instrumentation? Or is there a different term for it?
  2. Is there a way to exclude certain class/package from having the 2 class members added?
  1. Isn’t that considered offline instrumentation? Or is there a different term for it?

Yes it is.
And that is exactly why I said your question has nothing to do with the built-in JaCoCo plugin which does not support offline instrumentation and does not have the testCoverageEnabled property you mentioned.

  1. Is there a way to exclude certain class/package from having the 2 class members added?

Again, ask whoever does this offline instrumentation.
I would guess it probably is the Android Gradle Plugin which is a 3rd party plugin, but I’m not an Android developer, and I’m too lazy to google it for you right now. :slight_smile:

That’s quite confusing. Thanks for the clarification.

To sum it up for future reference:

  • Jacoco standalone does support offline instrumentation, but not the built-in Jacoco gradle plugin.
  • Android uses offline instrumentation but it’s provided by a different plugin, probably Android Gradle plugin.
1 Like