user1365169 user1365169 - 1 year ago 220
Android Question

Jacoco Code Coverage in android studio

I am trying to generate Jacoco code coverage report.
I have used AndroidTestCase for my test classes.

I have found using testCoverageEnabled true and using default android -studio default jacoco,
./gradlew connectedCheck or createDebugCoverageReport
create the percentage of successfull/fail test cases, but no coverage report.

Then I have tried jacoco {toolVersion ""}, and task jacocoTestReport(type:JacocoReport, dependsOn: "testDebug"). I have tried to change the dependsOn value with various task. The report shows 0 (zero) test coverage, which is impossible because at least half of all classes are tested.

I have followed various accepted answer of stack overflow in last couple of days. The result is negative.

My gradle file:

buildscript {
repositories {
dependencies {
classpath ''

apply plugin: ''
apply plugin: 'jacoco'

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
applicationId ""
minSdkVersion 21
targetSdkVersion 21
versionCode 1
versionName "1.0"

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(
'proguard-android.txt'), ''
debug {
testCoverageEnabled true

jacoco {
version ""

packagingOptions {
exclude 'LICENSE.txt'

jacoco {
toolVersion ""

task jacocoTestReport(type:JacocoReport, dependsOn: "testDebug") {
group = "Reporting"

description = "Generate Jacoco coverage reports"

// exclude auto-generated classes and tests
def fileFilter = ['**/R.class', '**/R$*.class',
'**/BuildConfig.*', '**/Manifest*.*',
def debugTree = fileTree(dir:
excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"

sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
additionalSourceDirs = files([
executionData = fileTree(dir: project.projectDir, includes:
['**/*.exec', '**/*.ec'])

reports {
xml.enabled = true
xml.destination = "${buildDir}/jacocoTestReport.xml"
csv.enabled = false
html.enabled = true
html.destination = "${buildDir}/reports/jacoco"

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('junit:junit:4.11') {
exclude module: 'hamcrest-core'

Answer Source

I see that you already got it working, however, there's a simpler method for getting Unit Test execution data. I recently was looking into this as well, I actually made a full write up earlier today.

In my situation, I didn't want to create an additional Gradle task as I wanted the report to be generated as a part of the existing workflow. I also didn't want to explicitly add the Jacoco plugin, as Google already dups the Jacoco Ant tasks for the coverage reports for Instrumentation Tests.

In addition to setting the properties android.jacoco.version and buildTypes.debug.testCoverageEnabled, I added the following to the testDebug JVM arguments to generate execution data:

project.afterEvaluate {
  def append = "append=true"
  def destFile = "destfile=$buildDir/outputs/code-coverage/connected/"
  testDebug.jvmArgs "-javaagent:$buildDir/intermediates/jacoco/jacocoagent.jar=$append,$destFile"

  createDebugCoverageReport.dependsOn testDebug

This appends the Unit Test execution data to the coverage file generated by connectedAndroidTest, so your report reflects both Instrumentation Tests and Unit Tests, rather than each variant individually.

Note that connectedAndroidTest overwrites the coverage file, take this into account when creating your report. If the task testDebug doesn't have any changes, and you run createDebugCoverageReport, it will only reflect your Instrumentation Test coverage. So, make a change to your Unit Tests. The Linux command touch may be useful here, although I haven't tried yet.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download