Skip to content

Instantly share code, notes, and snippets.

@gmazzo
Created December 16, 2018 17:09
Show Gist options
  • Save gmazzo/86a365c11bc92eb59c963dada03f960f to your computer and use it in GitHub Desktop.
Save gmazzo/86a365c11bc92eb59c963dada03f960f to your computer and use it in GitHub Desktop.
Jacoco script for Android unit and instrumentation tests coverage report, supporting Kotlin
apply plugin: 'jacoco'
jacoco {
toolVersion = '0.8.2'
}
android {
buildTypes {
debug {
testCoverageEnabled = true
}
}
testOptions.unitTests.all {
jacoco {
includeNoLocationClasses = true
}
}
}
task jacocoTestReport { self ->
build.dependsOn self
}
// based on https://proandroiddev.com/unified-code-coverage-for-android-revisited-44789c9b722f and https://github.com/nomisRev/AndroidGradleJacoco
android.testVariants.all {
def variant = it.testedVariant
def name = variant.name.capitalize()
tasks.create(name: "jacoco${name}TestReport", type: JacocoReport) { self ->
group = 'Reporting'
description = "Generates Jacoco coverage reports on the ${variant.name} variant"
reports {
xml.enabled = true
html.enabled = true
}
sourceDirectories = files(variant.sourceSets.collectMany({ it.java.source }))
classDirectories = fileTree(dir: buildDir, includes: [
"intermediates/classes/${variant.name}/**",
"tmp/kotlin-classes/${variant.name}/**"
], excludes: [
'android/**/*.*',
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Test*.*',
'**/*Module.*', // modules for Dagger.
'**/*Module$Companion.*', // modules for Dagger+Kotlin.
'**/*Dagger*.*', // Dagger auto-generated code.
'**/*MembersInjector*.*', // Dagger auto-generated code.
'**/*_Provide*Factory*.*',
'**/*_Factory.*', // Dagger auto-generated code
])
executionData = fileTree(dir: buildDir, includes: [
"jacoco/test${name}UnitTest.exec",
'outputs/code-coverage/connected/*coverage.ec'
])
doLast {
println "Wrote HTML coverage report to ${reports.html.destination}/index.html"
println "Wrote XML coverage report to ${reports.xml.destination}"
}
dependsOn "test${name}UnitTest", "create${name}CoverageReport"
jacocoTestReport.dependsOn self
}
}
@mahdit83
Copy link

But seems that this wont include androidUnitTests, so in out put I can just see unit tests result. Do you have any thought?

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