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?

Igor Lopatinkiy
  • 103
  • 1
  • 1
  • 5
  • I am not very sure if this is your issue but try removing the curly braces fron the buildDir -> `html.destination file("$buildDir/jacocoHtml")` – Cabrra Jul 15 '19 at 19:55

2 Answers2

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

Kyle
  • 1,430
  • 1
  • 11
  • 34
  • 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. – Kyle Jul 16 '19 at 15:35
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
    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](/help/how-to-answer). – alea Apr 30 '23 at 07:48