Skip to content
Devrath edited this page Jun 30, 2021 · 1 revision

Setting up of code coverage

Project level build.gradle

buildscript {
    ext.kotlin_version = "1.4.32"
    ext.jacocoVersion = '0.8.4' // ---- > Add here
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jacoco:org.jacoco.core:$jacocoVersion" // ---- > Add here
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

App level build.gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'jacoco' // --- > This line 
}

// ------------------------------------ > This block
jacoco {
    toolVersion = "$jacocoVersion"
}

tasks.withType(Test) {
    jacoco.includeNoLocationClasses = true
}

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

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

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

    sourceDirectories.from = files([mainSrc])
    classDirectories.from = files([debugTree])
    executionData.from = fileTree(dir: project.buildDir, includes: [
            'jacoco/testDebugUnitTest.exec', 'outputs/code_coverage/debugAndroidTest/connected/**/*.ec'
    ])
}
// ------------------------------------ > This block

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.code"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {

        debug{
            testCoverageEnabled true // --- > This line 
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    // ------------------------------------ > This block
    testOptions {
        animationsDisabled true

        unitTests {
            includeAndroidResources = true
        }
    }
    // ------------------------------------ > This block

}

dependencies {
// ---- > Dependencies
}

Clone this wiki locally