Skip to content

Instantly share code, notes, and snippets.

@halfvector
Created September 29, 2016 16:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halfvector/1d12449612980c91c2886e27e634eec0 to your computer and use it in GitHub Desktop.
Save halfvector/1d12449612980c91c2886e27e634eec0 to your computer and use it in GitHub Desktop.
jacocoTestReport for unit + instrumentation test code coverage for Android
/**
* This JacocoTestReport task merges local Unit Tests and emulator/device Instrumentation Tests into a single coverage report.
* Also ignores Dagger 2 generated code.
* Add this task to your Android app/build.gradle and run your test suites then execute this task.
* Coverage report will appear in app/build/reports/jacoco/jacocoTestReport/
* as standard xml compatible with codecov.io and human readable html
* dependsOn: ['testDebugUnitTest', 'connectedDebugAndroidTest']
*/
task jacocoTestReport(type: JacocoReport) {
group = "Reporting"
description = "Generate Jacoco coverage reports"
reports {
xml.enabled = true
html.enabled = true
}
// Exclude Dagger 2 and other generated code
def fileFilter = [
'**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*',
'**/*_*Factory*.*',
'**/*_Module*.*',
'**/*_ViewBinding*.*',
'**/*_MembersInjector*.*',
'**/*_Provide*.*',
'**/Dagger*.*',
]
def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
executionData = files([
// Unit Test coverage
"${buildDir}/jacoco/testDebugUnitTest.exec",
// Instrumentation Test coverage (one device per ec file)
fileTree(dir: "${buildDir}/outputs/code-coverage/connected/").matching {
include '*.ec'
}
])
// Always regenerate report
outputs.upToDateWhen { false }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment