Cannot obtain expected Coverage report by Excluding classes and packages using Jacoco (Gradle) plugin in Android
Asked Answered
L

0

1

I'm trying to exclude some classes & packages which are based on HILT generated contents, few modules & all BindingImplementations from the JACOCO report. It does not return the expected results.

Details follow,

Kotlin Version -> 1.9.0, Gradle Version -> 8.1.1, Java Compatibility -> Java Version 17, Jacoco Tool Version -> 0.8.8

App Gradle

debug {
        testCoverageEnabled true
      }

implementation "org.jacoco:org.jacoco.core:0.8.8"

Jacoco.gradle file.

jacoco {
    toolVersion = "0.8.8"
}

project.afterEvaluate {
  android.applicationVariants.all { variant ->
    createVariantCoverage(variant)
  }
}

ext {
    excludedSource = [
        '**/databinding/*.*',
        '**/R.class',
        '**/R$*.class',
        '**/BuildConfig.*',
        '**/Manifest*.*',
        '**/*Test*.*',
        'android/**/*.*',
        '**/*$ViewInjector*.*',
        '**/*$ViewBinder*.*',
        '**/*_MembersInjector.class',
        '**/Dagger*Component*.*',
        '**/*Module_*Factory.class',
        '**/di/module/*',
        '**/*_Factory*.*',
        '**/*MapperImpl*.*',
        '**/*$ViewInjector*.*',
        '**/*$ViewBinder*.*',
        '**/BuildConfig.*',
        '**/*Component*.*',
        '**/*BR*.*',
        '**/Manifest*.*',
        '**/*$Lambda$*.*',
        '**/*Module*.*',
        '**/*Dagger*.*',
        '**/*Hilt*.*',
        '**/*MembersInjector*.*',
        '**/*_MembersInjector.class',
        '**/*_Factory*.*',
        '**/*_Provide*Factory*.*',
        '**/*Extensions*.*'
    ]
}

def createVariantCoverage(variant) {
    def variantName = variant.name
    def testTaskName = "test${variantName.capitalize()}UnitTest"


// Add unit test coverage tasks
tasks.create(name: "${testTaskName}Coverage", type: JacocoReport, dependsOn: "$testTaskName") {
    group = "Reporting"
    description = "Generate Jacoco coverage reports for the ${variantName.capitalize()} build."

      reports {
          html {
              destination file("${buildDir}/coverage-report")
          }
      }

      def javaClasses = fileTree(dir: variant.javaCompileProvider.get().destinationDir, excludes: excludedSource)

      def kotlinClasses = fileTree(dir: "${buildDir}/tmp/kotlin-classes/${variantName}", excludes: excludedSource)

      classDirectories.setFrom(files([javaClasses, kotlinClasses]))

      sourceDirectories.setFrom(files([
              "$project.projectDir/src/main/java"
      ]))

      executionData.setFrom(files("${project.buildDir}/outputs/unit_test_code_coverage/${variantName}UnitTest/${testTaskName}.exec"))

       doLast {
          def m = new File("${project.buildDir}/reports/jacoco/${testTaskName}Coverage/html/index.html")
                .text =~ /Total[^%]*>(\d?\d?\d?%)/
          if (m) {
              println "Test coverage: ${m[0][1]}"
          }
      }
   }

}
Lactase answered 20/11, 2023 at 9:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.