How to run type of task in serial

I have a type of tasks which are android connected tests. We want to run them in serial and not in parallel. I read this link: Ability to limit parallelism for tasks that require shared resources · Issue #7047 · gradle/gradle · GitHub

tasks.withType<com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask>()

There are a couple of ideas to do it:

  • Have a mutex. I feel that this is not a good solution as i don’t tell gradle explicitely all info it needs to so the performance will be suboptimal.

  • Use shared build service. I see examples on how to build a plugin with shared build services, but i have here already a set of tasks from android gradle plugin so i don’t understand how i can wire this together

So what should the code be here?

tasks.withType<com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask>() {
?
}

Unfortunately the build DSL usage is missing from the Shared Build Services documentation. You want to use Task.usesService.

def myBuildService = gradle.sharedServices.registerIfAbsent( 'myService', BuildService ) {
    maxParallelUsages = 1
}
tasks.withType( com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask ).configureEach {
    usesService( myBuildService )
}

NOTE: I used configureEach to take advantage of task configuration avoidance, it is not a required component of the build service solution.

1 Like