What does Task.usesService(provider) do, and why is it needed when a task relies on a BuildService for shared state?
answer
- declares task → service dependency
- keeps service alive for task duration
- enables concurrency-constraint enforcement
- explicit beats inference (esp. worker actions)
- provider from registerIfAbsent
basics
~10 susesService tells Gradle a task depends on that service. It keeps the service alive while the task runs and lets Gradle order/limit access correctly. Without it, parallel cleanup or constraints can misbehave.
solid answer
~50 s`Task.usesService(provider)` declares an explicit dependency from a task onto a `BuildService`. It does two lifecycle-relevant things. First, it guarantees the service stays **alive and not closed** for the entire duration of the task — Gradle won't dispose the shared instance while a declared consumer is still running. Second, it lets Gradle enforce any **concurrency constraint** the service declares (its `maxParallelUsages`), throttling how many tasks touch the service at once. When you only obtain the value via the provider inside `doLast`, Gradle can usually infer the dependency, but the explicit `usesService` is the robust, recommended way — especially when the service is used through worker actions or when you want Gradle to account for it in scheduling. In short: referencing the provider gives you access; `usesService` makes the **task↔service lifecycle and ordering relationship** explicit so shared state is consumed safely under parallel execution.
code
kotlin · 7 linesval cache = gradle.sharedServices
.registerIfAbsent("cache", CacheService::class.java) {}
tasks.register("index") {
usesService(cache) // explicit consumption
doLast { cache.get().add("a") }
}go deeper
Just know it declares that a task uses the service so Gradle handles it correctly.
Explain it keeps the service alive for the task and helps Gradle schedule access.
Discuss inference limits (worker actions), lifetime-overlap guarantee, and concurrency-constraint enforcement.
Reason about correctness of parallel shared-state consumption at scale and when to mandate explicit usesService in plugin conventions.
## What usesService declares `tasks.named("x") { usesService(myServiceProvider) }` registers an explicit relationship: *this task consumes this build service*. The provider you pass is the `Provider<MyService>` returned from `registerIfAbsent`. ## Why it matters for shared state A `BuildService` is a **single shared instance** that tasks read and mutate. Under parallel execution, two things must hold for that to be safe: 1. **Lifetime overlap** — the service must not be disposed (its `close()` must not fire) while a task is still using it. Declaring `usesService` ties the service's lifetime to the task's, so Gradle keeps the instance available until the task completes. 2. **Constraint enforcement** — a service can cap concurrent use. When a task declares `usesService`, Gradle counts it against that cap and serializes/throttles accordingly. Without the declaration, Gradle has no signal to apply the constraint to that task. ## Inference vs. explicit declaration Gradle can sometimes *infer* the dependency: if a task's input property is the service `Provider` and it's read during execution, Gradle wires it up. But inference is not guaranteed in every access pattern — notably when the service is passed into a **worker action** or accessed indirectly. The explicit `usesService` removes the ambiguity and is the documented best practice. ```kotlin val cache = gradle.sharedServices .registerIfAbsent("cache", CacheService::class.java) {} tasks.register("build") { usesService(cache) // explicit task↔service link doLast { cache.get().put("k", "v") } } ``` ## The lifecycle takeaway For this topic — BuildService as a lifecycle hook for shared state — `usesService` is the knob that makes the **consume-while-alive** guarantee explicit. It is the difference between "the task happens to touch a shared object" and "Gradle knows the task owns a slice of the service's lifetime and schedules accordingly."
- Can Gradle ever infer the service dependency without usesService?Yes, when the service Provider is a task input read during execution. But inference is unreliable for indirect access (e.g. worker actions), so explicit usesService is recommended.
- What guarantee does usesService give about the service's close()?The service won't be disposed/closed while a declaring task is still running — its lifetime spans the task.
- How does usesService relate to limiting concurrent access?It lets Gradle count the task against the service's maxParallelUsages cap so it can throttle how many tasks use the service simultaneously.
- Claiming usesService is purely cosmetic / never needed.
- Assuming inference always works, including inside worker actions.
- Thinking usesService creates a copy of the service for the task.