Jacoco Offline instrumentation Android Script

52 views
Skip to first unread message

Sambuddha Dhar

unread,
Jan 17, 2023, 1:10:09 AM1/17/23
to JaCoCo and EclEmma Users
Hi Team,

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"(type: JacocoReport) {
                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")
                        xml(destdir: "$buildDir.path/reports/jacoco")
                    }
                }

            }
        }
    }

}

I am running - ./gradlew testProdUnitTestReport
The instrumented class folder is created properly. But I am not able to get the jacoco coverage report(xml/html) inside the /build/reports/jacoco folder. Please help me by pointing out what I am doing wrong here. Thanks in advance

Bets Regards,
Sambuddha Dhar


Marc Hoffmann

unread,
Jan 17, 2023, 4:57:05 AM1/17/23
to JaCoCo and EclEmma Users
Hi,

first of all please note that neither the Grade integration nor the Android SDK JaCoCo integration is developer by us. You might better check with those projects.

> But I am not able to get the jacoco coverage report(xml/html) inside the /build/reports/jacoco folder.

So, do you get any error message? What exactly is the build log output report task?

Cheers,
-marc



--
You received this message because you are subscribed to the Google Groups "JaCoCo and EclEmma Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jacoco+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/63c9d9da-2626-4d1a-b28d-b463153540c1n%40googlegroups.com.

Sambuddha Dhar

unread,
Jan 18, 2023, 3:41:44 AM1/18/23
to JaCoCo and EclEmma Users
Hi, 
It was my mistake. I solved it and now I am able to get the coverage report. One thing I discovered is: for classes which uses Kotlin object shows 0 coverage. 

object B {
  const val ABC = 1
}

class A(c: C) {
 fun logic1() {
   var a = c.getValue(B.ABC)
}

Now class A always shows 0 coverage.

Best Regards,
Sambuddha Dhar

Reply all
Reply to author
Forward
0 new messages