JaCoCo returning 0% Coverage with Kotlin and Android 3.0
Asked Answered
M

3

39

I am trying to check my code coverage for a test case that I wrote in Kotlin. When I execute ./gradlew createDebugCoverageReport --info, my coverage.ec file is empty and my reports indicate that I have 0% coverage. Please note, the test cases are 100% successful. Can anyone think of any reasons my coverage.ec file keeps returning 0 bytes?

I have searched everywhere with no luck.

apply plugin: 'com.android.library'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'jacoco'


android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

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


    testOptions {
        unitTests.all {
            jacoco {
                includeNoLocationClasses = true
            }
        }
    }

}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:25.4.0'
    testImplementation 'junit:junit:4.12'
    implementation files('pathtosomejarfile')

}



jacoco {
    toolVersion = "0.7.6.201602180812"
    reportsDir = file("$buildDir/customJacocoReportDir")

}



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: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
    def mainSrc = "${project.projectDir}/src/androidTest/java"

    sourceDirectories = files([mainSrc])
    classDirectories = files([debugTree])
    executionData = fileTree(dir: "$buildDir", includes: [
            "jacoco/testDebugUnitTest.exec",
            "outputs/code-coverage/connected/*coverage.ec"
    ])
}
Maintopmast answered 2/8, 2017 at 14:49 Comment(2)
#45351061Entry
Not just Jacoco, even the default Coverage from Intellij IDEA is also 0. Everything is 0 when it is Kotlin. #45820200Sanguinary
U
29

You can get line-by-line coverage for both Java and Kotlin code by defining the two different directories for generated .class files:

def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
def kotlinDebugTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/debug", excludes: fileFilter)

Then, simply include both fileTrees in your classDirectories:

classDirectories.from = files([debugTree], [kotlinDebugTree])
Upbringing answered 6/9, 2017 at 19:36 Comment(6)
This is as per reported in https://mcmap.net/q/410064/-android-studio-3-kotlin-code-coverage. But if observe carefully, it is reporting the coverage of the test code, not the app code. :(Sanguinary
Proposed solution should work. I wasn't aware of this post, and fixed it in a similar way. Wrote a blog post about that: vgaidarji.me/blog/2017/12/20/…Lytle
Thanks @VeaceslavGaidarji!Foretoken
classDirectories is deprecated. so what should be the answerLaellaertes
@charithaamarasinghe Use classDirectories.from instead of classDirectoriesUpbringing
it would be better if someone can direct me where to use this codeYellowwood
M
0

After a few days, I figured out a solution to this problem. For those of you experiencing similar issues: In your intermediates folder there should be a tmp folder. This folder contains the .class files for the Kotlin files. If you change the path of fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter) to where those class files are located, Jacoco will generate code coverage for you! Please note that you will not be able to see a full, line-by-line overview of your coverage using this method.

Maintopmast answered 1/9, 2017 at 19:24 Comment(1)
but why? Does't it work with -> {buildDir}/tmp/kotlin-classes/universalDebugAndroidTest/ path?Pendergast
A
0

Try using the new kotlinx-kover Gradle plugin compatible with JaCoCo and IntelliJ.
It resolves the problem with inline functions reported as 0% and maybe more.

plugins {
     id("org.jetbrains.kotlinx.kover") version "0.5.0"
}

Once applied, the plugin can be used out of the box without additional configuration.

Watch its YouTube announcement video and also track its roadmap from this youtrack issue.

Alcove answered 6/11, 2021 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.