Classes in bundle 'app' do not match with execution data - Android

608 Views Asked by At

Although similar questions are already present on internet, but I am unable to find any solution to this problem with regards to android platform. My project has multiple product flavors, uses kotlin and hilt. I believe byte code transformation while compiling the project is the root cause of disrupture.

I first thought probably Hilt is injecting code inside classes, therefore I made sure to copy classes before Hilt tasks execution into a separate folder, then use those classes as source for jacoco. But it didn't work.

Error

[ant:jacocoReport] Classes in bundle 'app' do not match with execution data. For report generation the same class files must be used as at runtime.
[ant:jacocoReport] Execution data for class com/company/myapp/Data$callApi$1 does not match.
[ant:jacocoReport] Execution data for class com/company/myapp/factory/SomeFactory$SomeGenerator does not match.

and the list continues for whole bunch of classes in the app. Due to these errors, code coverage is always zero although there are bunch of unit test already written in the app.

gradle.projectsEvaluated {
    def hiltTaskPattern = ~/\bhilt.*\w+FlavorADebug\b/

    def tasksList = getSubprojects()
            .collect { it.tasks }
            .flatten()

    def copyFilesTask = tasksList.find { it.name == "copyClassFilesForJacoco" }

    if (copyFilesTask != null) {
        tasksList.findAll { hiltTaskPattern.matcher(it.name).matches() }
                .each { it.dependsOn copyFilesTask }
    }
}

task copyClassFilesForJacoco(dependsOn: "compileDebugJavaWithJavac", type: Copy) {

    def javaDebugTree = fileTree(dir: "${buildDir}/intermediates/javac/flavorADebug/classes", excludes: androidFilesExcluded)
    def kotlinDebugTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/flavorADebug", excludes: androidFilesExcluded)

    from javaDebugTree, kotlinDebugTree
    into layout.buildDirectory.dir(classFilesPathForInstrumentation)

    doLast {
        println("Files copied to ${classFilesPathForInstrumentation}")
    }
}

Then in the testCoverage task of type JacocoReport, classDirectories point out to copied files

Also, kotlinx-kover seemed interesting, but it's in pre-mature state and lacks support of multiple product flavors along with outdated documentation which makes its usage unfavourable.

This answer explains very well reason for the problem, and also provide potential solution. But it is old, and not applicable to android project since it uses java plugin and jacoco-ant agent which is not compatible with android.

Can someone guide towards potential solutions for the aforementioned problem? TIA

0

There are 0 best solutions below