Jacoco converting coverage.ec to reports without coverage.em
Asked Answered
S

4

4

I was able to get the code coverage report by following the steps below,

  1. Enable coverage on the build type you want (e.g. debug)

    buildTypes { debug { testCoverageEnabled true } }

  2. Apply Jacoco plugin and set version

    apply plugin: 'jacoco' jacoco { version "0.7.1.201405082137" }

  3. Run

    ./gradlew createDebugCoverageReport

  4. All the tests in connectedAndroidTest are run and coverage report is generated based on them. I can find the coverage reports in

    app/build/outputs/reports/coverage/{buildType}/index.html

and a coverage.ec file in

app/build/outputs/code-coverage/connected/coverage.ec

But no jacoco.exec since I am running from Android Instrumentation instead of Robolectric test cases.

And when I run the instrumentation from ADB ( I guess this is still using Emma ) as follows, I get a coverage.ec file as follows,

$ adb shell am instrument -w -e coverage true -e coverageFile /sdcard/coverage.ec com.sample.helloworld.test/.Runner
....
OK (4 tests)

Generated code coverage data to /sdcard/coverage.ec

But I am not able to convert the coverage.ec to report using emma since the coverage.em file is missing,

java -cp ~/adt-bundle-mac-x86_64-20130729/sdk/tools/lib/emma_device.jar emma report -r html -in \
coverage.em,myFile.ec,myapp_coverage1.ec -sp /path/to/myapp/src

Is there a way around this problem??

Stalinsk answered 13/3, 2015 at 18:45 Comment(0)
S
8

Simply use the "coverage.ec" as the ".exec" file, it's worked for me

This is what Google did in Android Gradle Plugin source code.

public static final String FILE_COVERAGE_EC = "coverage.ec";

in SimpleTestCallable.java under com.android.builder.internal.testing package.

Sheridansherie answered 29/9, 2015 at 19:14 Comment(7)
Wow, yes you can simply rename the file to coverage.exec and it becomes selectable in Android Studio (2.0) under Show Coverage Data and you get all your code nicely highlighted right in the editor window.Zomba
@NicolasGramlich I changed it to exec and opened it through Show Coverage Data - but it shows 0% for all the codeTheseus
@UrielFrankel I met the same problem, so have you resolved it ?Kroll
yes. I have. i dived into the code of the jacoco and build a jar. and after a lot of twiking it works.Theseus
@UrielFrankel Care to share with us what you did? A lot of people will be grateful for it, including me :)Tgroup
It is a very long and painful and full of black magic answer. So hold on, and I will do it.Theseus
blog.houzz.com/post/158160064293/…Theseus
T
4

I wrote an article about the same scenario and my solution. You can read it here. For answering this specific question you should do the following steps: Change gradle to this:

apply plugin: 'jacoco'
jacoco {
   toolVersion = '0.7.5.201505241946'
}

Second download this jar. Continue with the things you did until you have coverage.ec file. Then when you have it run:

java -jar android-jacoco-the-missing.jar -f /path/to/coverage.ec -p ./path/to/android/project

And thats it! The jar will generate a folder with the code coverage.

Another option is to use the coverage.ec with the Jenkins Jacoco plugin. But for that you need to have Jenkins set on.

Theseus answered 9/3, 2017 at 15:5 Comment(6)
Executing the jar always gives me FileNotFoundException. The file is there and I checked the path multiple times.Herstein
I am sure it is in the correct location. I checked it multiple times. Another thing I noticed is that the jar wants to sit in the project dir and get relative path to the file, which is not very flexible.Herstein
Run the jar without parameters and you will see the options you haveTheseus
Any chances to have this jar genereting xml reports ?Salivation
If you are using Jenkins, there is a plugin for that.Theseus
I can;t even get an ec file because the class not found excpetion happens before that gets generatedCotoneaster
Q
1

To get the coverage out of coverage.ec file just rename it to coverage.exec and the open it with "Show coverage data" option

"Show coverage data" can be found under "Analyze" option in Android Studio.

Verified this with Android Studio 3.3

Quintic answered 11/3, 2019 at 6:34 Comment(0)
E
0

Remove . from following command its work for me

java -jar android-jacoco-the-missing.jar -f /path/to/coverage.ec -p /path/to/android/project
Eager answered 14/6, 2017 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.