This repository has been archived by the owner on Aug 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
jacoco-unit-tests.gradle
79 lines (71 loc) · 2.52 KB
/
jacoco-unit-tests.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
//
// Adds tasks to create unit test coverage reports for all existing flavors in a module.
// Adapted to work on multi-dimension build variants.
//
// Original source: https://blog.gouline.net/2015/06/23/code-coverage-on-android-with-jacoco/
//
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.7.6.201602180812"
}
project.afterEvaluate {
def buildTypes = android.buildTypes.collect { type -> type.name }
def productVariants = []
if (android.hasProperty("applicationVariants")) {
productVariants = android.applicationVariants.collect { variant ->
String variantName = variant.productFlavors[0].name
for (int i = 1; i < variant.productFlavors.size(); ++i) {
variantName += variant.productFlavors[i].name.capitalize()
}
return variantName
}
} else if (android.hasProperty("libraryVariants")) {
productVariants = android.libraryVariants.collect { variant ->
String variantName = variant.productFlavors[0].name
for (int i = 1; i < variant.productFlavors.size(); ++i) {
variantName += variant.productFlavors[i].name.capitalize()
}
return variantName
}
}
if (!productVariants) productVariants.add('')
productVariants.each { productVariantName ->
buildTypes.each { buildTypeName ->
def sourceName = "${buildTypeName}"
if (productVariantName) {
sourceName = "${productVariantName}${sourceName.capitalize()}"
}
def targetName = "${sourceName.capitalize()}"
def testTaskName = "test${sourceName.capitalize()}UnitTest"
def coverageTaskName = "${testTaskName}Coverage"
if (project.tasks.findByPath(testTaskName) && !project.tasks.findByPath(coverageTaskName)) {
task "$coverageTaskName"(type: JacocoReport, dependsOn: "$testTaskName") {
group = "Reporting"
description = "Generate Jacoco coverage reports on the ${targetName} build."
classDirectories = fileTree(
dir: "${project.buildDir}/intermediates/classes/${sourceName}",
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/*_Provide*Factory.*',
'**/*_MembersInjector.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)
def coverageSourceDirs = [
"src/main/java",
"src/$productVariantName/java",
"src/$buildTypeName/java"
]
additionalSourceDirs = files(coverageSourceDirs)
sourceDirectories = files(coverageSourceDirs)
executionData = files("${project.buildDir}/jacoco/${testTaskName}.exec")
reports {
xml.enabled = true
html.enabled = true
}
}
}
}
}
}