Skip to content

Instantly share code, notes, and snippets.

@ultraon
Last active August 19, 2020 18:10
Show Gist options
  • Save ultraon/54cca81ca159ed0a4a9ebf62e89c26ba to your computer and use it in GitHub Desktop.
Save ultraon/54cca81ca159ed0a4a9ebf62e89c26ba to your computer and use it in GitHub Desktop.
Good example of the merged Jacoco code covarage Gradle configuration
apply plugin: 'com.android.application'
apply from: "$rootDir/coverage.gradle"
//...
android {
//...
buildTypes {
//...
debug {
testCoverageEnabled true //we use "debug" build type for test coverage (can be other)
}
}
//...
testOptions {
unitTests.returnDefaultValues = true
unitTests.all {
jacoco {
includeNoLocationClasses = true
}
}
}
//...
}
apply plugin: 'jacoco'
/**
* The correct path of the report is $rootProjectDir/app/build/reports/jacoco/index.html
* to run this task use: ./gradlew clean jacocoTestReport
*/
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) { //we use "debug" build type for test coverage (can be other)
group = "reporting"
description = "Generate unified Jacoco code coverage report"
reports {
xml.enabled = false
html.enabled = true
csv.enabled = false
xml.destination = "${buildDir}/reports/jacocoTestReport.xml"
html.destination = "${buildDir}/reports/jacoco"
csv.destination = "${buildDir}/reports/jacocoTestReport.csv"
}
def fileFilter = [
'**/*Test*.*',
'**/AutoValue_*.*',
'**/*JavascriptBridge.class',
'**/R.class',
'**/R$*.class',
'**/Manifest*.*',
'android/**/*.*',
'**/BuildConfig.*',
'**/*$ViewBinder*.*',
'**/*$ViewInjector*.*',
'**/Lambda$*.class',
'**/Lambda.class',
'**/*Lambda.class',
'**/*Lambda*.class',
'**/*$InjectAdapter.class',
'**/*$ModuleAdapter.class',
'**/*$ViewInjector*.class',
'**/*_MembersInjector.class', //Dagger2 generated code
'*/*_MembersInjector*.*', //Dagger2 generated code
'**/*_*Factory*.*', //Dagger2 generated code
'*/*Component*.*', //Dagger2 generated code
'**/*Module*.*' //Dagger2 generated code
]
def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter) //we use "debug" build type for test coverage (can be other)
def mainSrc = "${project.projectDir}/src/main/java"
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
executionData = fileTree(dir: "$buildDir", includes: [
"jacoco/testDebugUnitTest.exec", //we use "debug" build type for test coverage (can be other)
"outputs/code-coverage/connected/*coverage.ec"
])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment