-
Notifications
You must be signed in to change notification settings - Fork 32
/
jacoco.gradle
98 lines (84 loc) · 2.96 KB
/
jacoco.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
apply plugin: JacocoPlugin
final String JACOCO_FULL_REPORT_TASK = "jacocoFullReport"
tasks.withType(Test) {
testLogging {
events "FAILED"
exceptionFormat "full"
}
}
task(JACOCO_FULL_REPORT_TASK) {
it.group 'reporting'
}
Task jacocoTestReportTask = findOrCreateJacocoTestReportTask()
getVariants().all { variant ->
JacocoReport reportTask = createReportTask(variant)
jacocoTestReportTask.dependsOn reportTask
}
if (project.tasks.findByName(JACOCO_FULL_REPORT_TASK)) {
project.tasks."$JACOCO_FULL_REPORT_TASK".dependsOn jacocoTestReportTask
} else {
project.tasks.whenTaskAdded {
if (it.name.contentEquals(JACOCO_FULL_REPORT_TASK)) {
it.dependsOn jacocoTestReportTask
}
}
}
def findOrCreateJacocoTestReportTask() {
Task jacocoTestReportTask = project.tasks.findByName("jacocoTestReport")
if (!jacocoTestReportTask) {
jacocoTestReportTask = project.task("jacocoTestReport") {
group = "reporting"
}
}
return jacocoTestReportTask
}
def getVariants() {
if (project.android.hasProperty('libraryVariants')) {
return project.android.libraryVariants
} else {
return project.android.applicationVariants
}
}
def createReportTask(def variant) {
def sourceDirs = sourceDirs(variant)
def classesDir = classesDir(variant)
def testTask = testTask(variant)
def executionData = executionDataFile(testTask)
return project.task("jacoco${testTask.name.capitalize()}Report", type: JacocoReport) { JacocoReport reportTask ->
reportTask.dependsOn testTask
reportTask.group = "reporting"
reportTask.description = "Generates Jacoco coverage reports for the ${variant.name} variant."
reportTask.executionData = project.files(executionData)
reportTask.sourceDirectories = project.files(sourceDirs)
reportTask.classDirectories = classesDir
reportTask.reports {
csv.enabled false
html.enabled true
xml.enabled true
}
}
}
def sourceDirs(variant) {
variant.sourceSets.java.srcDirs.collect { it.path }.flatten()
}
def classesDir(variant) {
def filters = ['**/R.class',
'**/R$*.class',
'**/BuildConfig.class',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/Manifest*.*',
'**/*Dagger*.*',
'**/*MembersInjector*.*',
'**/*_Provide*Factory*.*',
'**/*_Factory*.*' ]
def javaTree = fileTree(dir: variant.javaCompile.destinationDir, excludes: filters)
def kotlinTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/${variant}", excludes: filters)
files([ javaTree ], [ kotlinTree ])
}
def testTask(variant) {
project.tasks.withType(Test).find { task -> task.name =~ /test${variant.name.capitalize()}UnitTest/ }
}
def executionDataFile(Task testTask) {
testTask.jacoco.destinationFile.path
}