@@ -11,6 +11,7 @@ import org.gradle.api.GradleException
11
11
import org.gradle.api.NamedDomainObjectContainer
12
12
import org.gradle.api.Plugin
13
13
import org.gradle.api.Project
14
+ import org.gradle.api.Task
14
15
import org.gradle.api.tasks.TaskProvider
15
16
import org.gradle.api.tasks.testing.Test
16
17
import org.gradle.testing.jacoco.plugins.JacocoPlugin
@@ -64,7 +65,7 @@ class RootCoveragePlugin : Plugin<Project> {
64
65
}
65
66
66
67
private fun createCoverageTaskForRoot (project : Project ) {
67
- val task = project.createJacocoReportTask(
68
+ val rootCoverageTask = project.createJacocoReportTask(
68
69
taskName = " rootCoverageReport" ,
69
70
taskGroup = " reporting" ,
70
71
taskDescription = " Generates a Jacoco report with combined results from all the subprojects." ,
@@ -76,11 +77,12 @@ class RootCoveragePlugin : Plugin<Project> {
76
77
}
77
78
78
79
// Configure the root task with sub-tasks for the sub-projects.
79
- task .project.subprojects.forEach {
80
+ rootCoverageTask .project.subprojects.forEach {
80
81
it.afterEvaluate { subProject ->
81
82
subProject.applyConfiguration()
82
83
}
83
- task.addSubProject(it)
84
+ rootCoverageTask.addSubProject(it)
85
+
84
86
createSubProjectCoverageTask(it)
85
87
}
86
88
@@ -136,47 +138,28 @@ class RootCoveragePlugin : Plugin<Project> {
136
138
}
137
139
138
140
private fun JacocoReport.addSubProjectVariant (subProject : Project , variant : Variant , buildType : BuildType ) {
139
- val name = variant.name.replaceFirstChar(Char ::titlecase)
141
+ val variantName = variant.name.replaceFirstChar(Char ::titlecase)
140
142
141
143
// Gets the relative path from this task to the subProject
142
144
val path = project.relativeProjectPath(subProject.path)
143
145
144
146
// Add dependencies to the test tasks of the subProject
145
147
if (rootProjectExtension.shouldExecuteUnitTests() && (buildType.enableUnitTestCoverage || buildType.isTestCoverageEnabled)) {
146
- dependsOn(" $path :test${name } UnitTest" )
148
+ dependsOn(" $path :test${variantName } UnitTest" )
147
149
}
148
150
149
- var runsOnGradleManagedDevices = false
151
+ val androidTestTask = getAndroidTestTask(subProject, variant)
152
+ val runsOnGradleManagedDevices = androidTestTask.runsOnGradleManagedDevices
150
153
151
154
if (rootProjectExtension.shouldExecuteAndroidTests() && (buildType.enableAndroidTestCoverage || buildType.isTestCoverageEnabled)) {
152
-
153
- // Attempt to run on instrumented tests, giving priority to the following devices in this order:
154
- // - A user provided Gradle Managed Device.
155
- // - All Gradle Managed Devices if any is available.
156
- // - All through ADB connected devices.
157
- val gradleManagedDevices = subProject.extensions.getByType(BaseExtension ::class .java).testOptions.managedDevices.devices
158
- if (rootProjectExtension.runOnGradleManagedDevices && ! rootProjectExtension.gradleManagedDeviceName.isNullOrEmpty()) {
159
- runsOnGradleManagedDevices = true
160
- dependsOn(" $path :${rootProjectExtension.gradleManagedDeviceName}${name} AndroidTest" )
161
- } else if (rootProjectExtension.runOnGradleManagedDevices && gradleManagedDevices.isNotEmpty()) {
162
- runsOnGradleManagedDevices = true
163
- dependsOn(" $path :allDevices${name} AndroidTest" )
164
- } else {
165
- dependsOn(" $path :connected${name} AndroidTest" )
166
- }
155
+ dependsOn(androidTestTask.taskPath)
167
156
} else {
168
157
// If this plugin should not run instrumented tests on it's own, at least make sure it runs after those tasks (if they are
169
158
// selected to run as well and exists).
170
159
//
171
160
// In theory we don't need to do this if `rootProjectExtension.includeAndroidTestResults` is false, so we could check that, but
172
161
// it also does not hurt.
173
-
174
- val executeAndroidTestsOnGradleManagedDevicesTask = project.tasks.findByPath(" $path :allDevices${name} AndroidTest" )
175
- if (executeAndroidTestsOnGradleManagedDevicesTask != null ) {
176
- // This task only exists if a Gradle Managed Device is configured, which may not be the case.
177
- mustRunAfter(" $path :allDevices${name} AndroidTest" )
178
- }
179
- mustRunAfter(" $path :connected${name} AndroidTest" )
162
+ mustRunAfter(androidTestTask.taskPath)
180
163
}
181
164
182
165
sourceDirectories.from(variant.sources.java?.all)
@@ -200,6 +183,41 @@ class RootCoveragePlugin : Plugin<Project> {
200
183
)
201
184
}
202
185
186
+ private class AndroidTestTask (
187
+ val taskPath : String ,
188
+ val runsOnGradleManagedDevices : Boolean
189
+ )
190
+
191
+ private fun JacocoReport.getAndroidTestTask (subProject : Project , variant : Variant ): AndroidTestTask {
192
+ // Gets the relative path from this task to the subProject
193
+ val path = project.relativeProjectPath(subProject.path)
194
+ val variantName = variant.name.replaceFirstChar(Char ::titlecase)
195
+
196
+
197
+ // Attempt to run on instrumented tests, giving priority to the following devices in this order:
198
+ // - A user provided Gradle Managed Device.
199
+ // - All Gradle Managed Devices if any is available.
200
+ // - All through ADB connected devices.
201
+ val gradleManagedDevices = subProject.extensions.getByType(BaseExtension ::class .java).testOptions.managedDevices.devices
202
+
203
+ if (rootProjectExtension.runOnGradleManagedDevices && ! rootProjectExtension.gradleManagedDeviceName.isNullOrEmpty()) {
204
+ return AndroidTestTask (
205
+ taskPath = " $path :${rootProjectExtension.gradleManagedDeviceName}${variantName} AndroidTest" ,
206
+ runsOnGradleManagedDevices = true
207
+ )
208
+ } else if (rootProjectExtension.runOnGradleManagedDevices && gradleManagedDevices.isNotEmpty()) {
209
+ return AndroidTestTask (
210
+ taskPath = " $path :allDevices${variantName} AndroidTest" ,
211
+ runsOnGradleManagedDevices = true
212
+ )
213
+ } else {
214
+ return AndroidTestTask (
215
+ taskPath = " $path :connected${variantName} AndroidTest" ,
216
+ runsOnGradleManagedDevices = false
217
+ )
218
+ }
219
+ }
220
+
203
221
/* *
204
222
* Apply configuration from [RootCoveragePluginExtension] to the project.
205
223
*/
0 commit comments