I am using JaCoCo with a project using RoboGuice, Butterknife and Robolectric. I was able to customize it using the @Hieu Rocker solution, however there were some minor flaws, that is, in our project we use aromas and additional tests for these tastes, as well as additional Java code for each of them. We also use different types of assembly. Therefore, the decision to rely on the testDebug task was not enough. Here is my solution: In build.gradle in the application module add
apply from: '../app/jacoco.gradle'
Then create the jacoco.gradle file inside the application module with the following contents:
apply plugin: 'jacoco'
jacoco {
toolVersion "0.7.1.201405082137"
}
def getFlavorFromVariant (String variantName) {
def flavorString = variantName.replaceAll (/ (. *) ([AZ]. *) /) {all, flavorName, buildTypeName ->
flavorName
}
return flavorString;
}
def getBuildTypeFromVariant (String variantName) {
def buildTypeString = variantName.replaceAll (/ (. *) ([AZ]. *) /) {all, flavorName, buildTypeName ->
"$ {buildTypeName.toLowerCase ()}"
}
return buildTypeString;
}
def getFullTestTaskName (String variantName) {
return "test $ {variantName.capitalize ()} UnitTest";
}
android.applicationVariants.all {variant ->
def variantName = variant.name;
def flavorFromVariant = getFlavorFromVariant ("$ {variantName}");
def buildTypeFromVariant = getBuildTypeFromVariant ("$ {variantName}");
def testTaskName = getFullTestTaskName ("$ {variantName}")
task ("jacoco $ {variantName.capitalize ()} TestReport", type: JacocoReport, dependsOn: testTaskName) {
group = "Reporting"
description = "Generate JaCoCo coverage reports after running tests for variant: $ {variantName}."
reports {
xml.enabled = true
html.enabled = true
}
classDirectories = fileTree (
dir: "./build/intermediates/classes/${flavorFromVariant}/${buildTypeFromVariant}",
excludes: ['** / R * .class',
'** / * $ InjectAdapter.class',
'** / * $ ModuleAdapter.class',
'** / * $ ViewInjector * .class'
]
)
logger.info ("Configuring JaCoCo for flavor: $ {flavorFromVariant}, buildType: $ {buildTypeFromVariant}, task: $ {testTaskName}");
def coverageSourceDirs = [
'../app/src/main/java',
"../app/src/${flavorFromVariantasket/java"
]
sourceDirectories = files (coverageSourceDirs)
executionData = files ("$ buildDir / jacoco / $ {testTaskName} .exec")
// Bit hacky but fixes https://code.google.com/p/android/issues/detail?id=69174.
// We iterate through the compiled .class tree and rename $$ to $.
doFirst {
new File ("$ buildDir / intermediates / classes /"). eachFileRecurse {file ->
if (file.name.contains ('$$')) {
file.renameTo (file.path.replace ('$$', '$'))
}
}
}
}
}
You can execute it from the command line as follows:
.gradlew jacocoFlavor1DebugTestReport
or
.gradlew jacocoOtherflavorPrereleaseTestReport
In our project, we use the agreement not to use an uppercase letter inside the names of the flavor and type of assembly, but if your project does not comply with this agreement, you can simply change the functions getFlavorFromVariant (..) and getBuildTypeFromVariant (..)
Hope this helps someone
Piotr Zawadzki Jun 16 '15 at 10:05 2015-06-16 10:05
source share