简体   繁体   English

如何从 Jacoco 代码覆盖率中排除 Fragment

[英]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.我已经使用下面的代码来排除片段,但我无法从 jacoco 代码覆盖率中排除片段,请帮助我解决这个问题。

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.我还注意到在 Android 上,不同版本的 Gradle 和 jacoco 插件并不总是非常兼容。 So it might help to experiment with downgrading (or upgrading) too.因此,尝试降级(或升级)也可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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