10

I have an android project with Gradle 5.1.1 I want to use JaCoCo to generate HTML reports about test code coverage. I tried to follow the instructions in some articles, but all of my attempts led to errors.

All that worked for me is applying plugin 'jacoco' (Project successfully builded after that).

apply plugin: 'jacoco'

But as soon as i add task 'jacocoTestReport', Gradle cannot sync with files and i get this error.

That's my task:

jacocoTestReport {
reports {
    html.enabled true
    xml.enabled false
    csv.enabled false
    html.destination file("${buildDir}/jacocoHtml")
}}

That's my error:

ERROR: Could not find method jacocoTestReport() for arguments [build_8kqk7qvpoocfxfydulk1p4m35$_run_closure3@1aba12a5] on project ':app' of type org.gradle.api.Project.

I tried using "gradlew clean build" after adding "apply plugin" in 'build.gradle'. Author of tutorial successfully added task and used this line in terminal to get jacoco report:

gradlew build jacocoTestReport

Here's links to articles i used to understand how to add jacoco to project:

https://reflectoring.io/jacoco/

https://android.jlelse.eu/get-beautiful-coverage-reports-in-your-android-projects-ce9ba281507f

So my question is: Is there a way to add Jacoco to Android Project with Gradle? What am i doing wrong?

1
  • I am not very sure if this is your issue but try removing the curly braces fron the buildDir -> html.destination file("$buildDir/jacocoHtml") Commented Jul 15, 2019 at 19:55

2 Answers 2

6

Might just be a missing copy paste, but make sure you define your function as a task in gradle, ie: task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest']) {

I followed this blog for setting my Jacoco instance in my initial projects and found it quite helpful. https://engineering.rallyhealth.com/android/code-coverage/testing/2018/06/04/android-code-coverage.html

Sign up to request clarification or add additional context in comments.

1 Comment

Glad it worked for you. Jacoco is awsome and I highly recommend looking into using Danger with it for your pipelines. The blog I posted above is great for detailing how to support multi module projects as well.
0
plugins {
    id 'jacoco'
}

buildTypes {
    debug {
        testCoverageEnabled true
    }
}

task jacocoTestReport(type: JacocoReport,
    dependsOn: ['testDebugUnitTest']) {

reports.html.enabled = true

def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']

def mainSrc = "$project.projectDir/src/main/java"
sourceDirectories.from = files([mainSrc])

def javaTree = fileTree(dir: "$project.buildDir/intermediates/javac/debug/classes", excludes: fileFilter)
def kotlinTree = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/debug", excludes: fileFilter)
classDirectories.from = files([javaTree, kotlinTree])

executionData.from = fileTree(dir: "$buildDir", includes: [
        "outputs/unit_test_code_coverage/debugUnitTest/testDebugUnitTest.exec",
        "outputs/code-coverage/debugAndroidTest/connected/*/coverage.ec"])

}

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.