AndroidとかiOSとかモバイル多め。その他技術的なことも書いていきます。

kotlinのファイルをjacocoのコードカバレッジ対象に入れる

久し振りにjacocoの結果みたらあれ・・・なんか結果がおかしいなとなった。 よく見てみるとkotlinに変換したファイル達がコードカバレッジ対象から外れていた・・・

phicdy.hatenablog.com

以下で対応できた

  • sourceDirectoriessrc/main/kotlin を入れる(全部 src/main/java にあるから必要ないかも)
  • classDirectories$buildDir/tmp/kotlin-classes/<product flavor>Debug を追加

build.gradle

def coverageSourceDirs = ['src/main/java', 'src/main/kotlin']
// A list of files which should be excluded from coverage report since they are generated and/or framework code
def coverageExcludeFiles = [...]
task jacocoTestReport(type: JacocoReport, dependsOn: ['testUiTestDebugUnitTest']) {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    reports {
        xml.enabled true
        html.enabled true
        csv.enabled false
        xml.destination new File("${buildDir}/reports/jacoco/jacocoTestReport.xml")
        html.destination new File("${buildDir}/reports/jacoco/html")
        classDirectories =
                fileTree(dir: "${buildDir}/intermediates/classes/uiTest/debug",
                        exclude: coverageExcludeFiles) +
                fileTree(dir: "$buildDir/tmp/kotlin-classes/uiTestDebug",
                        excludes: coverageExcludeFiles)
    }
    sourceDirectories = files(coverageSourceDirs)
    executionData = files "${buildDir}/jacoco/testUiTestDebugUnitTest.exec"

    doLast {
        println "jacoco xml report has been generated to file://${buildDir}/reports/jacoco/jacocoTestReport.xml"
        println "jacoco html report has been generated to file://${reports.html.destination}/index.html"
    }
}

jacoco: Add kotlin classes to jacoco coverage by phicdy · Pull Request #69 · phicdy/MyCuration · GitHub

参考

vgaidarji.me