Skip to content

Instantly share code, notes, and snippets.

@almozavr
Last active January 5, 2021 12:37
Show Gist options
  • Save almozavr/3cd46c4b4f0babbfc136db91924ab56e to your computer and use it in GitHub Desktop.
Save almozavr/3cd46c4b4f0babbfc136db91924ab56e to your computer and use it in GitHub Desktop.
Gradle Jacoco config for Android (3.x plugin) with kotlin and custom excludes support
apply plugin: "jacoco"
jacoco {
toolVersion = deps.test.jacocoVersion
}
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
}
task('jacocoReports') {
group = "Reporting"
description = "Generate Jacoco coverage reports for all variants"
}
task('jacocoVerifications') {
group = "Verification"
description = "Generate Jacoco coverage reports for all variants"
}
variants().all { variant ->
def params = prepareJacocoParams(variant)
def reportTask = createReportTask(params)
def verificationTask = createVerificationTask(params)
verificationTask.dependsOn reportTask
jacocoReports.dependsOn reportTask
}
def prepareJacocoParams(variant) {
def params = [:]
params.variantName = variant.name
params.variantCapName = variant.name.capitalize()
params.fileFilter = [ '**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*',
// '**/App*.*',
// '**/*Activity*.*',
]
params.classDirectories = files([fileTree(
dir: variant.javaCompiler.destinationDir,
excludes: params.fileFilter
) + fileTree(
dir: "$buildDir/tmp/kotlin-classes/$params.variantName",
excludes: params.fileFilter
)])
params.sourceDirectories = files([
"src/main/java", "src/main/kotlin",
"src/$params.variantName/java", "src/$params.variantName/kotlin"
])
params.executionData = files("${buildDir}/jacoco/junitPlatformTest${params.variantCapName}.exec")
return params
}
def createReportTask(params) {
return task("jacoco${params.variantCapName}Report", type: JacocoReport, dependsOn: "test${params.variantCapName}UnitTest") {
group = "Reporting"
description = "Generate Jacoco coverage reports for $params.variantCapName"
reports {
xml.enabled = true
html.enabled = true
csv.enabled = false
}
classDirectories = params.classDirectories
sourceDirectories = params.sourceDirectories
executionData = params.executionData
}
}
def createVerificationTask(params) {
return task("jacoco${params.variantCapName}Verification", type: JacocoCoverageVerification) {
sourceDirectories = params.sourceDirectories
classDirectories = params.classDirectories
executionData = params.executionData
violationRules {
failOnViolation true
rule {
element = 'PACKAGE'
includes = ['com.myapp.domain.*']
excludes = ['com.myapp.domain.entity']
limit {
counter = 'CLASS'
value = 'MISSEDCOUNT'
maximum = 0
}
}
rule {
element = 'CLASS'
includes = ['com.myapp.domain.*']
excludes = ['com.myapp.domain.entity.*']
limit {
counter = 'INSTRUCTION'
value = 'COVEREDRATIO'
minimum = 0.8
}
}
}
}
}
def variants() {
if (project.android.hasProperty('libraryVariants')) {
return project.android.libraryVariants
} else {
return project.android.applicationVariants
}
}
@Ribesg
Copy link

Ribesg commented Oct 17, 2018

As of today this still works (when using @amatkivskiy change)

@jangrewe
Copy link

Thanks, i had to change @amatkivskiy 's change to files("${buildDir}/jacoco/test${params.variantCapName}UnitTest.exec") to make it work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment