Skip to content

Instantly share code, notes, and snippets.

@maiatoday
Created October 13, 2018 13:30
Show Gist options
  • Save maiatoday/3aa08130c21968ed746e63d23864d5c7 to your computer and use it in GitHub Desktop.
Save maiatoday/3aa08130c21968ed746e63d23864d5c7 to your computer and use it in GitHub Desktop.
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.8.2"
// Custom reports directory can be specfied like this:
// reportsDir = file("$buildDir/customJacocoReportDir")
}
project.afterEvaluate {
android.libraryVariants.all { variant ->
def variantName = variant.name
def testTaskName = "test${variantName.capitalize()}UnitTest"
tasks.create(name: "${testTaskName}Coverage", type: JacocoReport, dependsOn: "$testTaskName") {
group = "Reporting"
description = "Generate Jacoco coverage reports for the ${variantName.capitalize()} build."
reports {
html.enabled = true
xml.enabled = true
}
def excludes = [
'**/AutoValue_*.*',
'**/*_Factory.*',
'**/R.class',
'**/BR.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*',
'**/*Fragment**',
'**/*Activity**',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/*$Lambda$*.*', // Jacoco can not handle several "$" in class name.
'**/*Module.*', // Modules for Dagger.
'**/*Dagger*.*', // Dagger auto-generated code.
'**/*MembersInjector*.*', // Dagger auto-generated code.
'**/*_Provide*Factory*.*',
'**/*_Factory.*', //Dagger auto-generated code
'**/*$*$*.*' // Anonymous classes generated by kotlin
]
def javaClasses = fileTree(dir: variant.javaCompiler.destinationDir, excludes: excludes)
def kotlinClasses = fileTree(dir: "${buildDir}/tmp/kotlin-classes/${variantName}", excludes: excludes)
classDirectories = files([javaClasses, kotlinClasses])
sourceDirectories = files([
"$project.projectDir/src/main/java",
"$project.projectDir/src/${variantName}/java",
"$project.projectDir/src/main/kotlin",
"$project.projectDir/src/${variantName}/kotlin"
])
executionData = files("${project.buildDir}/jacoco/${testTaskName}.exec")
}
tasks.create(name: "${testTaskName}CoverageVerification", type: JacocoCoverageVerification, dependsOn: "${testTaskName}Coverage") {
group = "Reporting"
description = "Verify coverage limits for the ${variantName.capitalize()} build."
def excludes = [
'**/AutoValue_*.*',
'**/*_Factory.*',
'**/R.class',
'**/BR.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*',
'**/*Fragment**',
'**/*Activity**',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/*$Lambda$*.*', // Jacoco can not handle several "$" in class name.
'**/*Module.*', // Modules for Dagger.
'**/*Dagger*.*', // Dagger auto-generated code.
'**/*MembersInjector*.*', // Dagger auto-generated code.
'**/*_Provide*Factory*.*',
'**/*_Factory.*', //Dagger auto-generated code
'**/*$*$*.*' // Anonymous classes generated by kotlin
]
def javaClasses = fileTree(dir: variant.javaCompiler.destinationDir, excludes: excludes)
def kotlinClasses = fileTree(dir: "${buildDir}/tmp/kotlin-classes/${variantName}", excludes: excludes)
classDirectories = files([javaClasses, kotlinClasses])
sourceDirectories = files([
"$project.projectDir/src/main/java",
"$project.projectDir/src/${variantName}/java",
"$project.projectDir/src/main/kotlin",
"$project.projectDir/src/${variantName}/kotlin"
])
executionData = files("${project.buildDir}/jacoco/${testTaskName}.exec")
violationRules {
setFailOnViolation(true)
rule {
element = 'PACKAGE'
limit {
value = 'COVEREDRATIO'
counter = 'BRANCH'
minimum = 0.6
}
}
rule {
element = 'PACKAGE'
limit {
value = 'COVEREDRATIO'
counter = 'INSTRUCTION'
minimum = 0.9
}
}
rule {
element = 'PACKAGE'
includes = ['com.example.package.to.test']
enabled = true
limit {
value = 'COVEREDRATIO'
counter = 'INSTRUCTION'
minimum = 1
}
}
rule {
element = 'PACKAGE'
includes = ['com.example.another.package.to.test']
enabled = true
limit {
value = 'COVEREDRATIO'
counter = 'INSTRUCTION'
minimum = 0.96
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment