How do @OutputFile and @OutputDirectory participate in up-to-date checks, and why does declaring outputs matter beyond just naming the result?
answer
- outputs are fingerprinted too
- no outputs ⇒ never UP-TO-DATE
- output provider ⇒ auto dependsOn
- stale-output cleanup
- overlapping outputs hurt caching
basics
~10 s@OutputFile/@OutputDirectory tell Gradle what a task produces. Gradle fingerprints outputs too, so it detects manual deletion or tampering and reruns. Declared outputs also let Gradle wire task dependencies and clean stale outputs.
solid answer
~40 sDeclaring outputs with `@OutputFile`/`@OutputDirectory` does three jobs. First, **up-to-date safety**: Gradle stamps and fingerprints outputs after a run, so if someone deletes or edits an output between builds, the fingerprint mismatches and the task reruns instead of falsely staying UP-TO-DATE. Second, **dependency wiring**: when a `Provider` returned from one task's `@OutputFile` is used as another task's `@InputFile`, Gradle infers the task dependency automatically — no manual `dependsOn`. Third, **stale-output cleanup**: because Gradle knows what a task owns, it can remove orphaned outputs (e.g. when a source file is deleted) and detect *overlapping outputs* where two tasks write the same location, which degrades incrementality and caching. A task with declared inputs but **no** declared outputs can never be UP-TO-DATE — Gradle has nothing to compare — so it always reruns.
code
kotlin · 7 linesval generate = tasks.register<GenerateTask>("generate") {
out.set(layout.buildDirectory.file("gen/Config.kt")) // @OutputFile
}
val compile = tasks.register<CompileTask>("compile") {
source.set(generate.flatMap { it.out }) // input wired from output
}
// Gradle infers: compile dependsOn generate, no manual wiring neededgo deeper
Know @OutputFile/@OutputDirectory declare what a task produces and that Gradle uses them to skip unchanged work.
Explain that outputs are fingerprinted (manual deletion forces a rerun) and that no-output tasks always run.
Connect output declarations to automatic dependency inference via Providers and to stale-output cleanup.
Treat overlapping/undeclared outputs as architectural defects across modules; standardize per-task output layout and validation in CI.
## Outputs are first-class, not labels `@OutputFile` and `@OutputDirectory` declare the artifacts a task produces. This is far more than documentation: ### 1. Up-to-date includes outputs, not just inputs Gradle fingerprints declared outputs after execution. On the next build it checks **both** that inputs are unchanged **and** that outputs still match what it stamped. So: - Delete an output file by hand → fingerprint mismatch → task reruns and regenerates it. - Hand-edit a generated file → mismatch → task reruns, overwriting your edit. This is why a task with no declared outputs can *never* be UP-TO-DATE: there's no output state to validate, so Gradle must run it every time. ### 2. Automatic dependency inference via Providers Modern Gradle uses lazy `Provider`/`Property` wiring. If task B's input is set from task A's output provider, Gradle adds the dependency for you: ```kotlin val generate = tasks.register<GenerateTask>("generate") { out.set(layout.buildDirectory.file("gen/Config.kt")) } val compile = tasks.register<CompileTask>("compile") { source.set(generate.flatMap { it.out }) // B reads A's @OutputFile } // No explicit dependsOn — Gradle infers compile dependsOn generate ``` ### 3. Stale-output cleanup and overlapping outputs Because Gradle knows exactly which files a task owns, it can delete *orphaned* outputs when an input disappears. It also warns about **overlapping outputs** — two tasks writing the same directory — which forces conservative, less-incremental behavior and can disable caching for those outputs. ## @OutputFiles / @OutputDirectories For multiple outputs there are plural forms (`@OutputFiles`, `@OutputDirectories`), typically `Map`- or collection-valued, used when a single task emits several named artifacts. ## Why this matters in practice Correct output declarations are what make 'delete `build/` and rebuild' reliable, make task graphs self-wiring, and keep generated artifacts from being silently stale. Outputs without inputs (or vice versa) signal a modeling gap.
- Why can a task with inputs but no declared outputs never be reported UP-TO-DATE?Up-to-date requires comparing the current output state against a stored snapshot. With no declared outputs there is nothing to snapshot or compare, so Gradle cannot prove the result still exists and must rerun the task every build.
- What problem do 'overlapping outputs' cause?When two tasks write to the same location Gradle can't cleanly attribute files to one task, so it falls back to conservative behavior — it may disable up-to-date/caching optimizations for those outputs and emits a warning. Each task should own a distinct output location.
- How does declaring an output enable removing a manual dependsOn?If a consumer task's input Property is set from a producer task's output Provider, Gradle records an implicit task dependency from the provider's producing task, so the consumer automatically runs after the producer.
- Claiming outputs are only labels and don't affect up-to-date — they're fingerprinted and validated.
- Saying a task without outputs can still be UP-TO-DATE.
- Letting two tasks write the same directory without realizing it harms incrementality.