Gradle Build Cache Causes Stale Artifacts
Gradle build cache contains stale artifacts from previous builds. Changes to source files aren't reflected in the compiled output, causing deploying of old code. Incremental builds work correctly, but CI/CD pipeline with partial cache hits behaves unexpectedly.
This is particularly problematic when generated code or resource files are cached but source changed.
Error Messages You Might See
Common Causes
- Build cache not invalidated when dependency versions change
- Task outputs cached incorrectly without accounting for input file changes
- Resource files or generated code cached with wrong invalidation rules
- Gradle daemon holding stale classpath in memory
- CI/CD pipeline using shared cache across builds with different configurations
How to Fix It
Disable cache for CI/CD: ./gradlew clean build --no-build-cache. Or invalidate selectively: ./gradlew cleanBuild. Add @CacheableTask annotations carefully with correct inputs/outputs. Kill Gradle daemon: ./gradlew --stop. In CI, prefer clean builds over cached builds for reliability.
Real developers can help you.
You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.
Get HelpFrequently Asked Questions
Should build cache be used in CI?
Use with caution. For reliability, prefer: ./gradlew clean build in CI. Cache is useful locally for fast iteration.
How to completely clear the build cache?
Run: ./gradlew clean && rm -rf .gradle/build-cache/. Then rebuild: ./gradlew build
Why does CI cache behave differently than local?
Local cache persists across builds and daemon is reused. CI typically starts fresh. Use --no-build-cache in CI if experiencing issues.