I\'m trying to make jacoco create a code coverage report for my android test project. I have the following in build.gradle:
apply plugin: \'com.android.appli
I use same gradle.build and just enter the command below to terminal. It gave me coverage report and test result as a html page.
$./gradlew createDebugCoverageReport
Another suggestion is ; enter terminal
$./gradlew --gui
it opens a gui window and you can find the correct command.
I found a solution to same problem from Jacoco code coverage in Android Studio with flavors. It has example what to do in case coverage.ec is generated instead of testDebug.exec
.
I created the jacocoTestReport
task.
I executed $./gradlew createDebugCoverageReport
which generates the coverage.ec
file
$./gradlew jacocoTestReport
and that creates the HTML.Now (Oct 2015) you can use it as android team has fixed the bug.
android {
...
buildTypes {
debug {
testCoverageEnabled true
}
}
...
dependencies{
androidTestCompile 'com.android.support.test:runner:0.4.1'
// Set this dependency to use JUnit 4 rules
androidTestCompile 'com.android.support.test:rules:0.4.1'
// Set this dependency to build and run Espresso tests
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
// Espresso-contrib for DatePicker, RecyclerView, Drawer actions, Accessibility checks, CountingIdlingResource
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'
}
}
Then just use ./gradlew createDebugCoverageReport
.
I made an interesting observation. I had the same problem, with the coverage.ec
File having a size of 0 bytes. I tried all the answers here as well as these ones:
https://code.google.com/p/android/issues/detail?id=170607
It didn't change anything. My build.gradle
file has only these settings to let jacoco work:
apply plugin: 'com.android.application'
apply plugin: 'jacoco'
android {
...
buildTypes {
...
debug {
testCoverageEnabled true
}
}
...
}
My Android Studio Version is 1.2.2. I have the following gradle settings in "File -> Project Structure":
I tried to run the instrumentation tests with gradlew cC
on an Motorola Moto G 4G LTE - Android 4.4.4
with correctly ran the instrumentation tests but with a coverage.ec
file having 0 bytes. Today I tried another device: LG Spirit 4G LTE - Android 5.0
and now code coverage works!!! The coverage.ec
file has 322 bytes and I see correct html coverage reports.
What I think is that sometimes running gradle with gradlew cC --debug
shows that coverage.ec
cannot be found on the device and sometimes it shows the file being pulled. But maybe in both situations it cannot be found on the device. So choosing another device can fix the problem. Good luck!
Try to generate the debug coverage report using Nexus device
(5 or 5x), it will works perfectly. I was facing same issue with Samsung devices
(coverage.ec is empty) but after that I run it with Nexus 5x
and everything was working fine.
There's a problem in the Android Gradle Plugin 0.14.0 that generates an empty coverage.ec file. It's known and was fixed in a branch but doesn't seem to have made it out to the public yet: https://code.google.com/p/android/issues/detail?id=78556
On the other hand, a lovely workaround template project for Android Studio can be found here using Jacoco and RoboElectric: https://github.com/nenick/android-gradle-template
So you can wait for Google to fix it or use someone else's build library repository, but Android Studio 1.0.0's gradle plugin is useless.