简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM