Gradle Jacoco - Could not find method jacocoTestReport()

17,969

Solution 1

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.

Solution 2

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

Share:
17,969

Related videos on Youtube

Guido
Author by

Guido

Updated on June 04, 2022

Comments

  • Guido
    Guido almost 2 years

    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?

  • Guido
    Guido about 7 years
    Hey, I already tried that. But I'm using this on Android, so I can't use apply plugin: 'java'. Sorry I forgot to mention that, I updated the tags.
  • Godin
    Godin about 7 years
    @Guido I'm not developing for Android, but absence of java plugin looks weird. In any case I believe that you should report this incompatibility either to Gradle developers (because JaCoCo plugin is part of Gradle distribution), either to developers of plugins for Android.
  • Guido
    Guido about 7 years
    This is already a known thing. You will also receive the following error when you try to use apply plugin: 'java' in an Android application: Error:The 'java' plugin has been applied, but it is not compatible with the Android plugins.