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


当前回答

您可以删除特定组或工件id的缓存,而不是删除整个gradle缓存,就像这里的一些答案所建议的那样。我在我的.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
}

用法:

$ deleteGradleCache com.android.support

然后,在下一次构建或重新同步时,gradle将重新下载依赖项。

其他回答

Android Studio 3.4.1

简单地打开gradle选项卡(可以位于右侧),右键单击列表中的父项(应该称为“Android”),然后选择“刷新依赖项”。

这将解决您的问题。

对于大多数情况,只需简单地重新构建项目就可以了。有时你必须运行。/gradlew build——refresh-dependencies,就像前面提到的那样(需要很长时间,取决于你有多少依赖)。无论如何,有时这些都不会工作:依赖项不会被更新。然后,你可以这样做:

从gradle文件中删除依赖项 运行/调试你的项目并等待它失败(以NonExistingClass的原因) 点击“构建项目”并等待它成功完成 再次运行/调试

这很荒谬,看起来很疯狂,但我实际上每天都在使用这个过程,只是因为我需要的依赖关系可以更新几十次,而任何适当的解决方案都不会有任何效果。

你可以告诉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 }

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

只有手动删除缓存文件夹中的特定依赖才有效…由企业回购部门的同事制作的工艺品。

通常,您可以使用命令行选项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.