Last active
December 20, 2021 13:15
-
-
Save kaeawc/ce105cf1935820ef47795a8ad811938a to your computer and use it in GitHub Desktop.
Jacoco settings for multi module multi flavor Kotlin Android app
This file contains hidden or 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
codecov: | |
branch: master | |
bot: null | |
coverage: | |
precision: 2 | |
round: down | |
range: "70...100" | |
status: | |
changes: | |
default: | |
branches: null | |
ignore: null | |
comment: | |
layout: "diff, tree" | |
branches: null | |
behavior: default | |
require_changes: yes | |
require_base: no | |
require_head: yes |
This file contains hidden or 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' | |
project.afterEvaluate { | |
// Grab all build types and product flavors | |
def buildTypes = android.buildTypes.collect { type -> | |
type.name | |
} | |
def productFlavors = android.productFlavors.collect { flavor -> | |
flavor.name | |
} | |
// When no product flavors defined, use empty | |
if (!productFlavors) productFlavors.add('') | |
productFlavors.each { productFlavorName -> | |
buildTypes.each { buildTypeName -> | |
def sourceName, sourcePath | |
if (!productFlavorName) { | |
sourceName = sourcePath = "${buildTypeName}" | |
} else { | |
sourceName = "${productFlavorName}${buildTypeName.capitalize()}" | |
sourcePath = "${productFlavorName}/${buildTypeName}" | |
} | |
def testTaskName = "test${sourceName.capitalize()}UnitTest" | |
// Create coverage task of form 'testFlavorTypeCoverage' depending on 'testFlavorTypeUnitTest' | |
task "${testTaskName}Coverage" (type:JacocoReport, dependsOn: "$testTaskName") { | |
group = "Reporting" | |
description = "Generate Jacoco coverage reports on the ${sourceName.capitalize()} build." | |
def fileFilter = [ | |
'**/R.class', | |
'**/R$*.class', | |
'**/*$ViewInjector*.*', | |
'**/*$ViewBinder*.*', | |
'**/BuildConfig.*', | |
'**/Manifest*.*', | |
'**/*App.*', | |
'**/*Component.*', | |
'**/*Module.*', | |
'**/*Activity.*', | |
'**/*Fragment.*' | |
] | |
def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/$productFlavorName/$buildTypeName", excludes: fileFilter) | |
def kotlinDebugTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/$sourceName", excludes: fileFilter) | |
classDirectories = files([debugTree], [kotlinDebugTree]) | |
def coverageSourceDirs = [ | |
"src/main/java", | |
"src/$productFlavorName/java", | |
"src/$buildTypeName/java" | |
] | |
additionalSourceDirs = files(coverageSourceDirs) | |
sourceDirectories = files(coverageSourceDirs) | |
executionData = files("${project.buildDir}/jacoco/${testTaskName}.exec") | |
reports { | |
xml.enabled = true | |
html.enabled = true | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or 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: 'com.android.library' | |
apply from: "../jacoco.gradle" | |
android { | |
compileSdkVersion rootProject.ext.compileSdkVersion | |
buildToolsVersion "$buildToolsVersion" | |
defaultConfig { | |
minSdkVersion rootProject.ext.minSdkVersion | |
targetSdkVersion rootProject.ext.targetSdkVersion | |
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | |
} | |
buildTypes { | |
debug { | |
debuggable true | |
minifyEnabled false | |
testCoverageEnabled true | |
} | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
flavorDimensions "env" | |
productFlavors { | |
internal { | |
dimension "env" | |
} | |
external { | |
dimension "env" | |
} | |
} | |
} | |
dependencies { | |
// All your dependencies | |
} |
This file contains hidden or 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
buildscript { | |
apply from: rootProject.file("gradle/versions.gradle") | |
repositories { | |
jcenter() | |
google() | |
} | |
dependencies { | |
classpath "org.jacoco:org.jacoco.core:0.7.9" | |
classpath 'com.android.tools.build:gradle:3.0.1' | |
} | |
} | |
allprojects { | |
repositories { | |
jcenter() | |
google() | |
} | |
} | |
task clean(type: Delete) { | |
delete rootProject.buildDir | |
} |
This file contains hidden or 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
// Variables for entire project | |
ext { | |
// Setup | |
minSdkVersion = 16 | |
targetSdkVersion = 27 | |
compileSdkVersion = 27 | |
buildToolsVersion = "27.0.2" | |
} |
@douglasalipio jacoco.gradle line 22: "def testTaskName = "test${sourceName.capitalize()}UnitTest". Basically, you will execute
./gradlew testFlavorDebugUnitTest
Based on current code, reports are under each module (such as :app, :dep1, :dep2, etc.).
Is it possible to merge results and have them all under root/build/reports/jacoco
?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Which task do I need to execute to see the reports?