24

I want to run my tests in Android app and create coverage reports, so I added Jacoco configuration into my build.gradle file, but it doesn't work.

apply plugin: 'com.android.application'

android {

    compileSdkVersion 22
    buildToolsVersion '22.0.1'

    defaultConfig {
        applicationId "mm"
        minSdkVersion 12
        targetSdkVersion 18
    }


    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

dependencies {
    compile 'com.google.code.gson:gson:2.1'
    compile files('libs/android-async-http-1.4.4.jar')
    compile files('libs/freemarker.jar')
    compile files('libs/greendao-1.3.1.jar')
    compile files('libs/raygun4android-1.1.0.jar')
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:recyclerview-v7:22.2.0'
    testCompile 'org.mockito:mockito-core:1.10.19'
    testCompile 'org.hamcrest:hamcrest-library:1.3'
    compile 'junit:junit:4.11'

    androidTestCompile('junit:junit:4.11') {
        exclude module: 'hamcrest-core'
    }

}

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.1'
        classpath 'org.robolectric:robolectric-gradle-plugin:0.11.+'
    }
}

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

jacoco {
    version "0.7.1.201405082137"
}


jacoco {
    toolVersion "0.7.1.201405082137"
}

def coverageSourceDirs = [
        'src/main/java',
]

task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    reports {
        xml.enabled = true
        html.enabled = true
    }
    classDirectories = fileTree(
            dir: './build/intermediates/classes/debug',
            excludes: ['**/R*.class',
                       '**/*$InjectAdapter.class',
                       '**/*$ModuleAdapter.class',
                       '**/*$ViewInjector*.class'
            ])
    sourceDirectories = files(coverageSourceDirs)
    executionData = files("$buildDir/jacoco/testDebug.exec")
    doFirst {
        new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
            if (file.name.contains('$$')) {
                file.renameTo(file.path.replace('$$', '$'))
            }
        }
    }
}

I know, there is issue with gradle version 1.3.0 and with 1.3.1 it should work normally, however with 1.3.1 I get Task 'createDebugCoverageReport' not found in root project.

2 Answers 2

38

You have to enable the testCoverageEnabled :

buildTypes {
    debug{
        debuggable true
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        isTestCoverageEnabled true
    }
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        isTestCoverageEnabled true
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I'm having the same issue as the OP. I have testCoverageEnabled set to true, but it doesn't seem to help.
Do you have it included in the required buildType of the module(s) where the source code is?
@DannyChia which device you are using? Android version, company
But how to enable it for domain module (java-library)? We don't have buildTypes on java library.
I cannot add the property and enabled in it says "Unresolved reference: testCoverageEnabled" any ideas ?
1

isTestCoverageEnabled is deprecated, so use:

buildTypes {
    release {
        ...
        enableUnitTestCoverage = true
        enableAndroidTestCoverage = true
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.