3

I want to get line coverage in my source files for my test coverage results. The test results in the generated table have columns for missed lines, and even for missed control flows. According to stuff I've read, this means if the source files are found, I will be able to get line coverage displayed in my source files.

Currently, I have unit test results and android test results in separate reports. The android test results say "Source file "com/example/helloworld/KeyboardUtil.kt" was not found during generation of report."

This is the relevant part of the gradle.

jacoco {
    toolVersion = jacoco_version
}

tasks.withType(Test) {
    jacoco.includeNoLocationClasses = true
    jacoco.excludes = ['jdk.internal.*']
}

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

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

    def fileFilter = ['android/databinding/**/*.class',
                      '**/android/databinding/*Binding.class',
                      '**/android/databinding/*',
                      '**/androidx/databinding/*',
                      '**/BR.*',
                      '**/R.class',
                      '**/R$*.class',
                      '**/BuildConfig.*',
                      '**/Manifest*.*',
                      '**/*Test*.*',
                      'android/**/*.*',
                      '**/*$ViewInjector*.*',
                      '**/*$ViewBinder*.*',
                      '**/*_MembersInjector.class',
                      '**/Dagger*Component.class',
                      '**/Dagger*Component$Builder.class',
                      '**/*Module_*Factory.class',
                      '**/di/module/*',
                      '**/*_Factory*.*',
                      '**/*Module*.*',
                      '**/*Dagger*.*',
                      '**/*Hilt*.*',
                      '**/*MapperImpl*.*',
                      '**/*$ViewInjector*.*',
                      '**/*$ViewBinder*.*',
                      '**/BuildConfig.*',
                      '**/*Component*.*',
                      '**/*BR*.*',
                      '**/Manifest*.*',
                      '**/*$Lambda$*.*',
                      '**/*Companion*.*',
                      '**/*Module*.*',
                      '**/*Dagger*.*',
                      '**/*Hilt*.*',
                      '**/*MembersInjector*.*',
                      '**/*_MembersInjector.class',
                      '**/*_Factory*.*',
                      '**/*_Provide*Factory*.*',
                      '**/*Extensions*.*',
                      '**/*$Result.*',
                      '**/*$Result$*.*']

    def debugTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/debug", excludes: fileFilter)
    def mainSrc = fileTree(dir: "${projectDir}/src/main/java", excludes: fileFilter)

    getSourceDirectories().setFrom(files([mainSrc]))
    getClassDirectories().setFrom(files([debugTree]))

    def unitTestsData = "$project.buildDir/jacoco/testDebugUnitTest.exec"
    def androidTestsData = fileTree(dir: "${buildDir}/outputs/code_coverage/debugAndroidTest/connected/",
            includes: ["**/*.ec"])
    getExecutionData().setFrom(files([unitTestsData, androidTestsData]))

}

I believe the problem to be these lines:

    def debugTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/debug", excludes: fileFilter)
    def mainSrc = fileTree(dir: "${projectDir}/src/main/java", excludes: fileFilter)

    getSourceDirectories().setFrom(files([mainSrc]))
    getClassDirectories().setFrom(files([debugTree]))

The main src directory is correct, but in the report, the path to the result is "app > com.example.helloworld > KeyboardUtil" when it should be "app > src > main > java > com.example.helloworld > KeyboardUtil"

Not sure how to fix it.

John Glen
  • 771
  • 7
  • 24
  • Related: https://stackoverflow.com/questions/53829414/jacoco-not-generating-coverage-reports-based-on-source-file-method-names-not-c – Mr-IDE May 23 '21 at 17:50
  • Exact same problem here. Any solutions? It seems that the report that IS generated is for the `AndroidTest` directory, not the `test` directory (as my unit tests only cover basic java code and not android native code). – SMBiggs Sep 22 '22 at 12:13
  • Ah, I did end up fixing it. See here: https://stackoverflow.com/questions/38249722/gradle-createdebugcoveragereport-doesnt-run-local-unit-tests/72456273#72456273 – John Glen Sep 23 '22 at 23:20

0 Answers0