Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JaCoCo gradle plugin ignoring version

Gradle Version: 6.1.1

Android Gradle Plugin: 4.0.0

When trying to run my jacoco coverage it does not appear to be taking into account the version. When I look at the HTML report it states "Created with JaCoCo 0.7.9.201702052155" despite me having toolVersion = "0.8.5" in my setup. The report also does not have fixes that I expect in 0.8.3 relating to Kotlin as a secondary confirmation this isn't working.

apply plugin: 'jacoco'

jacoco {
    toolVersion = "0.8.5"
    reportsDir = file("$buildDir/reports")
}

tasks.withType(Test) {
    jacoco.includeNoLocationClasses = true
    jacoco.excludes = ['jdk.internal.*']
}

project.afterEvaluate {
    (android.hasProperty('applicationVariants')
            ? android.'applicationVariants'
            : android.'libraryVariants').all { variant ->
        def variantName = variant.name
        def unitTestTask = "test${variantName.capitalize()}UnitTest"
        def uiTestCoverageTask = "create${variantName.capitalize()}CoverageReport"

        tasks.create(name: "${unitTestTask}Coverage", type: JacocoReport, dependsOn: [
                "$unitTestTask",
                "$uiTestCoverageTask"
        ]) {
            group = "Reporting"
            description = "Generate Jacoco coverage reports for the ${variantName.capitalize()} build"

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

            def fileFilter = [
                    '**/R.class',
                    '**/R$*.class',
                    '**/BuildConfig.*',
                    '**/Manifest*.*',
                    '**/*Test*.*',
                    '**/com/example/databinding/*',
                    '**/com/example/generated/callback/*',
                    '**/android/databinding/*',
                    '**/androidx/databinding/*',
                    '**/di/module/*',
                    '**/*MapperImpl*.*',
                    '**/*$ViewInjector*.*',
                    '**/*$ViewBinder*.*',
                    '**/BuildConfig.*',
                    '**/*Component*.*',
                    '**/*BR*.*',
                    '**/Manifest*.*',
                    '**/*$Lambda$*.*',
                    '**/*Companion*.*',
                    '**/*Module.*',
                    '**/*Dagger*.*',
                    '**/*MembersInjector*.*',
                    '**/*_Factory*.*',
                    '**/*_Provide*Factory*.*',
                    '**/*Extensions*.*',
                    '**/*$Result.*', /* filtering `sealed` and `data` classes */
                    '**/*$Result$*.*'/* filtering `sealed` and `data` classes */
            ]

            classDirectories.setFrom(files([
                    fileTree(dir: "${buildDir}/tmp/kotlin-classes/${variantName}", excludes: fileFilter)
            ]))

            def coverageSourceDirs = [
                    "$project.rootDir/app/src/main/java",
                    "$project.projectDir/src/${variantName}/java"
            ]
            additionalSourceDirs.setFrom(files(coverageSourceDirs))
            sourceDirectories.setFrom(files(coverageSourceDirs))

            def uiTestsData = fileTree(dir: "${buildDir}/outputs/code_coverage/${variantName}AndroidTest/connected/", includes: ["**/*.ec"])

            executionData(files([
                    "$project.buildDir/jacoco/${unitTestTask}.exec",
                    uiTestsData
            ]))
        }
    }
}
like image 917
Josh Feinberg Avatar asked Sep 18 '25 17:09

Josh Feinberg


1 Answers

As stated from @Bilgehan KALKAN here, as of Gradle 7, this seems to be resolved.

android {
    //Other configurations...
    testCoverage.jacocoVersion = "0.8.7"
}

For lower gradle version try adding this to your Project Gradle file.

subprojects {
    configurations.all {
        resolutionStrategy {
            eachDependency { details ->
                if ('org.jacoco' == details.requested.group) {
                    details.useVersion "0.8.6"
                }
            }
        }
    }
}
like image 176
LethalMaus Avatar answered Sep 21 '25 06:09

LethalMaus