Exclude databinding package from jacoco report

Viewed 783

In my jacoco report I see some weird databinding package. enter image description here

I tried adding following to my build.gradle to exclude it:

    testOptions {
        unitTests.all {
            jacoco {
                includeNoLocationClasses = true
                afterEvaluate {
                    classDirectories.setFrom(files(classDirectories.files.collect {
                        fileTree(dir: it, exclude: '**/*databinding/**/*.*')
                    }))
                }
                classDirectories = fileTree(
                        dir: "${project.buildDir}",
                        excludes: ['**/*databinding/**/*.*']
                )
            }
        }
    }

It seems that they don't work. Is there a way to remove this package/folder from my report?

2 Answers

you can define much extensive filefilter

def fileFilter = [
                        // data binding
                        'android/databinding/**/*.class',
                        '**/android/databinding/*Binding.class',
                        '**/android/databinding/*',
                        '**/androidx/databinding/*',
                        '**/BR.*',
                        // android
                        '**/R.class',
                        '**/R$*.class',
                        '**/BuildConfig.*',
                        '**/Manifest*.*',
                        '**/*Test*.*',
                        'android/**/*.*',
                        // kotlin
                        '**/*MapperImpl*.*',
                        '**/*$ViewInjector*.*',
                        '**/*$ViewBinder*.*',
                        '**/BuildConfig.*',
                        '**/*Component*.*',
                        '**/*BR*.*',
                        '**/Manifest*.*',
                        '**/*$Lambda$*.*',
                        '**/*Companion*.*',
                        '**/*Module*.*',
                        '**/*Dagger*.*',
                        '**/*Hilt*.*',
                        '**/*MembersInjector*.*',
                        '**/*_MembersInjector.class',
                        '**/*_Factory*.*',
                        '**/*_Provide*Factory*.*',
                        '**/*Extensions*.*',
                        // sealed and data classes
                        '**/*$Result.*',
                        '**/*$Result$*.*',
                        // adapters generated by moshi
                        '**/*JsonAdapter.*',
                ]

def javaTree = fileTree(dir: "${project.buildDir}/intermediates/javac/$sourceName/classes", excludes: fileFilter)
def kotlinTree = fileTree(dir: "${project.buildDir}/tmp/kotlin-classes/$sourceName", excludes: fileFilter)
classDirectories.from = files([javaTree], [kotlinTree])

reference - https://medium.com/nerd-for-tech/setup-jacoco-code-coverage-with-your-multimodule-android-app-kotlin-a0f82573a1

Your exclusion is almost correct, here is the right one:

'**/databinding/*'

This is because the databinding build files reside in

app\build\intermediates\javac\[buildVariantName]\classes\[your\package\name]\databinding

I can't seem to find the docs right now, but i think your exclusion databinding/**/*.* is pointing to any file in a subdirectory of databinding and not to the files in the directory itself

Related