-
-
Save MyDogTom/6cbeedcbf13a61ab55f0d8fef4f46bab to your computer and use it in GitHub Desktop.
Gradle Jacoco config for Android (3.x plugin) with kotlin and custom excludes support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment