Home » Android » java – Gradle Jacoco – Could not find method jacocoTestReport()

java – Gradle Jacoco – Could not find method jacocoTestReport()

Posted by: admin June 15, 2020 Leave a comment

Questions:

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 [[email protected]] 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?

How to&Answers:

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.

Answer:

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'.