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')
}
Starting from JaCoCo 0.8. 2, we can exclude classes and methods by annotating them with a custom annotation with the following properties: The name of the annotation should include Generated. The retention policy of annotation should be runtime or class.
The easiest way to exclude code from code coverage analysis is to use ExcludeFromCodeCoverage attribute. This attribute tells tooling that class or some of its members are not planned to be covered with tests. EditFormModel class shown above can be left out from code coverage by simply adding the attribute.
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/**',
])
})
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With