• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import org.robolectric.gradle.RoboJavaModulePlugin
2
3apply plugin: RoboJavaModulePlugin
4apply plugin: "jacoco"
5
6def jacocoVersion = libs.versions.jacoco.get()
7
8jacoco {
9    toolVersion = jacocoVersion
10}
11
12configurations {
13    jacocoAnt
14    jacocoRuntime
15}
16
17dependencies {
18    testCompileOnly AndroidSdk.MAX_SDK.coordinates
19    testRuntimeOnly AndroidSdk.MAX_SDK.coordinates
20
21    testImplementation project(":robolectric")
22    testImplementation libs.junit4
23    testImplementation "org.jacoco:org.jacoco.agent:$jacocoVersion:runtime"
24}
25
26def unitTestTaskName = "test"
27
28def compileSourceTaskName = "classes"
29
30def javaDir = layout.buildDirectory.dir("classes/java/main").get().asFile
31
32def kotlinDir = layout.buildDirectory.dir("classes/kotlin/main").get().asFile
33
34def jacocoInstrumentedClassesOutputDir = layout.buildDirectory.dir("$jacocoVersion/classes/java/classes-instrumented").get().asFile
35
36// make sure it's evaluated after the AGP evaluation.
37afterEvaluate {
38    tasks.named(compileSourceTaskName).configure { task ->
39        task.doLast {
40            println "[JaCoCo]:Generating JaCoCo instrumented classes for the build."
41
42            if (jacocoInstrumentedClassesOutputDir.exists()) {
43                println "[JaCoCo]:Classes had been instrumented."
44                return
45            }
46
47            ant.taskdef(name: 'instrument',
48                    classname: 'org.jacoco.ant.InstrumentTask',
49                    classpath: configurations.jacocoAnt.asPath)
50
51            if (javaDir.exists()) {
52                ant.instrument(destdir: jacocoInstrumentedClassesOutputDir.path) {
53                    fileset(
54                            dir: javaDir.path,
55                            excludes: []
56                    )
57                }
58            } else {
59                println "Classes directory with path: $javaDir does not existed."
60            }
61
62            if (kotlinDir.exists()) {
63                ant.instrument(destdir: jacocoInstrumentedClassesOutputDir.path) {
64                    fileset(
65                            dir: kotlinDir.path,
66                            excludes: []
67                    )
68                }
69            } else {
70                println "Classes directory with path: $kotlinDir does not existed."
71            }
72        }
73    }
74
75    def executionDataFilePath = layout.buildDirectory.dir("jacoco").get().file("${unitTestTaskName}.exec").getAsFile().path
76
77    // put JaCoCo instrumented classes and JaCoCoRuntime to the beginning of the JVM classpath.
78    tasks.named(unitTestTaskName).configure { task ->
79        task.doFirst {
80            jacoco {
81                // disable JaCoCo on-the-fly from Gradle JaCoCo plugin.
82                enabled = false
83            }
84
85            println "[JaCoCo]:Modifying classpath of tests JVM."
86
87            systemProperty 'jacoco-agent.destfile', executionDataFilePath
88
89            classpath = files(jacocoInstrumentedClassesOutputDir.path) + classpath + configurations.jacocoRuntime
90
91            println "Final test JVM classpath is ${classpath.getAsPath()}"
92        }
93    }
94}
95