Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
}
@amadeu01
Copy link

amadeu01 commented Apr 16, 2018

Thanks!

@amatkivskiy
Copy link

amatkivskiy commented Jul 12, 2018

@almozavr Thanks for this code snippet.
Unfortunately to make it work (on Gradle 4.4 and Android plugin 3.1.3) you need to change executionData file name to
files("${buildDir}/jacoco/test${variantCapName}UnitTest.exec")

@Ribesg
Copy link

Ribesg commented Oct 17, 2018

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

@jangrewe
Copy link

jangrewe commented Oct 31, 2018

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