415

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.

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.

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?

Ondra Žižka
  • 36,997
  • 35
  • 184
  • 250
David T.
  • 18,561
  • 18
  • 61
  • 115
  • 4
    i found out later that the 45 minutes to compile is because i changed the settings for `Compiler -> Gradle` to not `Use in-process build`. nothing to do with the cache – David T. Apr 18 '14 at 00:37
  • None of the answers helped. Turns out some images got corrupted. Opening the images in Windows Explorer quickly shows you which images are corrupted (the ones which it doesn't load previews for). Replaced those images and voila! – Bimde Aug 16 '17 at 15:47
  • @david-t Could you point to this option's whereabouts? I have version *3.3.1* but cannot find it under *Preferences - Build, Execution, Deployment* – Leo Feb 25 '19 at 11:35
  • 1
    `./gradlew clean build --no-build-cache` will force a project build without using the build cache. – ams Dec 06 '20 at 03:51

10 Answers10

449

Gradle cache is located at

  • On Windows: %USERPROFILE%\.gradle\caches
  • On 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.


UPDATE

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 UNIX:

./gradlew cleanBuildCache
Bao Le
  • 13,726
  • 9
  • 59
  • 67
  • 28
    This did not clear the cache of a maven dependency. I deleted the library from the maven repository and invalidated the Android Studio caches, but the project still builds. This means the gradle cache for the maven dependency is not cleared. – mattm Jun 26 '15 at 02:44
  • 2
    The way you described will only clean cache from the main project and it's project dependencies, but the library dependencies are intact. You described how to clean AndroidStudio's cache, but not gradle's cache. – Leandroid Oct 16 '15 at 15:38
  • Yeah, but this doesn't appear on Mac before I load the project. – milosmns Nov 28 '15 at 17:21
  • it still works well on the latest version Android Studio – Raju yourPepe Dec 06 '18 at 08:04
  • I manually had to delete the cache folder inside the userdir / .gradle folder – Madhan May 07 '19 at 15:21
  • 2
    you probably don't want to dump the entire cache dir, just the build-cache. use `rm -rf ~/.gradle/caches/build-cache-1` instead. if you need to have gradle re download dependencies invoke the build with --refresh-dependencies – TrevJonez Jun 10 '19 at 17:17
  • 2
    if you want clean and clean cache, run both task: `gradlew clean cleanBuildCache` – equiman Jun 18 '19 at 23:30
  • In my case, I had also to remove `app/build` directory along with gradle cache `$HOME/.gradle/caches/` – Roman Nazarevych Sep 26 '19 at 16:46
  • i was getting 403 and response was cached. this worked for me. – Parham Heidari Mar 01 '21 at 16:23
334

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 .gradle/caches directory holds the Gradle build cache. So if you have any error about build cache, you can delete it.

The --no-build-cache option will run gradle without the build cache.

Daemon on MS Windows If you're on Windows, you'll need to kill the daemon before it allows you to clear those directories. See Kill all Gradle Daemons Regardless Version? for more info.

Jaywalker
  • 2,877
  • 3
  • 27
  • 41
Opal
  • 70,085
  • 21
  • 151
  • 167
  • 51
    One thing to note is that if you have a gradle.properties file in your .gradle folder in your home directory, you will not want to delete the whole folder. Typically, just deleting .gradle/caches is enough to get Gradle to redownload all dependencies. – Bradford2000 Feb 09 '15 at 17:28
  • 2
    with every update to Android Studio, gradle builds seem to get slower and slower. Why is it that when I deploy to device, stop app running, and then deploy again (without touching any code at all!) that Android studio once again takes 2 minutes to make a build and deploy it. It's friggin nutz. – Someone Somewhere Jun 12 '18 at 12:15
  • 1
    On Android Studio to clean out the system caches: Android stdio main menu, choose File | Invalidate Caches/Restart.and build menu >clean project – Shomu Oct 13 '18 at 12:16
  • 2
    If you're on Windows, you'll need to kill the daemon first: https://stackoverflow.com/questions/54189630/kill-all-gradle-daemons-regardless-version – winrid Oct 18 '20 at 19:33
  • --no-build-cache on windows appears to do nothing – Mark Feb 25 '21 at 15:04
80

UPDATE

cleanBuildCache no longer works.

Android Gradle plugin now utilizes Gradle cache feature
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-*

https://guides.gradle.org/using-build-cache/#caching_android_projects

Other digressions: see here (including edits).


=== OBSOLETE INFO ===

Newest solution using Gradle task:

cleanBuildCache

Available via Android plugin for Gradle, revision 2.3.0 (February 2017)

Dependencies:

  1. Gradle 3.3 or higher.
  2. Build Tools 25.0.0 or higher.

More info at:
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.

NOTE: The cleanBuildCache task is not available if you disable the build cache.


USAGE

Windows:

gradlew cleanBuildCache

Linux / Mac:

gradle cleanBuildCache

Android Studio / IntelliJ:

gradle tab (default on right) select and run the task or add it via the configuration window 

NOTE: gradle / gradlew are system specific files containing scripts. Please see the related system info how to execute the scripts:

informatik01
  • 15,174
  • 9
  • 67
  • 100
ceph3us
  • 6,591
  • 2
  • 34
  • 39
72

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

Stop first daemon:

./gradlew --stop

Clean cache using:

rm -rf ~/.gradle/caches/

Run again you compilation

Mohammad Kholghi
  • 257
  • 2
  • 12
rresino
  • 1,117
  • 7
  • 7
  • Can you explain why or link to some documentation on this? – tir38 Nov 14 '19 at 16:57
  • 4
    If your gradle daemon is running your gradle caches will be in use. Consequently, your OS will likely prevent the deletion. – 0x539 Jan 15 '20 at 06:15
  • After hours of struggle... THANK MATE. I actually belive that `./gradlew --stop` this line did everything. – Itoun Sep 24 '20 at 16:13
9

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.

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.

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.

TIP: search for Gradle's cache help online without mentioning the keyword 'android' to get help for the currently relevant cache.

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.

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.

i execute cleanBuildCache and the 3.4.2/ directory is gone. next i do another 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).

conclusions:

  1. caching of all normal Android builder tasks is handled by Gradle.
  2. executing cleanBuildCache does not clear or affect the build cache in any way.
  3. 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.)

next, i disable the Gradle cache by removing org.gradle.caching=true from gradle.properties and i try a couple of 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.

more conclusions:

  1. there is no Android builder cache fallback for when the Gradle cache fails to hit.
  2. the Android builder cache, at least for common tasks, has indeed been eliminated as i stated before.
  3. the relevant android doc contains outdated info. in particular the cache is not enabled by default as stated there, and the Gradle cache has to be enabled manually.

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!

Lanchon
  • 165
  • 1
  • 4
  • Can you link to where you read that the Android builder cache is now obsolete and that it now uses Gradle's build cache instead? – tir38 Jan 01 '20 at 22:53
  • @tir38, nope. but i updated my answer above with my own testing. thanks. – Lanchon Jan 17 '20 at 20:25
  • Thanks a bunch for your diligent research. You confirm most of what I saw in testing too: 1. empty `3.4.2/` dir. 2. presence of `cleanBuildCache` task 3. disabling gradle build cache and rebuilding didn't show any evidence of android tasks hitting cache. – tir38 Jan 22 '20 at 22:24
  • 2
    A little more poking and I got actual confirmation that android's build cache is/was rolled into gradle's https://issuetracker.google.com/issues/37324009#comment3 I opened a doc request to remove that doc page: https://issuetracker.google.com/issues/148169019 – tir38 Jan 22 '20 at 22:30
8

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.

Mr-IDE
  • 4,530
  • 1
  • 36
  • 44
  • To send OSX items to the finder/trash instead of directly remove, this post seems to have many good ideas https://apple.stackexchange.com/questions/50844/how-to-move-files-to-trash-from-command-line – AnneTheAgile Jul 30 '19 at 23:52
3

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

AsyncMoksha
  • 2,148
  • 2
  • 22
  • 31
0

In android studio open View > Tool Windows > Terminal and execute the following commands

On Windows:

gradlew cleanBuildCache

On Mac or Linux:

./gradlew cleanBuildCache

if you want to disable the cache from your project add this into the gradle build properties

(Warning: this may slow your PC performance if there is no cache than same time will consume after every time during the run app)

android.enableBuildCache=false
Ankur Rohilla
  • 640
  • 14
0

To clear your gradle cache in android studio:

  1. open terminal and
  2. run gradlew clean
phoenixstudio
  • 928
  • 1
  • 5
  • 15
0

My ~/.gradle/caches/ folder was using 14G.

After using the following solution, it went from 14G to 1.7G.

$ rm -rf ~/.gradle/caches/transforms-*
$ rm -rf ~/.gradle/caches/build-cache-*

Bonus

This command shows you in detail the used cache space

$ sudo du -ah --max-depth = 1 ~/.gradle/caches/ | sort -hr
Antonio Cord
  • 96
  • 2
  • 4