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

JaCoCo使用Kotlin和Android 3.0返回0%的覆盖率,android,kotlin,code-coverage,jacoco,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: 'k

我试图检查我在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"
    ])
}

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

通过为生成的.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.from = files([debugTree], [kotlinDebugTree])

不仅是Jacoco,甚至Intellij IDEA的默认覆盖率也为0。当它是Kotlin时,一切都是0。这是根据stackoverflow.com/a/45354933/3286489报告的。但如果仔细观察,它报告的是测试代码的覆盖范围,而不是应用程序代码:(建议的解决方案应该有效。我不知道这篇文章,并以类似的方式修复了它。为此写了一篇博文:Thank@VeaceslavGaidarji!classDirectories已弃用。那么answer@charithaamarasinghe使用
classDirectories.from
而不是
classDirectories