Android 如何清除gradle缓存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23025433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clear gradle cache?
提问by David T.
I'm trying to use Android Studio, and the first time I boot it up, it takes like 45 MINUTES to compile... If I don't quit the application, it is okay - each subsequent compilation/running the app will take around 45 seconds.
我正在尝试使用 Android Studio,第一次启动时,编译需要 45 分钟...如果我不退出应用程序,没关系 - 每次后续编译/运行应用程序都需要大约 45 秒。
I've tried to check some of my caches: there's a .gradle/caches
folder in my home directory, and it's contains 123 MB.
我试图检查我的一些缓存:.gradle/caches
我的主目录中有一个文件夹,它包含 123 MB。
There's also a .gradle
folder in my project folder... one of the taskArtifacts
was like 200 MB. I'm scared to just randomly nuke them both. What parts of the folders are safe to delete?
.gradle
我的项目文件夹中还有一个文件夹……其中一个taskArtifacts
大约 200 MB。我害怕只是随机地对他们俩进行核弹。文件夹的哪些部分可以安全删除?
Is there a better explanation for why my Android Studio is taking forever to run the gradle assemble
task upon first time loading the application?
有没有更好的解释为什么我的 Android Studiogradle assemble
在第一次加载应用程序时需要永远运行任务?
Do I also have to clear the intellij cache too?
我是否也必须清除 Intellij 缓存?
采纳答案by Opal
As @Bradford20000 pointed out in the comments, there might be a gradle.properties
file as well as global gradle scripts located under $HOME/.gradle
. In such case special attention must be paid when deleting the content of this directory.
正如@ Bradford20000在评论中指出,有可能是一个gradle.properties
文件,以及位于下全球gradle这个脚本$HOME/.gradle
。在这种情况下,删除该目录的内容时必须特别注意。
The .gradle/caches
directory holds the Gradle
build cache. So if you have any error about build cache, you can delete it.
该.gradle/caches
目录包含Gradle
构建缓存。因此,如果您对构建缓存有任何错误,可以将其删除。
回答by Bao Le
Gradlecache locates at
Gradle缓存位于
- On Windows:
%USER_HOME%\.gradle/caches/
- On Mac/Unix:
~/.gradle/caches/
- 在 Windows 上:
%USER_HOME%\.gradle/caches/
- 在 Mac/Unix 上:
~/.gradle/caches/
You can browse to these directory and manually delete it or run
您可以浏览到这些目录并手动删除它或运行
rm -rf $HOME/.gradle/caches/
on Unix system. Run this command will also force to download dependencies.
在Unix系统上。运行此命令也将强制下载依赖项。
Update 2:Clear the Androidbuild cache of current project
更新 2:清除当前项目的Android构建缓存
Note: Android Studio's File | Invalidate Caches/Restartdoesn't clear the Android build cache, so you'll have to clean it separately.
注意:Android Studio 的文件 | Invalidate Caches/Restart不会清除 Android 构建缓存,因此您必须单独清理它。
On Windows:
在 Windows 上:
gradlew cleanBuildCache
On Mac or Linux:
在 Mac 或 Linux 上:
./gradlew cleanBuildCache
回答by ceph3us
EDIT:cleanBuildCache
no longer works
编辑:cleanBuildCache
不再有效
android gradle plugin now utilizes gradle cache feature
android gradle 插件现在利用 gradle 缓存功能
REF: https://guides.gradle.org/using-build-cache/
参考:https: //guides.gradle.org/using-build-cache/
TO CLEAR CACHE
Clean the cache directory to avoid any hits from previous builds
rm -rf $GRADLE_HOME/caches/build-cache-*
清除缓存
清理缓存目录以避免以前构建的任何命中
rm -rf $GRADLE_HOME/caches/build-cache-*
REF: https://guides.gradle.org/using-build-cache/#caching_android_projects
参考:https: //guides.gradle.org/using-build-cache/#caching_android_projects
OTHER DIGRESSIONS
其他题外话
see here(including edits).
见这里(包括编辑)。
================
================
OBSOLETE INFO:
过时的信息:
Newest solution usinggradle task
使用gradle 任务的最新解决方案
cleanBuildCache
cleanBuildCache
available via android plugin for Gradle, revision 2.3.0 (February 2017)
可通过适用于 Gradle 的 android 插件获得,修订版 2.3.0(2017 年 2 月)
Dependencies:
依赖项:
- Gradle 3.3 or higher.
- Build Tools 25.0.0 or higher.
- Gradle 3.3 或更高版本。
- 构建工具 25.0.0 或更高版本。
more at:
更多在:
https://developer.android.com/studio/build/build-cache.html#clear_the_build_cache
https://developer.android.com/studio/build/build-cache.html#clear_the_build_cache
background
背景
Build cache:
构建缓存:
stores certain outputs that the Android plugin generates when building your project (such as unpackaged AARs and pre-dexed remote dependencies). Your clean builds are much faster while using the cache because the build system can simply reuse those cached files during subsequent builds, instead of recreating them. Projects using Android plugin 2.3.0 and higher use the build cache by default. To learn more, read Improve Build Speed with Build Cache.
存储 Android 插件在构建项目时生成的某些输出(例如未打包的 AAR 和预先索引的远程依赖项)。使用缓存时,您的干净构建要快得多,因为构建系统可以在后续构建期间简单地重用这些缓存文件,而不是重新创建它们。使用 Android 插件 2.3.0 及更高版本的项目默认使用构建缓存。要了解更多信息,请阅读使用构建缓存提高构建速度。
Note: The cleanBuildCache task is not available if you disable the build cache.
注意:如果禁用构建缓存,cleanBuildCache 任务将不可用。
usage:
用法:
windows
视窗
gradlew cleanBuildCache
linux / mac
linux/mac
gradle cleanBuildCache
Android Studio / IntelliJ
安卓工作室/IntelliJ
gradle tab (default on right) select and run the task or add it via the configuration window
**gradle/gradlew are system specific files containing scripts - please see system info how to execute the script
**gradle/gradlew 是包含脚本的系统特定文件 - 请参阅系统信息如何执行脚本
回答by rresino
Take care with gradle daemon, you have to stop it before clear and re-run gradle.
小心 gradle 守护进程,你必须在清除并重新运行 gradle 之前停止它。
Stop first daemon:
停止第一个守护进程:
./gradlew --stop
Clean cache using:
使用以下方法清理缓存:
rm -rf ~/.gradle/caches/
Run again you compilation
再次运行你的编译
回答by Mr-IDE
The gradle daemon also creates a many large text files of every single build log. They are stored here:
gradle 守护程序还会为每个构建日志创建许多大型文本文件。它们存储在这里:
~/.gradle/daemon/X.X/daemon-XXXX.out.log
~/.gradle/daemon/X.X/daemon-XXXX.out.log
"X.X" is the gradle version in use, like "4.4", and "XXXX" are just random numbers, like "1234".
“XX”是使用的gradle版本,比如“4.4”,“XXXX”只是随机数,比如“1234”。
The total size can grow to several hundred MB in just a few months. There is no way to disable the logging, and the files are not automatically deleted and they do not really need to be retained.
在短短几个月内,总大小可以增长到数百 MB。没有办法禁用日志记录,文件不会自动删除,也不需要真正保留。
But you can create a small gradle task to automatically delete them, and free up lots of disk space:
但是你可以创建一个小的 gradle 任务来自动删除它们,并释放大量的磁盘空间:
Add this to your app/build.gradle
:
将此添加到您的app/build.gradle
:
android {
buildTypes {
...
}
// Delete large build log files from ~/.gradle/daemon/X.X/daemon-XXX.out.log
// Source: https://discuss.gradle.org/t/gradle-daemon-produces-a-lot-of-logs/9905
def gradle = project.getGradle()
new File("${gradle.getGradleUserHomeDir().getAbsolutePath()}/daemon/${gradle.getGradleVersion()}").listFiles().each {
if (it.getName().endsWith('.out.log')) {
// println("Deleting gradle log file: $it") // Optional debug output
it.delete()
}
}
}
To see which files are being deleted, you can see the debug output in Android Studio -> View -> Tool Windows -> Build. Then press "Toggle View" button on that window to show the text output.
要查看哪些文件正在被删除,您可以在 Android Studio -> View -> Tool Windows -> Build 中查看调试输出。然后按该窗口上的“切换视图”按钮以显示文本输出。
Note that a Gradle Sync or any Gradle Build will trigger the file deletions.
请注意,Gradle Sync 或任何 Gradle Build 都会触发文件删除。
A better way would be to automatically move the files to the Trash/Recycle Bin, or at least copy them to a Trash folder first. But I don't know how to do that.
更好的方法是自动将文件移动到垃圾箱/回收站,或者至少先将它们复制到垃圾箱文件夹。但我不知道该怎么做。
回答by Lanchon
there seems to be incorrect info posted here. some people report on how to clear the Android builder cache (with task cleanBuildCache
) but do not seem to realize that said cache is independent of Gradle's build cache, AFAIK.
这里发布的信息似乎不正确。有些人报告了如何清除 Android 构建器缓存(带有任务cleanBuildCache
),但似乎没有意识到所述缓存独立于 Gradle 的构建缓存 AFAIK。
my understanding is that Android's cache predates (and inspired) Gradle's, but i could be wrong. whether the Android builder will be/was updated to use Gradle's cache and retire its own, i do not know.
我的理解是 Android 的缓存早于(并受到启发)Gradle 的缓存,但我可能是错的。我不知道 Android 构建器是否会/被更新为使用 Gradle 的缓存并停用它自己的缓存。
EDIT:the Android builder cache is obsolete and has been eliminated. the Android Gradle plugin now uses Gradle's build cache instead. to control this cache you must now interact with Gradle's generic cache infrastructure.
编辑:Android 构建器缓存已过时并已被淘汰。Android Gradle 插件现在使用 Gradle 的构建缓存。要控制此缓存,您现在必须与 Gradle 的通用缓存基础结构进行交互。
TIP: search for Gradle's cache help online without mentioning the keyword 'android' to get help for the currently relevant cache.
提示:在线搜索 Gradle 的缓存帮助,无需提及关键字 'android' 以获得当前相关缓存的帮助。
EDIT 2:due to tir38's question in a comment below, i am testing using an Android Gradle plugin v3.4.2 project. the gradle cache is enabled by org.gradle.caching=true
in gradle.properties
. i do a couple of clean build
and the second time most tasks show FROM-CACHE
as their status, showing that the cache is working.
编辑 2:由于 tir38 在下面评论中的问题,我正在使用 Android Gradle 插件 v3.4.2 项目进行测试。gradle 缓存由org.gradle.caching=true
in启用gradle.properties
。我做了几次,clean build
第二次大多数任务显示FROM-CACHE
为它们的状态,表明缓存正在工作。
surprisingly, i have a cleanBuildCache
gradle task and a <user-home>/.android/build-cache/3.4.2/
directory, both hinting the existence of an Android builder cache.
令人惊讶的是,我有一个cleanBuildCache
gradle 任务和一个<user-home>/.android/build-cache/3.4.2/
目录,两者都暗示了 Android 构建器缓存的存在。
i execute cleanBuildCache
and the 3.4.2/
directory is gone. next i do another clean build
:
我执行cleanBuildCache
,3.4.2/
目录不见了。接下来我做另一个clean build
:
- nothing changed: most tasks show
FROM-CACHE
as their status and the build completed at cache-enabled speeds. - the
3.4.2/
directory is recreated. - the
3.4.2/
directory is empty (save for 2 hidden, zero length marker files).
- 没有任何变化:大多数任务显示
FROM-CACHE
为它们的状态,并且构建以启用缓存的速度完成。 - 该
3.4.2/
目录是重新创建。 - 该
3.4.2/
目录为空(保存 2 个隐藏的零长度标记文件)。
conclusions:
结论:
- caching of all normal Android builder tasks is handled by Gradle.
- executing
cleanBuildCache
does not clear or affect the build cache in any way. - there is still an Android builder cache there. this could be vestigial code that the Android build team forgot to remove, or it could actually cache something strange that for whatever reason has not or cannot be ported to using the Gradle cache. (the 'cannot' option being highly improvable, IMHO.)
- 所有普通 Android 构建器任务的缓存都由 Gradle 处理。
- 执行
cleanBuildCache
不会以任何方式清除或影响构建缓存。 - 那里仍然有一个 Android 构建器缓存。这可能是 Android 构建团队忘记删除的残留代码,或者它实际上可能缓存一些奇怪的东西,无论出于何种原因,都没有或无法使用 Gradle 缓存进行移植。(恕我直言,“不能”选项是高度可改进的。)
next, i disable the Gradle cache by removing org.gradle.caching=true
from gradle.properties
and i try a couple of clean build
:
接下来,我通过删除org.gradle.caching=true
来禁用 Gradle 缓存,gradle.properties
然后尝试以下操作clean build
:
- the builds are slow.
- all tasks show their status as being executed and not cached or up to date.
- the
3.4.2/
directory continues to be empty.
- 构建很慢。
- 所有任务都将其状态显示为正在执行且未缓存或最新。
- 该
3.4.2/
目录继续为空。
more conclusions:
更多结论:
- there is no Android builder cache fallback for when the Gradle cache fails to hit.
- the Android builder cache, at least for common tasks, has indeed been eliminated as i stated before.
- the relevant android doccontains outdated info. in particular the cache is not enabled by defaultas stated there, and the Gradle cache has to be enabled manually.
- 当 Gradle 缓存未能命中时,没有 Android 构建器缓存回退。
- 正如我之前所说,Android 构建器缓存(至少对于常见任务)确实已被消除。
- 相关的android 文档包含过时的信息。特别是缓存在默认情况下未启用,如此处所述,必须手动启用 Gradle 缓存。
EDIT 3: user tir38 confirmed that the Android builder cache is obsolete and has been eliminated with this find. tir38 also created this issue. thanks!
编辑 3:用户 tir38 确认 Android 构建器缓存已过时并已通过此 find消除。tir38 也造成了这个问题。谢谢!
回答by Zinan Xing
Command: rm -rf ~/.gradle/caches/
命令: rm -rf ~/.gradle/caches/