Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude Fragment from Jacoco code coverage

I have used below code to exclude fragment but I unable to exlcude fragment from jacoco code coverage kindly help me on this.

task jacocoTestReport(type: JacocoReport) {

def coverageSourceDirs = [
        'src/main/java'
]

group = "Reporting"
description = "Generates Jacoco coverage reports"
reports {
    xml {
        enabled = true
        destination "${buildDir}/reports/jacoco/jacoco.xml"
    }
    csv.enabled false
    html {
        enabled true
        destination "${buildDir}/jacocoHtml"
    }
}

classDirectories = fileTree(
        dir: 'build/intermediates/classes/debug',
        excludes: ['**/R.class',
                   '**/R$*.class',
                   '**/BuildConfig.*',
                   '**/Manifest*.*',
                   '**/*Fragment*.*'
        ]
)

sourceDirectories = files(coverageSourceDirs)
additionalSourceDirs = files(coverageSourceDirs)
executionData = files('build/outputs/code - coverage/connected/flavors/smartcompanion/coverage.ec')
}

We used command below :-

gradlew connectedCheck
gradlew connectedAndroidTest
gradlew connectedDubugAndroidTest

Everytime in coverage report we able to see fragment.

like image 898
Anshul Agarwal Avatar asked Dec 04 '25 22:12

Anshul Agarwal


1 Answers

There is another way to exclude files, you can filter them out:

classDirectories = fileTree(
    dir: 'build/intermediates/classes/debug',
    excludes: ['**/R.class',
               '**/R$*.class',
               '**/BuildConfig.*',
               '**/Manifest*.*',
               '**/*Fragment*.*'
    ]
).filter ({file -> !file.name.contains('Fragment')})

For some reason I was also unable to exclude fragments. I couldn't find out what went wrong. Filtering them out by name worked though. So it kinda feels like a workaround, but at least I could move on and get my work done.

I also noticed that on Android, the different versions of Gradle and the jacoco plugin are not always very compatible. So it might help to experiment with downgrading (or upgrading) too.

like image 78
michpohl Avatar answered Dec 06 '25 11:12

michpohl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!