How to clear gradle cache?
As @Bradford20000 pointed out in the comments, there might be a
gradle.properties file as well as global gradle scripts located under
$HOME/.gradle. In such case special attention must be paid when deleting the content of this directory.
.gradle/caches directory holds the
Gradle build cache. So if you have any error about build cache, you can delete it.
The --no-build-cache option will run gradle without the build cache.
Daemon on MS Windows
If you're on Windows, you'll need to kill the daemon before it allows you to clear those directories. See Kill all Gradle Daemons Regardless Version? for more info.
How to clear specific gradle cache files when running the Clean project command?
That "Clean project" menu item under the hood appears to do a couple of things (based on the output of the Gradle Console window when you click it):
- A gradle clean, the equivalent of calling
- Generate sources and dependencies for a debug build, including a mockable Android sources jar if needed.
I would make your task a dependency for the Gradle clean task, so that whenever the project is cleaned, this task is also invoked. This can be achieved by adding the line
clean.dependsOn clearLibCache in your
build.gradle after you declare the task.
How can I force gradle to redownload dependencies?
Generally, you can refresh dependencies in your cache with the command line option --refresh-dependencies. You can also delete the cached files under
~/.gradle/caches. With the next build Gradle would attempt to download them again.
What is your specific use case? Do you use dynamic dependency versions or SNAPSHOT versions?
On Unix systems, you can delete all the existing artifacts (artifacts and metadata) Gradle has downloaded using:
rm -rf $HOME/.gradle/caches/
Note that --refresh-dependencies won't always re-download every artifact; it will use existing copies if they match what exists in the repository. From the Gradle User Guide, refreshing dependencies:
The --refresh-dependencies option tells Gradle to ignore all cached entries for resolved modules and artifacts. A fresh resolve will be performed against all configured repositories, with dynamic versions recalculated, modules refreshed, and artifacts downloaded. However, where possible Gradle will check if the previously downloaded artifacts are valid before downloading again. This is done by comparing published SHA1 values in the repository with the SHA1 values for existing downloaded artifacts.
It’s a common misconception to think that using --refresh-dependencies will force download of dependencies. This is not the case: Gradle will only perform what is strictly required to refresh the dynamic dependencies. This may involve downloading new listing or metadata files, or even artifacts, but if nothing changed, the impact is minimal.
How to clear $HOME/.gradle/caches/ whenever I commit in git?
It seems your script is run after
git commit is done. Is it possible to clear the cache in your script? If not, it could be done in a
Touch a file named
post-commit in .git/hooks/ and make it executable. It could be implemented like this in bash
rm -rf $HOME/.gradle/cache/
By default, whenever a commit is made in the current repository, the hook is triggered.