JaCoCo使用Kotlin和Android 3.0返回0%的覆盖范围

时间:2017-08-02 14:49:59

标签: android kotlin code-coverage jacoco

我正在尝试检查我在Kotlin中编写的测试用例的代码覆盖率。当我执行./gradlew createDebugCoverageReport --info时,我的coverage.ec文件为空,我的报告显示我的覆盖率为0%。请注意,测试用例100%成功。任何人都可以想到我的coverage.ec文件不断返回0字节的原因吗?

我到处搜寻都没有运气。

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"
    ])
}

2 个答案:

答案 0 :(得分:25)

通过为生成的.class文件定义两个不同的目录,您可以获得Java和Kotlin代码的逐行覆盖:

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

然后,只需在classDirectories中包含两个fileTrees:

classDirectories = files([debugTree], [kotlinDebugTree])

答案 1 :(得分:0)

几天后,我找到了解决这个问题的方法。对于那些遇到类似问题的人: 在您的intermediates文件夹中应该有一个tmp文件夹。该文件夹包含Kotlin文件的.class文件。如果您将fileTree(dir:“$ {buildDir} / intermediates / classes / debug”的路径(不包括:fileFilter)更改为这些类文件所在的位置,Jacoco将为您生成代码覆盖率!请注意,使用此方法,您将无法查看完整的逐行概述。