@@ -145,6 +145,9 @@ class RootCoveragePlugin : Plugin<Project> {
145145 if (rootProjectExtension.shouldExecuteUnitTests() && (buildType.enableUnitTestCoverage || buildType.isTestCoverageEnabled)) {
146146 dependsOn(" $path :test${name} UnitTest" )
147147 }
148+
149+ var runsOnGradleManagedDevices = false
150+
148151 if (rootProjectExtension.shouldExecuteAndroidTests() && (buildType.enableAndroidTestCoverage || buildType.isTestCoverageEnabled)) {
149152
150153 // Attempt to run on instrumented tests, giving priority to the following devices in this order:
@@ -153,16 +156,26 @@ class RootCoveragePlugin : Plugin<Project> {
153156 // - All through ADB connected devices.
154157 val gradleManagedDevices = subProject.extensions.getByType(BaseExtension ::class .java).testOptions.managedDevices.devices
155158 if (rootProjectExtension.runOnGradleManagedDevices && ! rootProjectExtension.gradleManagedDeviceName.isNullOrEmpty()) {
159+ runsOnGradleManagedDevices = true
156160 dependsOn(" $path :${rootProjectExtension.gradleManagedDeviceName}${name} AndroidTest" )
157161 } else if (rootProjectExtension.runOnGradleManagedDevices && gradleManagedDevices.isNotEmpty()) {
162+ runsOnGradleManagedDevices = true
158163 dependsOn(" $path :allDevices${name} AndroidTest" )
159164 } else {
160165 dependsOn(" $path :connected${name} AndroidTest" )
161166 }
162167 } else {
163168 // If this plugin should not run instrumented tests on it's own, at least make sure it runs after those tasks (if they are
164- // selected to run as well).
165- mustRunAfter(" $path :allDevices${name} AndroidTest" )
169+ // selected to run as well and exists).
170+ //
171+ // In theory we don't need to do this if `rootProjectExtension.includeAndroidTestResults` is false, so we could check that, but
172+ // 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+ }
166179 mustRunAfter(" $path :connected${name} AndroidTest" )
167180 }
168181
@@ -181,7 +194,8 @@ class RootCoveragePlugin : Plugin<Project> {
181194 executionData.from(
182195 subProject.getExecutionDataFileTree(
183196 includeUnitTestResults = rootProjectExtension.includeUnitTestResults && (buildType.enableUnitTestCoverage || buildType.isTestCoverageEnabled),
184- includeAndroidTestResults = rootProjectExtension.includeAndroidTestResults && (buildType.enableAndroidTestCoverage || buildType.isTestCoverageEnabled)
197+ includeConnectedDevicesResults = rootProjectExtension.includeAndroidTestResults && (buildType.enableAndroidTestCoverage || buildType.isTestCoverageEnabled) && ! runsOnGradleManagedDevices,
198+ includeGradleManagedDevicesResults = rootProjectExtension.includeAndroidTestResults && (buildType.enableAndroidTestCoverage || buildType.isTestCoverageEnabled) && runsOnGradleManagedDevices
185199 )
186200 )
187201 }
0 commit comments