Can't create JaCoCo reports with Gradle

android

(Natalia) #1

I need to create JaCoCo reports with my Android app.

Here is my build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'jacoco'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "myapp"
        minSdkVersion 15
        targetSdkVersion 26
        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
        }
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
        compile 'com.android.support.constraint:constraint-layout:1.0.2'
        testCompile 'junit:junit:4.12'
        testCompile 'org.robolectric:robolectric:3.4.2'
    }
}

task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) { 
    group = "reporting"
    description = "Generate unified Jacoco code coverage report"

    reports {
        xml.enabled = false
        html.enabled = true
        csv.enabled = false
        xml.destination = "app/build/reports/jacocoTestReport.xml"
        html.destination = "app/build/reports/jacoco"
        csv.destination = "app/build/reports/jacocoTestReport.csv"
    }


    def debugTree = fileTree(dir: "app/build/intermediates/classes/debug") 
    def mainSrc = "app/src/main/java"

    sourceDirectories = files([mainSrc])
    classDirectories = files([debugTree])
    executionData = fileTree(dir: "app/build", includes: [
            "jacoco/testDebugUnitTest.exec", 
            "outputs/code-coverage/connected/*coverage.ec"
    ])
}

When I run ./gradlew test, the build is successful, but there are no reports from jacoco.

What’s wrong?