-
Notifications
You must be signed in to change notification settings - Fork 4
/
gradle-jacoco-android.gradle
151 lines (122 loc) · 4.29 KB
/
gradle-jacoco-android.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
class JacocoInstrument extends DefaultTask {
def buildType = "debug"
def flavorName = ""
@InputFiles
FileCollection jacocoClasspath
@TaskAction
def taskAction() {
ant.taskdef(name: 'instrument', classname: 'org.jacoco.ant.InstrumentTask', classpath: jacocoClasspath.asPath)
def variantPath
if (flavorName.length() > 0) {
variantPath = "$flavorName/$buildType"
} else {
variantPath = "$buildType"
}
def buildDir = project.buildDir
def instrumentedClassDir = "$buildDir/generated-classes/jacoco/$variantPath"
def backupClassDir = "$buildDir/generated-classes/jacoco/$variantPath" + "Backup"
def classDir = "$buildDir/classes/$variantPath"
ant.instrument(destdir: instrumentedClassDir) {
fileset(dir: classDir)
}
project.copy {
from classDir
into backupClassDir
}
project.delete classDir
project.copy {
from instrumentedClassDir
into classDir
}
}
}
class JacocoRestore extends DefaultTask {
def buildType = "debug"
def flavorName = ""
@TaskAction
def taskAction() {
def variantPath
if (flavorName.length() > 0) {
variantPath = "$flavorName/$buildType"
} else {
variantPath = "$buildType"
}
def buildDir = project.buildDir
def instrumentedClassDir = "$buildDir/generated-classes/jacoco/$variantPath"
def backupClassDir = "$buildDir/generated-classes/jacoco/$variantPath" + "Backup"
def classDir = "$buildDir/classes/$variantPath"
project.copy {
from backupClassDir
into classDir
}
}
}
class JacocoPullResult extends DefaultTask {
def buildType = "debug"
def flavorName = ""
def packageName = ""
@TaskAction
def taskAction() {
def variantPath
if (flavorName.length() > 0) {
variantPath = "$flavorName/$buildType"
} else {
variantPath = "$buildType"
}
def buildDir = project.buildDir
def jacocoResultPath = "$buildDir/jacoco/$variantPath"
new File(jacocoResultPath).mkdirs()
def proc = ("adb pull /sdcard/$packageName" + "jacoco.exec $jacocoResultPath/jacoco.exec").execute()
proc.in.eachLine { line -> println line }
proc.err.eachLine { line -> println 'RESULT: ' + line }
proc.waitFor()
}
}
allprojects {
afterEvaluate { project ->
def variants = project.android.applicationVariants
variants.all { variant ->
def log = project.logger
def javaCompile = variant.javaCompile;
if (variant.buildType.name.equals("release")) {
log.info("Skipping release build type.")
return;
}
def variantName = "$variant.name".capitalize()
def variantNameForTest
if (variant.flavorName != null && variant.flavorName.length() > 0) {
variantNameForTest = variantName
} else {
variantNameForTest = ""
}
def jacocoTask = tasks.create("jacoco" + "$variant.name".capitalize())
log.info("apply jacoco instrument task to debug tasks. $variant.buildType.name")
def taskName = "jacocoInstrument" + "$variantName"
def jacocoInstrumentTask = tasks.create(taskName, JacocoInstrument)
jacocoInstrumentTask.jacocoClasspath = project.configurations["jacocoAnt"]
jacocoInstrumentTask.flavorName = variant.flavorName
def jacocoRestoreTask = tasks.create("jacocoRestore" + "$variantName", JacocoRestore)
jacocoRestoreTask.flavorName = variant.flavorName
def jacocoPullResultTask = tasks.create("jacocoPullResult" + "$variantName", JacocoPullResult)
jacocoPullResultTask.flavorName = variant.flavorName
jacocoPullResultTask.packageName = variant.packageName
tasks["compile$variantName" + "Java"].doLast {
log.info("JACOCO inject instrument task info " + name)
if (gradle.taskGraph.hasTask(jacocoTask)) {
jacocoInstrumentTask.execute()
}
}
// for backward compatibility gradle android plugin 0.8.+
def connectedTest = project.configurations.getNames().contains("androidTestCompile") ?
project.tasks.getByName("connectedAndroidTest$variantNameForTest") :
project.tasks.getByName("connectedInstrumentTest$variantNameForTest")
connectedTest.doFirst {
jacocoRestoreTask.execute()
}
connectedTest.doLast {
jacocoPullResultTask.execute()
}
jacocoTask.dependsOn(connectedTest)
}
}
}