How to clear gradle cache?

2019-01-02 16:45发布

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 like 45 seconds.

I've tried to check some of my caches: there's a .gradle/caches folder in my home directory, and it's like 123 MB large.

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?

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?

Do I also have to clear the intellij cache too?

7条回答
明月照影归
2楼-- · 2019-01-02 17:15

Command: rm -rf ~/.gradle/caches/

查看更多
后来的你喜欢了谁
3楼-- · 2019-01-02 17:17

You can do it on Android Studio

To clean out the system caches:

On the main menu, choose File | Invalidate Caches/Restart. The Invalidate Caches message appears informing you that the caches will be invalidated and rebuilt on the next start. Use buttons in the dialog to invalidate caches, restart IntelliJ IDEA or both.

https://www.jetbrains.com/idea/help/cleaning-system-cache.html

Update: above method will clear Android Studio cache, not Gradle cache. Gradle cache locates at

  • On Windows: %USER_HOME%\.gradle/caches/
  • On Mac/Unix: $HOME/.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.

Update 2: Clear the Android build cache of current project

Note: Android Studio's File | Invalidate Caches/Restart doesn't clear the Android build cache, so you'll have to clean it separately.

On Windows:

gradlew cleanBuildCache

On Mac or Linux:

./gradlew cleanBuildCache
查看更多
看风景的人
4楼-- · 2019-01-02 17:27

The gradle daemon also creates a many large text files of every single build log. They are stored here:

~/.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".

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.

But you can create a small gradle task to automatically delete them, and free up lots of disk space:

Add this to your 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.

Note that a Gradle Sync or any Gradle Build will trigger the file deletions.

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.

查看更多
不流泪的眼
5楼-- · 2019-01-02 17:37

Take care with gradle daemon, you have to stop it before clear and re-run gradle.

Stop first daemon:

./gradlew --stop

Clean cache with:

rm -rf ~/.gradle/caches/

Run again you compilation

查看更多
旧人旧事旧时光
6楼-- · 2019-01-02 17:40

You can safely delete the whole .gradle folder located under project directory. It'll be recreated every time the tasks are run. The same is for .gradle under home directory. It'll also be recreated as well, but the whole dependencies must be downloaded again - it's quite time-consuming.

As I see in various places over the web gradle for android development is quite slow and people complain about it. No idea what's reason exactly - this is how it works.

I don't think that low performance is because of big folders. Actually they help it to be faster.

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. The cache directory holds the Gradle build cache. So if you have any error about that you can delete that. It's on .gradle/caches

查看更多
怪性笑人.
7楼-- · 2019-01-02 17:40

If you are interested in the entire cache of IntelliJ. Here you go.

You need not to do it manually, let the IntelliJ do it for you. Go to control panel and try to uninstall it. It prompts you the option to delete the cache, and other options as well. Think twice before you attempt it. It might create other problems, like other answers mentioned about this.

enter image description here

查看更多
登录 后发表回答