JaCoCo - generate coverage report for manual tests of Android app

Hello,
I need help setting up JaCoCo to generate coverage report for manual tests of Android app (multimodule project). I´ve used AI for setup, but it did not bring any valid results. So far I was able to write task for generating report and run it without error, but it did not generate anything.

Here is what I do:

  1. configure testCoverageEnabled true for each subproject by
    subproject.afterEvaluate { if (subproject.plugins.hasPlugin('com.android.application') || subproject.plugins.hasPlugin('com.android.library')) { subproject.android.buildTypes.debug.testCoverageEnabled = true } }
    I´ve tried manual configuration of some subprojects as well, did not get different result

  2. Created task for generating report:

    apply plugin: ‘jacoco’
    
    tasks.register(‘generateCombinedCoverageReport’, JacocoReport) {
    
    def coverageFile = file("D:\\Stažené\\coverage.ec")
    executionData.setFrom(files(coverageFile))
    
    def modules = [
            project(':app'),
            project(':qrScan'),
    ]
    
    def sourceDirs = []
    def classDirs = []
    
    modules.each { module ->
        sourceDirs += files('../app/src/main/java')
        classDirs += fileTree(
                dir: "${module.buildDir}/intermediates/javac/debug/compileDebugJavaWithJavac/classes",
                excludes: [
                        '**/BR.class',
                        '**/androidx*',
                        '**/dagger*',
                        '**/hilt*',
                ])
    }
    
    sourceDirectories.setFrom(files(sourceDirs))
    classDirectories.setFrom(files(classDirs))
    
    reports {
        xml.required.set(false)
        html.required.set(true)
        html.outputLocation.file("D:\\Stažené\\report.html")
    }
    
    doFirst {
        println "Coverage file exists: ${coverageFile.exists()}"
        println "Included modules: ${modules*.name}"
        println "Source dirs: ${sourceDirs}"
        println "Class dirs: ${classDirs}"
    
        new File("$buildDir/intermediates/javac/debug/compileDebugJavaWithJavac/classes").eachFileRecurse { file ->
            if (file.name.contains('$$')) {
                file.renameTo(file.path.replace('$$', '$'))
            }
        }
    
    }
    

    I´ve tried also with different report locations (even default ones)

  3. Then I run “./gradlew clean assembleDebug

  4. Then I install the app using “adb install -r .\app\build\outputs\apk\debug\debug.apk

  5. Then I do the manual tests targeting mainly code from the modules explicitly mentioned in task that generates the report

  6. Then I use “adb shell am instrument -w -e coverage true -e coverageFile /data/data/com.nn.myapp.debug/files/coverage.ec com.nn.myapp.debug/androidx.test.runner.AndroidJUnitRunner” which generates .ec report. This report has usually about 300-400kb (depending on how much of manual testing I do)

  7. I download the report to the path specified in task for generating report

  8. I launch the task for generating report, but I get no report

I was also trying to specify test class in point 6 to “run” empty test method, but it did not work. “am instrument” said that there is no such class. According to AI it does not matter because coverage data are supposed to be gathered since app starts.

Please help me figure out whats the problem and get me my report. Thanks!

Noone can help me with my issue?

The problem is, that you may ask at the wrong location.

From the Gradle side is a bit hard to reason due to lots and lots of very bad practice in that snippet, could well be that there is the reason, but probably not if you verified with your println debugging.

Ignoring the fact that this an Android build as Android builds are always special, but you say you get the coverage data file successfully so Android per-se might be out of the loop.

So what remains is that JaCoCo is not doing what you expect it to do.
Maybe it would make more sense to seek for help in a JaCoCo community. :man_shrugging:

Otherwise it might make sense to knit your problem into an MCVE.
Should be rather trivial as you already just try to create a report on a file you produced manually.
So check that in together with the other files needed by the report and provide it, then maybe someone can see what the problem might be. :man_shrugging:

1 Like

Thanks, will close the ticket then and will ask on different places

1 Like