统一 Gradle 版本减少磁盘空间占用
最近电脑总是提示磁盘空间不够,发现 Gradle 缓存占用太多空间,所以动手清理一下。
问题背景
最近我经常在 Mac 上运行 Android 项目。512 G的硬盘原本一直够用,最近却老是提示磁盘空间快满了,不得不经常要手动清理一下空间。
要手动清理磁盘只不过带来些小小的不便,让人很恼火的是磁盘空间不够常常会引发其他问题。比如,我的 Android Studio 中 Gemini 已经配置好了,AI 也能回答我的提问,但代码提示功能和上下文菜单中的 Gemini 却用不了。我注意到 Android Studio 状态栏中提示 “ai model is not ready”。
我早前使用 Chrome 内置的 Gemini 时遇到类似情况,磁盘剩余空间过小导致 Chrome 无法下载 Gemini 模型。所以我猜测 Android Studio 提示 “ai model is not ready” 很可能也跟磁盘空间不够有关,毕竟一个 AI 模型动辄上 10G,还是比较占空间的。
谁占了我的磁盘空间
我们可以借助 du
命令可以非常方便地找到占用空间最多的目录和文件。在 Mac 上,个人目录通常是第一查找目标。Mac 系统设置的储存空间里并没有列出隐藏文件所占用的空间,所以我们优先查找隐藏文件:
1 | du -h /Users/<User>/.* | sort -rh | head -n 10 |
个人目录中最大的10个隐藏文件如下:
1 | 18G /Users/cm/.cache |
.gradle
目录占用的空间让人震惊。(注:以上看到的 15G 是我经过清理之后的数据。清理之前实际上比 15G 要大得多)。进一步缩小统计范围到 .gradle/caches
目录,看看谁占了最多的空间:
1 | # .gradle/caches 目录中最大的10个文件 |
我发现 .gradle/caches
中有各种不同版本号的子目录,分别都占用了不小的空间:
1 | 2.3G /Users/cm/.gradle/caches/8.11.1 |
关于 Gradle Caches
DeepSeek 关于 .gradle/caches
目录的解释如下:
Gradle stores cached data in the
~/.gradle/caches
directory for various versions you are using in your project to
improve build speed and efficiency. The exact directories will be named after each Gradle version that you have used,
like.gradle/caches/3.5
or.gradle/caches/4.10
.Each of these directories can contain a lot of files due to the following reasons:
Cached Libraries: Every time you build your project Gradle will download any libraries that it needs (dependencies)
and store them in the cache for later use. This could be a large number of small files, especially if your dependencies
are large or complex.Transformed Code Caches: Gradle can also cache some forms of compiled code to speed up builds. For example, it
might cache the results of applying an annotation processor to some source code. If you have a lot of such caching
happening in your build, this could be causing a significant increase in disk space usage.Build Outputs: Gradle stores the outputs of individual tasks so they don’t need to be rebuilt if nothing has
changed (incremental builds). This can result in large files being stored here if you have a lot of build outputs.
由于 .gradle/caches
只是缓存数据,所以我们可以放心优化。DeepSeek 提到尽可能使用同一版本的 Gradle 可以有效减少 .gradle/caches
占用的磁盘空间。
If you’re only using a single version of Gradle across all your projects and if that version doesn’t
have any outdated or unnecessary dependencies cached (as mentioned before), the~/.gradle/caches
directory can become
quite lean indeed.Consider pruning older Gradle versions that you no longer use. This can help to manage disk space used for caching.
我的这些 Android 项目是个人项目或开源项目,所以不同项目统一使用的相同版本的 Gradle 并不存在任何障碍或风险。不过,如果是公司的项目的话,就要注意应当尽可能跟团队保持一致的开发环境。
统一不同项目的 Gradle 其实无非就是改改 gradle-wrapper.properties
文件的 distributionUrl
参数中的版本号。但改成哪个版本的 Gradle 最合适呢?
统计 Gradle 版本号
DeepSeek 帮我写了这个 py_gradle_ver_stat.py
脚本用于统计本地 Android 项目中哪个版本的 Gradle 用得最多。
1 | import os |
脚本输出日志如下:
1 | python3 py_gradle_ver_stat.py |
日志显示我的项目当中使用 8.12.1
版本的 Gradle 最多,所以以后就统一使用这个版本的 Gradle 吧。我果断将几个项目的 Gradle 升级到 8.12.1,之后在 .gradle/caches
中删除了这几个旧版本的缓存数据后再看磁盘空间,又有200多G剩余,应该够用上好一阵子了。
总结
- 磁盘空间不够可能会影响电脑功能。借助
du
命令发现 Gradle Caches 占用过多磁盘空间 - 一种减少磁盘空间占用的方式是不同项目尽可能使用同一个版本的 Gradle,以减少 Caches
- 写了一个 Python 脚本找到当前用得最多的 Gradle 版本,并统一使用这个版本
参考
Android Gradle 插件 8.8 版本说明 | Android Studio | Android Developers