Is there any document/blogpost describing the step by step process of JaCoCo offline instrumentation in Android

68 views
Skip to first unread message

tester...@gmail.com

unread,
Jan 13, 2020, 7:05:34 PM1/13/20
to JaCoCo and EclEmma Users
Dear All,

I am trying to use JaCoCo to measure the coverage achieved by an automated testing tool that uses appium and python. 
I checked the JaCoCo official webpage for offline instrumentation but didn't understand the process properly. I would be really grateful if someone can give a step by step procedure to do the JaCoCo offline instrumentation in Android. 
A blog post on this topic would be really helpful to the new users.

Thank you!


Marc Hoffmann

unread,
Jan 14, 2020, 12:20:26 AM1/14/20
to JaCoCo and EclEmma Users
Hi,

why do you think you need offline instrumentation? Using the JaCoCo Java agent is very convenient and documented here:


Regards,
-marc

--
You received this message because you are subscribed to the Google Groups "JaCoCo and EclEmma Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jacoco+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/142163e8-fe60-4a0a-8f41-e186ee1c51d8%40googlegroups.com.

tester...@gmail.com

unread,
Jan 14, 2020, 1:23:00 PM1/14/20
to JaCoCo and EclEmma Users
The testing tool only uses the android apk, not the source code. Can I still use the Java agent? If so, how do I instrument the apk using Java agent? 
Is there any sample project available that used java agent to instrument the apk? I am really sorry if I am asking dumb questions.

Regards,
Sam  


On Monday, January 13, 2020 at 11:20:26 PM UTC-6, Marc R. Hoffmann wrote:
Hi,

why do you think you need offline instrumentation? Using the JaCoCo Java agent is very convenient and documented here:


Regards,
-marc

On 14. Jan 2020, at 01:05, tester...@gmail.com wrote:

Dear All,

I am trying to use JaCoCo to measure the coverage achieved by an automated testing tool that uses appium and python. 
I checked the JaCoCo official webpage for offline instrumentation but didn't understand the process properly. I would be really grateful if someone can give a step by step procedure to do the JaCoCo offline instrumentation in Android. 
A blog post on this topic would be really helpful to the new users.

Thank you!



--
You received this message because you are subscribed to the Google Groups "JaCoCo and EclEmma Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jac...@googlegroups.com.

Marc Hoffmann

unread,
Jan 22, 2020, 1:42:30 AM1/22/20
to JaCoCo and EclEmma Users
Hi Sam,

AFAIK apk archive already contain dex files (not class files). JaCoCo only works in class files.

I think indeed with offline instrumentation it is possible to instrument class files before dex files and the apk are created. As I’m not a Android developer I can’t tell you details about the setup. A quick google search results in several blogs from people who use JaCoCo for Android.

Regards,
-marc



To unsubscribe from this group and stop receiving emails from it, send an email to jacoco+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/5113efe9-9b9e-4ef3-b519-6ef6588820cf%40googlegroups.com.

Rolf Smit

unread,
Jan 22, 2020, 1:58:40 AM1/22/20
to jac...@googlegroups.com
Jacoco support is build in into the Android tooling (Android Gradle Plugin). Android supports two different types of tests: instrumented unit-tests (on device, phone) and non-instrumented unit-tests (on development machine/build-server etc.).

The documentation on instrumented tests cleary states that you need to use offline instrumentation, I quote:

To generate code coverage files (*.ec) that can be used by EMMA or JaCoCo: -e coverage true Note: For this to work, your classes have to be instrumented offline (i.e. at build time) by EMMA/JaCoCo. By default, the code coverage results file will be saved in a /data/data//files/coverage.ec file, unless overridden by coverageFile flag (see below)

Source: https://developer.android.com/reference/androidx/test/runner/AndroidJUnitRunner

To enable code coverage (jacoco) in an Android project just enable `testCoverageEnabled` in the Android module build.gradle file:

android {
    buildTypes {
        debug {
            testCoverageEnabled true
        }
    }
}

This wil make sure .exec files are generated (for instrumented tests .ec files). Additional configuration might be required, for example if you use Robolectric you will very likely need something like this:

tasks.withType(Test) {
    jacoco.includeNoLocationClasses = true
}

However instead of doing allot of configuration yourself I highly recomend to use a plugin that takes care of most of the configuration (multi-module coverage or Kotlin support can be quite a pain to setup). A plugin that does this can be found here (note: I'm the author of this plugin): https://github.com/NeoTech-Software/Android-Root-Coverage-Plugin

Regards,

Rolf Smit

Reply all
Reply to author
Forward
0 new messages