0

My root build.gradle file

   buildscript {
    repositories {
        google()
        mavenCentral()
        jcenter()
        maven { url "https://plugins.gradle.org/m2/" }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath "org.jacoco:org.jacoco.core:0.8.0"
    }
}

apply plugin: 'android-sdk-manager'

allprojects {
    apply plugin: 'jacoco'
    apply plugin: 'com.github.ben-manes.versions'

    repositories {
        google()
        mavenCentral()
        jcenter()
        flatDir {
            dirs 'libs'
        }
    }

    tasks.withType(JavaCompile) {
        sourceCompatibility = JavaVersion.VERSION_1_10
        targetCompatibility = JavaVersion.VERSION_1_10

        // report all Java errors even if the IDE does not
        configure(options) {
            compilerArgs << '-Xlint:all' << '-Xlint:-options'
            deprecation = true
            encoding = 'UTF-8'
        }
    }

    // print errors from test in the terminal
    tasks.withType(Test) {
        testLogging {
            exceptionFormat 'full'
        }
    }
    project.plugins.whenPluginAdded { plugin ->
        if ("com.android.build.gradle.AppPlugin".equals(plugin.class.name)) {
            println "Disable pre dexing for module ${project.name}"
            project.android.dexOptions.preDexLibraries = false
        } else if ("com.android.build.gradle.LibraryPlugin".equals(plugin.class.name)) {
            println "Disable pre dexing for module ${project.name}"
            project.android.dexOptions.preDexLibraries = false
        }
    }
}

app/build.gradle file

apply plugin: 'com.android.application'

apply from: '../jacoco.gradle'

android {
    compileSdkVersion COMPILE_SDK_VERSION as int
    buildToolsVersion BUILD_TOOLS_VERSION
    defaultConfig {
        applicationId "..."
        minSdkVersion MIN_SDK_VERSION as int
        targetSdkVersion TARGET_SDK_VERSION as int
        project.ext.set("archivesBaseName", "..." + versionName)
        multiDexEnabled true

        renderscriptTargetApi RENDERSCRIPT_TARGET_API as int
        renderscriptSupportModeEnabled true
    }

    signingConfigs {
        debugKey {
            ...
        }
        releaseKey {
            ...
        }
    }

    buildTypes {
        debug {
            versionNameSuffix ''
            debuggable true
            minifyEnabled false
            zipAlignEnabled true
            signingConfig signingConfigs.debugKey
            testCoverageEnabled true
        }
        staging {
            versionNameSuffix ''
            debuggable false
            minifyEnabled true
            zipAlignEnabled true
            signingConfig signingConfigs.stagKey
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            matchingFallbacks = ['release']
        }
        release {
            versionNameSuffix ''
            debuggable false
            minifyEnabled true
            zipAlignEnabled true
            signingConfig signingConfigs.releaseKey
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'LICENSE.txt'
    }
    testOptions {
        animationsDisabled true
        unitTests {
            returnDefaultValues = true
            includeAndroidResources = true
        }
    }
    lintOptions {
        lintConfig file('lint.xml')
        abortOnError false
    }
    sourceSets {
        main {
            java.srcDirs = ['src/main/java']
        }
    }
    dexOptions {
        javaMaxHeapSize "3g"
        jumboMode = true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    // Espresso
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    // Junit
    testImplementation "junit:junit:${JUNIT_VERSION}"

    //Mockito
    testImplementation "org.mockito:mockito-core:2.22.0"

    ...
}

and jacoco.gradle file

apply plugin: 'jacoco'

def buildDir = "$project.buildDir"
def taskName = "debug"
def testTaskName = "testDebugUnitTest"

jacoco {
    toolVersion "0.7.7.201606060606"
}

task "${testTaskName}Coverage"(type: JacocoReport, dependsOn: ["${testTaskName}", "createDebugCoverageReport"]) {

    group = "Reporting"
    description = "Generate Jacoco coverage reports on the ${testTaskName} build."

    reports {
        xml.enabled = true
        html.enabled = true
        html.destination file("$buildDir/jacoco")
        csv.enabled false
    }

    def coverageSourceDirs = ["src/main/java"]

    def fileFilter = [
            '**/R.class',
            '**/R$*.class',
            '**/*$ViewInjector*.*',
            '**/*$ViewBinder*.*',
            '**/BuildConfig.*',
            '**/Manifest*.*',
            'android/**',
            'com/google/**',
            'com/intellij/**',
            'junit/**',
            'net/**',
            'okhttp/**',
            'org/**',
            'rx/**',
            '**/*_MembersInjector.class',
            '**/Dagger*Component.class', // covers component implementations
            '**/Dagger*Component$Builder.class', // covers component builders
            '**/*Module_*Factory.class'
    ]

    def javaClasses = fileTree(
            dir: "$buildDir/intermediates/app_classes/${taskName}",
            excludes: fileFilter
    )

    classDirectories = files([javaClasses])
    sourceDirectories = files([coverageSourceDirs])
    additionalSourceDirs = files([coverageSourceDirs])
    executionData = fileTree(dir: "$buildDir", includes: [
            "jacoco/testDebugUnitTest.exec"
    ])
}

and I am running following command on mac

./gradlew clean testDebugUnitTestCoverage 

When I run this command, it shows message

Task ':app:testDebugUnitTestCoverage' is not up-to-date because:
  Output property 'reports.enabledDirectoryReportDestinations.html' file .../app/build/jacoco/index.html has been removed.
  Output property 'reports.enabledDirectoryReportDestinations.html' file .../app/build/jacoco/jacoco-sessions.html has been removed.
  Output property 'reports.enabledDirectoryReportDestinations.html' file .../app/build/jacoco/jacoco-resources has been removed.
[ant:jacocoReport] Loading execution data file .../app/build/jacoco/testDebugUnitTest.exec
[ant:jacocoReport] Writing bundle 'app' with 3040 classes

and no coverage gets calculated in folder

reports/coverage/debug

however folder

tests/testDebugUnitTest

shows passed unit tests data with success.

1
  • Your jacoco.gradle clearly states html.destination file("$buildDir/jacoco") and your log clearly shows .../app/build/jacoco/index.html, so why do you look/expect something in reports/coverage/debug ? Commented Dec 13, 2018 at 12:40

2 Answers 2

2

I found solution here. Just change path of classes directory

def javaClasses = fileTree(
            dir: "$buildDir/intermediates/javac/debug",
            excludes: fileFilter
    )
Sign up to request clarification or add additional context in comments.

Comments

1

I wrote an article a while ago about jacoco. https://github.com/uriel-frankel/android-code-coverage/ The jacoco version should change as well:

apply plugin: 'jacoco'
jacoco {
   toolVersion = '0.7.5.201505241946'
}

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.