我如何告诉gradle从存储库中重新下载依赖项?


当前回答

通常,您可以使用命令行选项refresh-dependencies刷新缓存中的依赖项。你也可以删除~/.gradle/caches下的缓存文件。在下一次构建时,Gradle会再次尝试下载它们。

您的具体用例是什么?您使用动态依赖版本还是快照版本?


在Unix系统上,你可以删除Gradle已经下载的所有现有工件(工件和元数据):

rm -rf $HOME/.gradle/caches/

注意——refresh-dependencies不会总是重新下载每个工件;它将使用与存储库中存在的内容匹配的现有副本。从Gradle用户指南,刷新依赖:

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.

其他回答

如果您正在使用Intellij,您可以右键单击根项目,然后选择刷新gradle依赖项。

你可以这样做

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

引用自禁用Gradle构建缓存

当你经常使用Gradle构建>个大型项目时,Gradle构建缓存可能是一个很棒的东西。然而,如果只是偶尔构建开源>项目,它很快就会变得很大。 要禁用Gradle构建缓存,请在~/.gradle/ Gradle .properties中添加以下代码行 org.gradle.caching = false 可以清除现有缓存 rm -rf $HOME/.gradle/caches/ rm -rf $HOME/.gradle/wrapper/

删除所有缓存会重新下载所有依赖项。所以它需要很长时间,这是无聊的事情等待一次又一次重新下载所有的依赖。

我怎么能解决这个问题下面的方式。

只需删除需要刷新的组。

例如:如果我们想要刷新com.user.test组

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

然后从构建中删除依赖项。Gradle和重新添加它。 然后它将刷新我们想要的依赖项。

你可以告诉Gradle重新下载构建脚本中的依赖项,方法是将依赖项标记为“正在更改”。Gradle将每24小时检查一次更新,但是这可以使用resoltionstrategy DSL进行配置。我发现在SNAPSHOT或NIGHTLY构建中使用它很有用。

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

扩展:

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

压缩:

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

我在这个论坛找到了这个解决方案。

对于那些想知道在哪里运行gradle命令的人:

Open Android Studio 点击终端(你会在Android Studio的基础上找到它) 命令工具将打开 输入命令gradlew build——refresh-dependencies