I'm using JaCoCo with a project using RoboGuice, Butterknife and Robolectric. I was able to set it up using @Hieu Rocker's solution, however there were some minor drawbacks i.e. in our project we use flavors and have some extra tests for those flavors as well as extra java code for each of them. We also use different build types. Therefore a solution to rely on the "testDebug" task was not good enough.
Here's my solution:
In build.gradle in app module add
apply from: '../app/jacoco.gradle'
Then create a jacoco.gradle file inside of app module with the following content:
apply plugin: 'jacoco'
jacoco {
toolVersion "0.7.1.201405082137"
}
def getFlavorFromVariant(String variantName) {
def flavorString = variantName.replaceAll(/(.*)([A-Z].*)/) { all, flavorName, buildTypeName ->
flavorName
}
return flavorString;
}
def getBuildTypeFromVariant(String variantName) {
def buildTypeString = variantName.replaceAll(/(.*)([A-Z].*)/) { 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/${flavorFromVariant}/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 command line like this:
.gradlew jacocoFlavor1DebugTestReport
or
.gradlew jacocoOtherflavorPrereleaseTestReport
In our project we use a convention not to use capital letter inside of flavor and build type names, but if your project does not follow this convention you can simply change getFlavorFromVariant(..) and getBuildTypeFromVariant(..) functions
Hope this helps someone
jacocoagent
and retrieving the report from the emulator. – Liberalism