fedor.belov
fedor.belov

Reputation: 23343

How can I force Gradle to redownload dependencies?

How can I tell Gradle to redownload dependencies from repositories?

Upvotes: 1045

Views: 1136171

Answers (26)

Naga
Naga

Reputation: 11604

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.

Upvotes: 1065

Mahozad
Mahozad

Reputation: 24712

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

configurations.all {
    resolutionStrategy.cacheChangingModulesFor(0, TimeUnit.SECONDS)
}
dependencies {
    // ...
    implementation("ir.mahozad.multiplatform:wavy-slider:1.0.0") {
        isChanging = true
    }
}

Upvotes: 2

Benjamin Muschko
Benjamin Muschko

Reputation: 33456

Option 1: Use --refresh-dependencies flag

Generally, you can refresh dependencies in your cache with the command line option --refresh-dependencies.

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.

Option 2: Remove ~/.gradle/caches

You can also delete the cached files (artifacts and metadata) under ~/.gradle/caches. With the next build Gradle will attempt to download them again.

On *nix systems (Linux, macOS, ...), you can execute this command:

rm -rf $HOME/.gradle/caches/

After doing this, you might also need to kill the running Gradle Daemon:

./gradlew --stop 

Upvotes: 1124

kgandroid
kgandroid

Reputation: 5595

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

Upvotes: 28

Viktor Mellgren
Viktor Mellgren

Reputation: 4506

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

enter image description here

Upvotes: 17

Eric
Eric

Reputation: 17566

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.

Upvotes: 58

Vinujan.S
Vinujan.S

Reputation: 1309

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

Upvotes: 16

Karol Król
Karol Król

Reputation: 3530

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

Upvotes: 24

Louis
Louis

Reputation: 2033

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.

Upvotes: 6

WesternGun
WesternGun

Reputation: 12787

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

Upvotes: 0

Umi
Umi

Reputation: 4217

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.

Upvotes: 369

Carl Du Plessis
Carl Du Plessis

Reputation: 335

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.

Upvotes: 14

Ko Ga
Ko Ga

Reputation: 906

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

Upvotes: 30

Chamly Idunil
Chamly Idunil

Reputation: 1872

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.

Upvotes: 3

Vadim
Vadim

Reputation: 1131

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.

Upvotes: 5

ULLAS K
ULLAS K

Reputation: 901

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

Upvotes: 0

Juan Mendez Escobar
Juan Mendez Escobar

Reputation: 2767

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.

Upvotes: 11

wuqilong
wuqilong

Reputation: 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/

Upvotes: -1

Xelian
Xelian

Reputation: 17218

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.

Upvotes: 15

jujka
jujka

Reputation: 1217

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.

Upvotes: 0

Farhan
Farhan

Reputation: 3363

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.

Upvotes: 25

Ali
Ali

Reputation: 2487

For MAC

./gradlew build --refresh-dependencies

For Windows

gradlew build --refresh-dependencies

Can also try gradlew assembleDevelopmentDebug --refresh-dependencies

Upvotes: 78

farhang67
farhang67

Reputation: 789

delete this directory:

C:\Users\[username]\.gradle

Upvotes: -3

Rahul Gupta
Rahul Gupta

Reputation: 9

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.

Upvotes: -8

Jared Rummler
Jared Rummler

Reputation: 38131

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.

Upvotes: 18

Jacques Koorts
Jacques Koorts

Reputation: 1827

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

Upvotes: 0

Related Questions