Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle Jacoco - Could not find method jacocoTestReport()

I'm trying to generate a Jacoco test report in Gradle. When I try to sync my code, I will receive the following error:

Error:(56, 0) Could not find method jacocoTestReport() for arguments [build_38ehqsoyd54r3n1gzrop303so$_run_closure4@10012308] on project ':app' of type org.gradle.api.Project.

My build.gradle file contains the following items:

apply plugin: 'jacoco'

jacoco {
    toolVersion = "0.7.6.201602180812"
    reportsDir = file("$buildDir/reports/jacoco")
}

jacocoTestReport {
    group = "Reporting"
    reports {
        xml.enabled true
        csv.enabled false
        html.destination "${buildDir}/reports/coverage"
    }
}

When I look at the documentation , I don't see anything which I'm doing wrong.

Gradle version: 3.3

Why am I receiving this error and how can I fix it?

like image 254
Guido Avatar asked May 09 '17 13:05

Guido


2 Answers

As stated in documentation mentioned in your question:

If the Java plugin is also applied to your project, a new task named jacocoTestReport is created that depends on the test task.

what is pretty logical - measurement of coverage for Java code requires its compilation, execution of tests, etc.

So that indeed usage of your example of build.gradle causes failure, which disappears after addition of apply plugin: 'java'.

like image 126
Godin Avatar answered Sep 20 '22 11:09

Godin


Basically I know two ways to achieve this.

The first approach is the built-in android gradle plugin feature:

android { 
    ... 
    buildTypes { 
       debug { 
          testCoverageEnabled = true 
       } 
       ... 
    } 
    ... 
}

This one will define gradle tasks, which can be executed. As far as I know this works fine with instrumentation tests. More information: Code Coverage on Android

The 2nd approach is to use this plugin:

https://github.com/vanniktech/gradle-android-junit-jacoco-plugin

Setup is easy:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
         classpath 'com.vanniktech:gradle-android-junit-jacoco-plugin:0.6.0'
    }
}

apply plugin: 'com.vanniktech.android.junit.jacoco'

And after project sync, you will have tasks like jacocoTestReport<Flavor><BuildType>

We use this to measure the code coverage of our unit tests running on the local machine.

like image 24
Christopher Avatar answered Sep 19 '22 11:09

Christopher