0

On my android project jacoco doesn't include the robolectic tests. I can get the android espresso and junit test coverage with jacoco without any issues.

I did see other questions about this issue and all the answers is to upgrade jacoco version. I'm using the latest jacoco version 0.7.9

This is my project main build.gradle

buildscript {

  dependencies {
    classpath 'org.jacoco:org.jacoco.core:0.7.9'

    classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6-rc1'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
   }
}

App module build gradle.

apply plugin: 'jacoco'

android {
    testOptions {
      unitTests.all {
        jacoco {
           includeNoLocationClasses = true
        }
        includeAndroidResources = true
       }
     }
  }
Libin
  • 16,967
  • 7
  • 61
  • 83

1 Answers1

1

I solve that problem with creating a separate task for jacoco in gradle.

First off all you need to add jacoco plugin.

apply plugin: "jacoco"

I didn't add any dependencies as you do on the code snippet above. Just add plugin.

Then add testCoverageEnabled true param to buildTypes section.

buildTypes {
    debug {
        testCoverageEnabled true
    }
}

In this example it is in just for debug, but I believe if you add it for release it should also work.

Lastly add jacoco task like below;

task jacocoTestReport(type: JacocoReport, dependsOn: "testDebugUnitTest") {

    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    reports {
        xml.enabled = true
        html.enabled = true
    }
    classDirectories = fileTree(
            dir: './build/intermediates/classes/debug',
            excludes: ['**/R*.class',
                       '**/*$InjectAdapter.class',
                       '**/*$ModuleAdapter.class',
                       '**/*$ViewInjector*.class'
            ])
    sourceDirectories = files(coverageSourceDirs)
    executionData = files("$buildDir/jacoco/testDebug.exec")
    doFirst {
        new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
            if (file.name.contains('$$')) {
                file.renameTo(file.path.replace('$$', '$'))
            }
        }
    }
}

Using that task you should be able to create coverage reports. It will export html formatted coverage report in build folder. For more information you can look at this tutorial.

emin deniz
  • 439
  • 5
  • 13