Jacoco with Robolectric and Gradle?

1,314 views
Skip to first unread message

Andreas

unread,
Apr 14, 2014, 3:20:53 AM4/14/14
to robol...@googlegroups.com
Hey guys.

Thanks for the efforts in bringing Robolectric to Gradle!
It's all working well and I have Jacoco generating code coverage for Android, but having some issues with configuring Jacoco. This may or may not be related to Robolectric itself, but I suspect someone might know the answer.

I don't seem to be able to configure Jacoco for the Android module as I do for my Java modules. I need to define exclude patterns.

In a Java module, I simply define,
    test {
        jacoco {
            toolVersion = "0.6.3.201306030806"
            excludes = codeCoverageExclude.tokenize(',')
        }
    }


and Jacoco will exclude based on my exclude patterns.

However, for the Android module with Robolectric tests, it seems to ignore my attempts to configure Jacoco.

I have tried putting the jacoco closure both under "androidTest" and just "test", but neither works. As Jacoco actually is running and generating Jacoco exec files for the Android tests, it seems like it should be possible to configure it as well?

Any ideas?

(I'm using Gradle 1.11, Android Gradle plugin 0.9.2 and Robolectric Gradle plugin 0.9.4, running Gradle from command line)

Thanks, Andreas

Paul Peavyhouse

unread,
May 7, 2014, 8:43:42 PM5/7/14
to robol...@googlegroups.com
Any success with this? I am trying to do the same thing, with no luck! :(

Pv
Message has been deleted

Nico Küchler

unread,
May 8, 2014, 7:42:15 AM5/8/14
to robol...@googlegroups.com

Ha Duy Trung

unread,
May 15, 2014, 2:29:26 AM5/15/14
to robol...@googlegroups.com
This is how we make it work at the moment (Gradle 1.11, Android Gradle plugin 0.10.0, Robolectric Gradle plugin 0.10.0):

apply plugin: 'jacoco'


// get class dirs for project dependencies
FileTree getJacocoClassDirs(List projects) {
    def classDirs = fileTree(dir: "${buildDir}/classes/debug", exclude: '**/R*.class')
    projects.each {
        def projBuildDir = project(it).buildDir
        classDirs += fileTree(dir: "${projBuildDir}/classes/release", exclude: '**/R*.class')
    }
    return classDirs
}

// get source dirs for project dependencies
FileCollection getJacocoSrcDirs(List projects) {
    Set srcDirs = android.sourceSets.main.java.srcDirs
    projects.each {
        def projDir = project(it).projectDir
        srcDirs.add("${projDir}/src") // assume that android main sourceSets is here
    }
    return files(srcDirs)
}

// generate coverage report for this project and all its project dependencies
task jacocoTestReport(type: JacocoReport, dependsOn: test) {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination "${buildDir}/reports/coverage"
    }

    // TODO: automatically get project dependencies recursively
    def dependencies = [] // your gradle project dependencies go here
    classDirectories = getJacocoClassDirs(dependencies)
    sourceDirectories = getJacocoSrcDirs(dependencies)
    executionData = files("${buildDir}/jacoco/testDebug.exec")
}
Reply all
Reply to author
Forward
0 new messages