985

How can I tell Gradle to redownload dependencies from repositories?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
fedor.belov
  • 22,343
  • 26
  • 89
  • 134

26 Answers26

1067

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.

outis
  • 75,655
  • 22
  • 151
  • 221
Benjamin Muschko
  • 32,442
  • 9
  • 61
  • 82
  • 42
    I can't speak for the OP, but my specific use case is to test whether my configuration of a non-MavenCentral repository actually works. – Emil Lundberg Jun 24 '13 at 08:15
  • 8
    You also need to remove ~/.m2 directory (if exists). If you have configured maven repo few of those artifacts get downloaded to ~/.m2 too. Better to remove both ~/.gradle and ~/.m2 to start on clean slate. – Gopinath M.R Jan 14 '14 at 21:05
  • 19
    Maven Local is only relevant if your build defines it as a repository. – Benjamin Muschko Jan 16 '14 at 21:21
  • 1
    Hey how would your answer differ if there was a specific use case, e.g. SNAPSHOT or nightly? – avanderw Mar 07 '14 at 13:32
  • 2
    @avanderw Then you might want to solve this programmatically instead of using this command line option. – Benjamin Muschko Mar 07 '14 at 15:16
  • 1
    I cleared ~/.gradle/caches, specific dependency and android studio nor gradlew did not download dependency. Did this and it worked! Thank you, saved me couple hours of pain. – Volodymyr Lykhonis Feb 11 '15 at 17:20
  • I see this as a general issue with Android Studio. Have the kids at Google resolved this in a recent release of AS ? (I'm using 1.2.1.1 atm) – angryITguy Oct 15 '15 at 04:35
  • 33
    @Gopinath that is dangerous advice, as .m2 can contain a maven setting file. I guess you mean delete .m2/repository – Ward Nov 05 '15 at 08:28
  • 21
    `find $HOME/.gradle/caches/ -name "*LIBRARY_NAME*" -exec rm -r {} \; ` – fangzhzh Jan 08 '16 at 03:42
  • 2
    get the backup of cache folder in case if anything goes wrong later. – Mangesh Pawar Mar 25 '19 at 11:02
  • where to add the option? The link is dead. – WesternGun Jan 07 '20 at 09:40
  • @BenjaminMuschko plz help me.. In my case gradle is unable to download any new dependency. Saying could not able to resolve all classfiles and also something about repo.maven.apache.org – Ankit Mishra Jan 13 '21 at 21:09
  • After this, I also needed to kill the current gradle daemon with `./gradlew --stop` – Westy92 Jan 09 '23 at 23:07
  • If you are using Intellij or other Jetbrains products, remember to reload all the Gradle projects. – Steve Gelman Apr 20 '23 at 14:21
1002

If you are using a recent version of Gradle, you can use --refresh-dependencies option.

./gradlew build --refresh-dependencies

you can refer to the Gradle manual.

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.

Paulo Merson
  • 13,270
  • 8
  • 79
  • 72
Naga
  • 10,944
  • 2
  • 21
  • 38
  • 15
    You have to be noted that since gradle will try to download all dependency file, it takes time long. – Naga Aug 31 '15 at 07:20
  • 11
    It's also worth noting that this doesn't always work. I just tested running "gradle clear war --refresh-dependencies" with a cached jar that was three days old, when I had deployed a new version late last night. The build broke because of a missing dependency that was added in the new code. I still had the three-day-old jar in the cache afterwards. I ended up just deleting the version folder in my .m2 cache and rebuilding. It then got the newest version because it basically had no choice! – Spanky Quigman Mar 22 '16 at 16:05
  • 23
    better yet just ./gradlew --refresh-dependencies – headsvk Mar 02 '17 at 17:38
  • 2
    This worked great as './gradlew build --refresh-dependencies' from the Android Studio terminal. Thanks! – the_dude_abides Jul 06 '17 at 00:34
  • 4
    Is there a way to get Android Studio to do this on a build from inside the IDE? – karl Jul 12 '18 at 14:32
  • 1
    why wouldn't `--refresh-dependencies` just work without me having to `rm -rf ~/.gradle` folder? I verified that the local jar file is not up to date compared with Artifactory. Gradle version is 4.9 and I have `mavenLocal()` in buildscript->repositories . Any hint for what I can further check? – Alex Apr 23 '19 at 14:59
  • In my case, .gradle folder’s owner is my self, so I don’t need to delete the folder before execute —refresh-dependencies. could you try to check ~/.gradle folder to see who is the owner of the folder and content. – Naga Apr 23 '19 at 22:56
  • Thanks @Nada, no problems with folder permissions. I resolved the problem by removing the "mavenLocal()" line. I realized that expecting gradle to modify Maven caches by inspecting Artifactory is unreasonable: gradle should only be concerned with its own cache. I still left in a maven-url clause which uses an env var so that I can point gradle to a local maven repo on demand: `buildscript { repositories { maven { url "$System.env.TMP_MAVEN_TARGET" } maven { url ... } }` – Alex Apr 24 '19 at 10:35
  • good to hear that. I don’t use mavenLocal() in my project. so it is reasonable for me. – Naga Apr 24 '19 at 15:29
  • as @headsvk pointed out , not including build is better , as a build could invoke other failures dependent on missing dependencies – f0ster May 23 '20 at 14:40
  • In my case I also had to remove the gradle cache directory containing the offending jar before running this command. `--refresh-dependencies` wouldn't recognize that the jar was out of date, or broken somehow. – Lorenz Leitner Nov 18 '20 at 13:00
361

You can tell Gradle to re-download some dependencies in the build script by flagging the dependency as 'changing'. Gradle will then check for updates every 24 hours, but this can be configured using the resolutionStrategy DSL. I find it useful to use this for for SNAPSHOT or NIGHTLY builds.

configurations.all {
    // Check for updates every build
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

Expanded:

dependencies {
    implementation group: "group", name: "projectA", version: "1.1-SNAPSHOT", changing: true
}

Condensed:

implementation('group:projectA:1.1-SNAPSHOT') { changing = true }

I found this solution at this forum thread.

AMK
  • 662
  • 6
  • 16
Umi
  • 4,137
  • 1
  • 17
  • 13
  • 5
    Do you happen to know how this works for sources for the same library? Currently, the compiled library is downloaded every time it changes, but the source is not. – Markus Feb 19 '14 at 15:35
  • 3
    A snapshot version is "changing" by definition. Gradle knows that so you don't actually need to define this in your dependency declaration. – Benjamin Muschko May 16 '14 at 13:40
  • 4
    Thanks for this. FWIW, our dependency was on a snapshot version and until we did this it wasn't checking for updates on every build. – sfitts Jul 09 '14 at 00:54
  • 14
    `cacheChangingModulesFor` is the key, `changing: true` is optional because it's implied by `-SNAPSHOT`, it's possible to use the shorthand here: `compile 'group:projectA:1.1-SNAPSHOT'` because of the above implication. One can also restrict the resolutionStrategy to one config: `configurations.compile.resolutionS...`. – TWiStErRob Nov 10 '14 at 13:15
  • How can I use this if I want to redownload plugins every build which are defined in buildscript classpath? – Michael Feb 19 '15 at 16:00
  • The DSL is documented at http://gradle.org/docs/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html – Chip McCormick Feb 27 '15 at 20:04
  • 2
    @Umi Is there a condensed version of this? Such as `compile 'com.burrowsapps:ads:1.0:true'`? – Jared Burrows Feb 29 '16 at 06:44
  • Just make sure in Android Studio is not set to offline work. Which then won't make it. – Juan Mendez Nov 26 '17 at 20:24
  • 1
    I am having the same observation as @MarkusWüstenberg. Is there a way we get the source refreshed as well? – sutanu dalui Feb 15 '18 at 05:48
  • This is just the programmatic version of `--refresh-dependencies`. How to do this for a single JAR? – Abhijit Sarkar Jul 09 '19 at 22:14
  • Looks like this solution isn't working: https://discuss.gradle.org/t/tell-gradle-not-to-cache-snapshot-dependencies-cachechangingmodulesfor-0-doesnt-work-as-expected/19667/8 – Max Sep 12 '19 at 05:05
  • 1
    not working for me. After adding changing=true, did not pick the latest jar published in mavenLocal. – eastwater Jul 14 '20 at 00:38
  • @MarkusWüstenberg I am facing the same issue. Compiled library is downloaded but sources is not. Did you guys solved it? – aman.nepid Dec 07 '20 at 20:59
  • 1
    @aman.nepid sorry, I have no idea, this was 6 years ago. :) – Markus Dec 08 '20 at 11:38
  • No worries @Markus. The issue still exists :D. Lets see I have asked in new question though: https://stackoverflow.com/questions/65189106/how-to-get-latest-sources-jar-and-javadoc-jar-for-latest-snapshot-aar – aman.nepid Dec 08 '20 at 15:49
  • For Android, `resolutionStrategy` works project-wide when applied in a top-level `build.gradle`, inside `allprojects` block. However, current Android Studio moves most of the logic to `settings.gradle`, with `dependencyResolutionManagement` and whatnot. What is the best place for `resolutionStrategy` now? – gmk57 Mar 04 '22 at 16:27
  • You're a life savior! I can confirm that it worked for me perfectly till this day. I had an artifact in maven central which had not been published to the public. I tried to download the artifact but it downloaded and cached some early information about it and not didn't download the actual dependency. So doing this made it to force recheck for the new files after I have published the artifacts publicly. Thank you! – Ahmad Hamwi Apr 08 '23 at 14:34
78

For MAC

./gradlew build --refresh-dependencies

For Windows

gradlew build --refresh-dependencies

Can also try gradlew assembleDevelopmentDebug --refresh-dependencies

Ali
  • 2,427
  • 22
  • 25
  • 3
    Android uses regular gradle. It's just a plugin. – Dragas Feb 01 '18 at 19:55
  • 1
    The Gradle wrapper is not an Android exclusive. You can generate one using `gradle wrapper` task; you can even generate a wrapper using another Gradle wrapper: `gradlew wrapper` – Salvioner Nov 29 '19 at 15:38
51

For Windows...in order to make gradle re-download specific dependencies:

  1. delete the dependencies you want to re-download from the directory below:

    C:\Users\%USERNAME%\.gradle\caches\modules-2\files-2.1
    
  2. delete all metadata directories at the path:

    C:\Users\%USERNAME%\.gradle\caches\modules-2\metadata-*
    
  3. run gradle build (or gradlew build if using gradle wrapper) in the project's root directory.

note: the numbers in the file paths above might be different for you.

Eric
  • 16,397
  • 8
  • 68
  • 76
  • 1
    Thank you, I was looking for a 1-time deletion of a specific dependency to re-download. This is also the same on the mac: `~/.gradle/caches/modules-2/files-2.1` – slycrel Jun 15 '20 at 23:33
  • I didn't find any metadata folder but deleting specific dependency in modules-2\files-2.1 was enough. Thanks! – Alexey Simchenko Mar 17 '22 at 20:19
29

None of the solutions above worked for me.

If you use IntelliJ, what resolved it for me was simply refreshing all Gradle projects:

enter image description here

Ko Ga
  • 856
  • 15
  • 25
25

One can remove folder with cached jars.

In my case, on Mac the library was cached at path:

/Users/MY_NAME/.gradle/caches/modules-2/files-2.1/cached-library-to-remove

I removed the cached library folder ("cached-library-to-remove" in above example), deleted the build folder of my project and compiled again. Fresh library was downloaded then.

Ashraf.Shk786
  • 618
  • 1
  • 11
  • 23
Farhan
  • 3,162
  • 3
  • 25
  • 17
24

For those who are wondering where to run gradle commands:

  1. Open Android Studio
  2. Click on Terminal(You will find it in the base of Android Studio)
  3. The command tool will open
  4. Type your command ./gradlew build --refresh-dependencies
Boken
  • 4,825
  • 10
  • 32
  • 42
kgandroid
  • 5,507
  • 5
  • 39
  • 69
23

To refresh cached 'release' version the only option is to clear local cache.

rm -rf $HOME/.gradle/caches/

To refresh cached 'snapshot' version you can:

./gradlew build --refresh-dependencies
Karol Król
  • 3,320
  • 1
  • 34
  • 37
18

Instead of removing your entire gradle cache, like some answers here are suggesting, you can delete the cache for a specific group or artifact id. I added the following function to my .bash_profile:

deleteGradleCache() {
  local id=$1
  if [ -z "$id" ]; then
    echo "Please provide an group or artifact id to delete"
    return 1
  fi
  find ~/.gradle/caches/ -type d -name "$id" -prune -exec rm -rf "{}" \; -print
}

Usage:

$ deleteGradleCache com.android.support

Then, on the next build or if you resync, gradle will re-download dependencies.

Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
15

There is 2 ways to do that:

  1. Using command line option to refresh dependenices cashe.
  2. You can delete local cache where artefasts are caches by Gradle and trigger build

Using --refresh-dependencies option:

./gradlew build --refresh-dependencies

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

Long explanantion

  • WIth –refresh-dependencies’ Gradle will always hit the remote server to check for updated artifacts: however, Gradle will avoid downloading a file where the same file already exists in the cache.
    • First Gradle will make a HEAD request and check if the server reports the file as unchanged since last time (if the ‘content-length’ and ‘last-modified’ are unchanged). In this case you’ll get the message: "Cached resource is up-to-date (lastModified: {})."
    • Next Gradle will determine the remote checksum if possible (either from the HEAD request or by downloading a ‘.sha1’ file).. If this checksum matches another file already downloaded (from any repository), then Gradle will simply copy the file in the cache, rather than re-downloading. In this case you’ll get the message: "“Found locally available resource with matching checksum: [{}, {}]”.

Using delete: When you delete caches

rm -rf $HOME/.gradle/caches/

You just clean all the cached jars and sha1 sums and Gradle is in situation where there is no artifacts on your machine and has to download everything. Yes it will work 100% for the first time, but when another SNAPSHOT is released and it is part of your dependency tree you will be faced again in front of the choice to refresh or to purge the caches.

Xelian
  • 16,680
  • 25
  • 99
  • 152
14

Seems change is changed to isChange for gradle version 6.3, kotlin version 1.3.70, Groovy 2.5.10

The working configuration is

implementation("com.sample:commons:1.0.0-SNAPSHOT") {
        isChanging = true
    }

Also, run this command to fetch the latest

./gradlew assemble  --refresh-dependencies
Vinujan.S
  • 1,199
  • 14
  • 27
  • 2
    Very usefull, thanks. Furthermore, if anyone else is using IntelliJ like me, sometimes I have to close and reopen after this refresh. – heringer Jul 28 '21 at 13:13
13

For Android Studio 3.4.1

Simply open the gradle tab (can be located on the right) and right-click on the parent in the list (should be called "Android"), then select "Refresh dependencies".

This should resolve your issue.

Carl Du Plessis
  • 325
  • 3
  • 6
10

If you are using Intellij, you can right click the root project and then select refresh gradle dependencies.

enter image description here

Viktor Mellgren
  • 4,318
  • 3
  • 42
  • 75
  • 3
    To get to the above menu, at the top click: View -> Tool Windows -> Gradle. Then right click on the root project from that Gradle window and you will see the above dialog box. – Freddie Feb 28 '23 at 12:56
9

This worked for me. Make sure Gradle is not set to offline by unchecking button at File>Settings>Gradle>Offline Work.

Add this to the top level of your build.gradle, nice to have above dependencies

configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

I made sure my dependencies are written like this:

implementation('com.github.juanmendez:ThatDependency:ThatBranch-SNAPSHOT') {
    changing = true
}

Thereafter, I open the Gradle panel in Android Studio and click the blue circle arrows button. I can always see my updates getting a new fresh copy.

grine4ka
  • 2,878
  • 1
  • 19
  • 29
Juan Mendez
  • 2,658
  • 1
  • 27
  • 23
5

Mb I'm too late however my solution is for single repository. I think deleting ~/.gradle/* is overkill. The problmem I've bumped into was that I was deleting directory where sources were and gradle was getting another version not from nexus. To avoid that I run the next:

~/.gradle$ find . -type d -name 'group.plugins.awssdk'
./caches/modules-2/files-2.1/group.plugins.awssdk
./caches/modules-2/metadata-2.23/descriptors/group.plugins.awssdk

~/.gradle$ rm -r ./caches/modules-2/files-2.1/group.plugins.awssdk   ./caches/modules-2/metadata-2.23/descriptors/group.plugins.awssdk

After that gradle is dragging files from nexus.

Vadim
  • 1,131
  • 17
  • 16
5

In my case none of the above worked, what I did was:

  • In build.gradle, commenting the dependencies related to the unresolved imports I had
  • Clicking "Sync Now"
  • Uncommenting what I just commented
  • Clicking "Sync Now" again

Then my imports were properly resolved again.

Louis
  • 1,913
  • 2
  • 28
  • 41
2

Deleting all the caches makes download all the dependacies again. so it take so long time and it is boring thing wait again again to re download all the dependancies.

How ever i could be able to resolve this below way.

Just delete groups which need to be refreshed.

Ex : if we want to refresh com.user.test group

rm -fr ~/.gradle/caches/modules-2/files-2.1/com.user.test/

then remove dependency from build.gradle and re add it. then it will refresh dependencies what we want.

Chamly Idunil
  • 1,848
  • 1
  • 18
  • 33
0

I think gradle 2.14.1 fixes the issue. The accepted answer is correct, but there is a bug in gradle with –refresh-dependencies. 2.14.1 fixes that.

See https://discuss.gradle.org/t/refresh-dependencies-should-use-cachechangingmodulesfor-0s/556

Jacques Koorts
  • 1,819
  • 1
  • 17
  • 10
0

For the majority of cases, just simply re-building the project should do the trick. Sometimes you have to run ./gradlew build --refresh-dependencies as several answers have already mentioned (takes a long time, depending on how much dependencies you have). How ever, sometimes none of those will work: the dependency just won't get updated. Then, you can do this:

  1. Remove dependency from your gradle file
  2. Run / debug your project and wait for it to fail (with NonExistingClass reason)
  3. Hit "build project" and wait for it to finish successfully
  4. Run / debug once again

This is ridiculous and seems like madness, but I actually do use this procedure daily, simply because the dependency I need can be updated dozens of times and none of adequate solutions would have any effect.

jujka
  • 1,190
  • 13
  • 18
0

If you are using eclipse and if you want force eclipse to re load dependencies you could try below command

gradlew clean cleaneclipse build eclipse --refresh-dependencies
ULLAS K
  • 881
  • 2
  • 11
  • 24
0

Only a manual deletion of the specific dependency in the cache folder works... an artifactory built by a colleague in enterprise repo.

WesternGun
  • 11,303
  • 6
  • 88
  • 157
0

This is for Kotlin DSL (build.gradle.kts):

configurations.all {
    resolutionStrategy.cacheChangingModulesFor(0, TimeUnit.SECONDS)
}
dependencies {
    // ...
    implementation("ir.mahozad.multiplatform:wavy-slider:0.0.1") {
        isChanging = true
    }
}
Mahozad
  • 18,032
  • 13
  • 118
  • 133
-1

You can do it like this

https://marschall.github.io/2017/04/17/disabling-gradle-cache.html

To quote from Disabling the Gradle Build Cache

The Gradle build cache may be a great thing when you’re regularly building >large projects with Gradle. However when only occasionally building open source >projects it can quickly become large.

To disable the Gradle build cache add the following line to ~/.gradle/gradle.properties

org.gradle.caching=false

You can clean the existing cache with

rm -rf $HOME/.gradle/caches/
rm -rf $HOME/.gradle/wrapper/
piet.t
  • 11,718
  • 21
  • 43
  • 52
wuqilong
  • 1
  • 1
-3

delete this directory:

C:\Users\[username]\.gradle
farhang67
  • 759
  • 2
  • 14
  • 27
  • 3
    Given that there are potentially local configurations, deleting, (or renaming/relocating) the caches directory as mentioned in other answers is a better solution. – dimwittedanimal Oct 16 '17 at 17:30
-8

You need to redownload it, so you can either manually download and replace the corrupted file and again sync your project . Go to this location C:\users[username].gradle\wrapper\dist\gradle3.3-all\55gk2rcmfc6p2dg9u9ohc3hw9\gradle-3.3-all.zip Here delete gradle3.3allzip and replace it by downloading again from this site https://services.gradle.org/distributions/ Find the same file and download and paste it to that location Then sync your project. Hope it works for you too.