12

I want to exclude some source files in Jacaco Test coverage report.For other generated code I have done like this:

classDirectories = fileTree(
            dir: "${project.buildDir}/intermediates/classes/debug/com",
            excludes: [
                    '**/R.class',
                    '**/R$*.class']
                    )  

But for excluding Java files when I am trying to do like this:

   dir: "${project.buildDir}/intermediates/classes/debug/com",
            excludes: [
                 'src//java/com/example/application/Constants.java'] 

have also tried like this: '**/application/Constants.class'.It doesn't work. Do I need to include the path here: dir: "${project.buildDir}/intermediates/classes/debug/com"?

I am using Android studio 3.0 (i don't think it matters here). Full code that I am trying:

task jacocoTestReport(type: JacocoReport) {
        group = "Reporting"
        description = "Generate Jacoco coverage reports"
        reports {
            xml.enabled = true
            html.enabled = true
        }

 sourceDirectories = files(sourceSets)

 classDirectories = fileTree(
            dir: "${project.buildDir}/intermediates/classes/debug/com",
            excludes: [
                    'src//java/com/example/application/Constants.java',     //this is not working
                    '**/R.class',
                    '**/R$*.class',
                    '**/BuildConfig.*',
                    '**/Manifest*.*',
                    '**/*$ViewInjector*.*',
                    '**/*$ViewBinder*.*',
                    '**/*$Lambda$*.*', // Jacoco can not handle several "$" in class name.
                    '**/*Module.*', // Modules for Dagger.
                    '**/*Dagger*.*', // Dagger auto-generated code.
                    '**/*MembersInjector*.*', // Dagger auto-generated code.
                    '**/*_Provide*Factory*.*',
                    '**/*_Factory.*', //Dagger auto-generated code
                    '**/*$*$*.*', // Anonymous classes generated by kotlin
                    //add libraries
                    'android/**/*.*',
                    'com/**/*.*',
                    'uk/**/*.*',
                    'io/**/*.*',
                    //remove what we don't test
                    'androidTest/**/*.*',
                    'test/**/*.*',
                    '**/injector/**/*.*',
                    '**/model/**/*.*',
                    '**/mock/**/*.*',
                    '**/event/**/*.*',
                    '**/**_ViewBinding**',
                    '**/*EventType.*',
                    '**/**Mocked'
            ]

    )
    executionData = fileTree(dir: 'build/jacoco', include: '**/*.exec')

}
Shubham
  • 2,627
  • 3
  • 20
  • 36
  • Hi, you tried using class file like this: `'**/application/Constant.class'`, but your source file is named `Constants.java`. So did you also try without this typo? e.g. `'**/Constants.class'` ? – Mayoares Dec 19 '17 at 13:33
  • yes I tried without the typo as well – Shubham Dec 20 '17 at 12:14

3 Answers3

5

In my project, config like this:

//exclude the folders we do not want to check
jacocoTestReport {
    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it, exclude: [
                    '**/enum/**',
                    '**/util/**',
            ])
        })
    }
}
davis dai
  • 129
  • 3
3

classDirectories of task of type JacocoReport is about class files, not java source files. And I'm pretty sure that your directory

 classDirectories = fileTree(
            dir: "${project.buildDir}/intermediates/classes/debug/com",

contains class files and does not contain java files. Also as you can see other patterns are about class files:

                '**/R.class',
                '**/R$*.class',

And that's why your exclusion

           'src//java/com/example/application/Constants.java',     //this is not working

doesn't work. So change it to match existing class file in given directory.

Also note that you can easily debug result of fileTree(...) call by simply printing it:

tree.each {File file ->
    println file
}
Godin
  • 9,801
  • 2
  • 39
  • 76
  • thanks for your answer, I even tried using class file like this: '**/application/Constant.class'. What does ** refer to. As per my understanding, it refers to anything inside package. This doen't seem to work for me. – Shubham Dec 08 '17 at 10:28
  • @Shubham I already explained how you can debug result of `fileTree`, so please debug it. And IMO without **[Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve)** from you that **fully demonstrates** your problem, there is nothing more to say that could help you, especially that similar configuration works fine for others. – Godin Dec 08 '17 at 10:34
  • @Godin How to do this on a source set since someone may plugin generated code plugins adding to the source sets? – Dean Hiller Jun 07 '20 at 13:29
0

For gradle 6+ you configure it like below:

jacocoTestCoverageVerification {
    violationRules {
        rule {
            includes = ['com/myapp/*']
            excludes = [
                    'com/myapp/folderToExclude1/*',
                    'com/myapp/folderToExclude2/*',
            ]
           limit {
                minimum = 0.85
            }
        }
    }
}
Celt
  • 2,469
  • 2
  • 26
  • 43