Skip to content

jitsm555/JacocoAndroidSample

Repository files navigation

JacocoAndroidSample

To generate the coverage report in Android, we use Jacoco (Java Code Coverage), one of the most used tools in Java for this purpose.

Introduction

Test coverage reports are an important tool to measure how much our tests actually exercise our code.

build.gradle

Enabled test coverage in app/build.gradle

android {
    ...
    buildTypes {
        debug {
            testCoverageEnabled true
        }
        ...
    }
}

Create task for jacoco report

Create jacocoTestReport in app/build.gradle

apply plugin: 'jacoco'

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

    reports {
        xml.enabled = true
        html.enabled = true
    }

    def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
    def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
    def mainSrc = "${project.projectDir}/src/main/java"

    sourceDirectories = files([mainSrc])
    classDirectories = files([debugTree])
    executionData = files("${buildDir}/jacoco/testDebugUnitTest.exec")
}

Jacoco Report Path

Generated jacoco report path after successful jacocoTestReport execution.

app/build/reports/coverage/debug/index.html

Gradle command for jacoco report

./gradlew clean jacocoTestReport

Pre-requisites(Optional)

  • Android SDK 26
  • Android Build Tools v26.0.1
  • Android Support Repository

Screenshots

Report Path

Java Classes

Coverage

Resources

Check out the following links for more details:

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.

Releases

No releases published

Packages

No packages published

Languages