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
<http://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

On Wed, 22 Jan 2020 at 07:42, Marc Hoffmann <[email protected]>
wrote:

> 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
>
>
>
> On 14. Jan 2020, at 19:22, [email protected] wrote:
>
> 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:
>>
>> https://www.jacoco.org/jacoco/trunk/doc/agent.html
>>
>> Regards,
>> -marc
>>
>> On 14. Jan 2020, at 01:05, [email protected] 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 [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/jacoco/142163e8-fe60-4a0a-8f41-e186ee1c51d8%40googlegroups.com
>> <https://groups.google.com/d/msgid/jacoco/142163e8-fe60-4a0a-8f41-e186ee1c51d8%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>>
>>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jacoco/5113efe9-9b9e-4ef3-b519-6ef6588820cf%40googlegroups.com
> <https://groups.google.com/d/msgid/jacoco/5113efe9-9b9e-4ef3-b519-6ef6588820cf%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jacoco/49B6654A-9B05-465A-8897-0A101DF33EA8%40mountainminds.com
> <https://groups.google.com/d/msgid/jacoco/49B6654A-9B05-465A-8897-0A101DF33EA8%40mountainminds.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jacoco/CABR9TNB%2BWiN3XChGekV22HATbJjhkb8Cu4_-AShCy2a28ZGwBg%40mail.gmail.com.

Reply via email to