jacocoTestReport task not being generated

13,380

Solution 1

You are searching for a wrong task to execute. By performing ./gradlew tasks you'll be able to find createFlavorCoverageReport tasks:

enter image description here

After executing ./gradlew createDevDebugCoverageReport with the setup that you have mentioned in the question I was able to find generated report at /app/build/reports/dev/debug directory.

Solution 2

There are several things which we have to take care while using jacoco report which as follows:

Enabled test coverage in app/build.gradle

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

Create task for jacoco report

apply plugin: 'jacoco'

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

    reports {
        xml.enabled = true
        html.enabled = true
    }

    def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
    def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
    def mainSrc = "${project.projectDir}/src/main/java"

    sourceDirectories = files([mainSrc])
    classDirectories = files([debugTree])
    executionData = files("${buildDir}/jacoco/testDebugUnitTest.exec")
}

Gradle command for jacoco report

./gradlew clean jacocoTestReport

Find jacoco report here

Generated jacoco report path after successful jacocoTestReport execution.

app/build/reports/coverage/debug/index.html

Also, I have created one android jacoco related sample repository where you can look.

https://github.com/jiteshmohite/JacocoAndroidSample

Also, please ensure you are running Gradle command inside the application directory.

Try the above-mentioned sample repository for reference. I created this with zero complexities, So everyone can go and use it.

Solution 3

There are two things:

  • You need to enable the code coverage support for the build type that you will be testing with. Your build.gradle should include the following (Which you've already included):

    android { 
        ... 
        buildTypes { 
            debug { 
                testCoverageEnabled = true 
            } 
            ... 
        } 
        ... 
    }
    
    • To generate a report, run gradle testBlueDebugUnitTestCoverage and you will see them in “build/reports/jacoco/testBlueDebugUnitTestCoverage/”
    • Here is a gitHub example

  • Use Gradle plugin that generates JaCoCo reports:

    Setup it as like this:

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

Another solution to issue reported here:

task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") { 
    group = "Reporting" 
    description = "Generate Jacoco coverage reports after running tests." 
    reports { 
        xml.enabled = false 
        html.enabled = true 
    } 
    classDirectories = fileTree( 
            dir: './build/classes/debug', 
            excludes: ['**/R.class', 
                       '**/R$*.class', 
                       '**/*$InjectAdapter.class', 
                       '**/*$ModuleAdapter.class', 
                       '**/*$ViewInjector*.class' 
            ]) 
    sourceDirectories = files(coverageSourceDirs) 
    executionData = files('build/jacoco/testDebug.exec') 

    renamedFilesMap = [:] 

    // Hacky fix for issue: https://code.google.com/p/android/issues/detail?id=69174. 
    // Rename files with '$$' before generating report, and then rename back after 
    doFirst { 
        new File('build/classes/debug').eachFileRecurse { file -> 
            if (file.name.contains('$$')) { 
                oldPath = file.path 
                newPath = oldPath.replace('$$', '$') 
                file.renameTo(newPath) 
                renamedFilesMap[newPath] = oldPath 
            } 
        } 
    } 
    doLast { 
        renamedFilesMap.each() { 
            newPath, oldPath -> 
                new File(newPath).renameTo(oldPath) 
        } 
    } 

}
Share:
13,380

Related videos on Youtube

ant2009
Author by

ant2009

Updated on October 23, 2022

Comments

  • ant2009
    ant2009 over 1 year
    Android Studio 3.1 Canary 8
    Build #AI-173.4529993, built on January 6, 2018
    JRE: 1.8.0_152-release-1024-b01 amd64
    JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
    Linux 4.14.14-300.fc27.x86_64
    

    I am trying to use the jacoco to generate code coverage. However, when I run the command ./gradlew tasks I don't see any tasks called jacocoTestReport.

    I get the below error when I try and run the tasks ./gradlew jacocoTestReport:

    Task 'jacocoTestReport' not found in root project 'EnumSample'

    This is my build.gradlew file:

    apply plugin: 'com.android.application'
    apply plugin: 'jacoco'
    
    android {
        compileSdkVersion 27
        defaultConfig {
            applicationId "me.androidbox.enumsample"
            minSdkVersion 19
            targetSdkVersion 27
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
            debug {
                testCoverageEnabled true
            }
        }
    }
    
    jacoco {
        toolVersion "0.8.0"
    }
    
    task jacocoTestReport(type: JacocoReport) {
        executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
    
        subprojects.each {
            sourceSets it.sourceSets.main
        }
    
        reports {
            xml.enabled true
            html.enabled false
            csv.enabled false
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:27.0.2'
        implementation 'com.android.support.constraint:constraint-layout:1.0.2'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.1'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    }
    

    I have tried to clean and rebuild the project. However, the reporting task isn't there.

    Many thanks for any suggestions.

  • Girish
    Girish about 3 years
    Followed the steps but i see below in index.html --app No class files specified.