0

I am trying to use Jacoco Offline instrumentation in my android project to get coverage with PowerMockito. Following is my build.gradle.

apply plugin: 'jacoco'

configurations {
    jacocoAnt
    jacocoRuntime
}

jacoco {
    toolVersion = '0.8.8'
}

dependencies {
    jacocoAnt group: 'org.jacoco', name: 'org.jacoco.ant', version: '0.8.8', classifier: 'nodeps'
    jacocoRuntime group: 'org.jacoco', name: 'org.jacoco.agent', version: '0.8.8', classifier: 'runtime'
}

//def testTaskName = "test${sourceName.capitalize()}UnitTest"

project.afterEvaluate {
    // Grab all build types and product flavors
    def buildTypes = android.buildTypes.collect { type -> type.name }
    def productFlavors = android.productFlavors.collect { flavor -> flavor.name }

    // When no product flavors defined, use empty
    if (!productFlavors) productFlavors.add('')

    productFlavors.each { productFlavorName ->
        buildTypes.each { buildTypeName ->
            def sourceName, sourcePath
            if (!productFlavorName) {
                sourceName = sourcePath = "${buildTypeName}"
            } else {
                sourceName = "${productFlavorName}${buildTypeName.capitalize()}"
                sourcePath = "${productFlavorName}/${buildTypeName}"
            }
            def testTaskName = "test${sourceName.capitalize()}UnitTest"

            def excludes = ['**/R.class',
                            ......]


            task "${testTaskName}Instrument"() {
                ext.outputDir = buildDir.path + '/classes-instrumented'
                doLast {
                    ant.taskdef(name: 'instrument',
                            classname: 'org.jacoco.ant.InstrumentTask',
                            classpath: configurations.jacocoAnt.asPath)
                    ant.instrument(destdir: outputDir) {
                        fileset(dir: "${project.buildDir}/intermediates/javac", excludes: excludes)
                        fileset(dir: "${project.buildDir}/tmp/kotlin-classes", excludes: excludes)
                    }
                    testProdUnitTest.classpath = files(outputDir) + testProdUnitTest.getClasspath()
                }

            }

            task "${testTaskName}Report"(dependsOn: ["${testTaskName}Instrument"]) {
                doLast {
                    ant.taskdef(name: 'report',
                            classname: 'org.jacoco.ant.ReportTask',
                            classpath: configurations.jacocoAnt.asPath)
                    ant.report() {
                        executiondata {
                            ant.file(file: "$buildDir.path/jacoco/testProdUnitTest.exec")
                        }
                        structure(name: 'Example') {
                            classfiles {
                                fileset(dir: "${project.buildDir}/intermediates/javac", excludes: excludes)
                                fileset(dir: "${project.buildDir}/tmp/kotlin-classes", excludes: excludes)
                            }
                            sourcefiles {
                                fileset(dir: 'src/main/java')
                            }
                        }
                        html(destdir: "$buildDir.path/reports/jacoco")
                    }
                }

            }
        }
    }
}

The instrumented class folder is created properly. But still for some classes jacoco shows 0 coverage. Please help me by pointing out what I am doing wrong here. Thanks in advance

Dharsam1990
  • 121
  • 10

0 Answers0