Refresh or Redownload Dependencies in Gradle

This quick tutorial is going to cover how to refresh or redownload dependencies in Gradle. This is useful when the Gradle Dependency Cache is out of sync with the actual state of the configured repositories.

1. Refresh or Redownload Dependencies in Gradle

We can tell Gradle to refresh or redownload dependencies by several ways as below:

1.1. Use the –refresh-dependencies option on the command line

This is the simplest and easiest way. Let’s see an example:

The –refresh-dependencies option tells Gradle to ignore all cached entries for resolved modules and artifacts.

1.2. Remove all the cached files in the Gradle cache location

Another way to refresh or redownload dependencies in Gradle is to remove all the cached files in the Gradle Cache Location.

On Linux systems, we can remove all the cached files under  the ~/.gradle/caches directory. For example:

On Windows systems, we can remove cached files under:

Note that this way will take time because Gradle will need to redownload all dependencies not only for the current project but also for all projects.

1.3. Make use of Changing modules

Changing modules which can represent code that is under active development during the workday, or code that is being rebuilt nightly, are subject to change.  An example of this type of changing module is a Maven SNAPSHOT module, which always points at the latest artifact published. And Gradle caches changing modules for 24 hours by default.

So, we can tell Gradle to refresh or redownload dependencies in the build script by marking those as ‘changing’. Let’s see the following example:

Firstly, we tell Gradle not to cache changing modules by set the value of the cacheChangingModulesFor property to 0 second.

Secondly, we mark dependencies which are needed to be refreshed  or redownloaded, as changing module:

2. Force Gradle to use dependencies from the cache

We can use the –offline command line switch tells Gradle to always use dependencies from the cache. Here is an example:

3. Conclusion

The tutorial has illustrated us how to refresh or redownload dependencies in Gradle by three different ways. And we can see that the first way which forces Gradle to go to the network to re-evaluate and re-fetch all dependency metadata,  is the simplest and easiest way.

Here are other related tutorials for your references:

Install Gradle on Ubuntu 16.04 LTS (Xenial Xerus)

Install Gradle on Windows

Gradle Proxy Settings

Specify The Build File in Gradle

Convert Maven POM File to Gradle Build File

Gradle Tutorials

Using Gradlew, The Gradle Wrapper