1

I run my tests with "gradlew createDebugCoverageReport". My problem is that the coverage report includes every single source file I have. I want to exclude some files. I added this to my build.gradle but it did not work:

sourceSets {
    androidTest {
        java
                {
                    exclude '**/TouchImageView.java'
                }
    }
}
Asker
  • 431
  • 5
  • 14
  • check this post: http://stackoverflow.com/questions/16800238/android-project-with-robolectric-and-gradle-android-studio, look he doesn't use java {} – piotrek1543 Jan 19 '16 at 21:35
  • If I do it this way I get the following error "Gradle DSL method not found: 'androidTest()'" – Asker Jan 19 '16 at 22:16

1 Answers1

4

You have to add the jacoco plugin at the begining of your build.gradle

apply plugin: 'jacoco'

Then enable the coverage with testCoverageEnabled true i.e

buildTypes {
        release {
            ...
        }
        debug {
            testCoverageEnabled true
        }
    }

And create the task jacocoTestReport:

task jacocoTestReport(type:JacocoReport, dependsOn: "connectedDebugAndroidTest") {

    group = "Reporting"

    description = "Generate Jacoco coverage reports"

    // exclude auto-generated classes and tests
    def fileFilter = ['**/R.class',
                      '**/R$*.class',
                      '**/BuildConfig.*',
                      '**/Manifest*.*',
                      '**/*IScript*.*',
                      'android/**/*.*',
                      '**/*_Factory*',
                      '**/*_MembersInjector*',
                      '**/*Fake*']

    def debugTree = fileTree(dir:
            "${project.buildDir}/intermediates/classes/debug",
            excludes: fileFilter)

    def mainSrc = "${project.projectDir}/src/main/java"

    sourceDirectories = files([mainSrc])
    classDirectories = files([debugTree])

    executionData = fileTree(dir: project.projectDir, includes:
            ['**/*.exec', '**/*.ec'])

    reports {
        xml.enabled = true
        xml.destination = "${buildDir}/jacocoTestReport.xml"
        csv.enabled = false
        html.enabled = true
        html.destination = "${buildDir}/reports/jacoco"
    }
}

Add your exclusions to fileFilter array. Then run the report:

$ gradle jacocoTestReport
mromer
  • 1,867
  • 1
  • 13
  • 18