The build cache stores certain outputs that the Android plugin for Gradle 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. The build cache also works on continuous integration servers and when running multiple build processes on a single local machine.
Projects using Android plugin 2.3.0 and higher enable the build cache by default (unless you explicitly disable the build cache). However, the plugin disables caching of pre-dexed remote dependencies if you set one of the following build properties to something different than what's shown below. (These are the default settings for each property, so if you don't declare them at all, caching of pre-dexed remote dependencies remains enabled.)
android {
defaultConfig {
// If you do enable multidex, you must also set
// minSdkVersion to 21 or higher.
multiDexEnabled false
}
buildTypes {
<build-type> {
minifyEnabled false
}
}
dexOptions {
preDexLibraries true
}
...
}
...
Note: If your project uses Android plugin version 2.2.2 or 2.2.3, it's using the experimental version of the build cache feature. You should update your project to use the latest version of the Android plugin.
If you want to learn about other ways to make your builds faster, read Optimize your build speed.
Change the location of the build cache
By default, the Android plugin saves your cache in
<user-home>/.android/build-cache/. If you
configure one of the following path variables (listed in decreasing
priority), Android Studio uses
<path-variable>/.android/build-cache/ instead:
-
ANDROID_SDK_HOME -
user.home -
HOME
The Android plugin uses one default location for the build cache so it can share the cached files between all your projects that use Android plugin 2.3.0 or higher (and don’t disable build cache). For example, after one of your projects builds and caches a pre-dexed dependency, other projects that also use that dependency can skip pre-dexing it again by copying it from the shared build cache.
If you would rather have a project create its own cache (and not share that
cache with other projects), you can specify a unique location for the cache
in that project’s gradle.properties file as follows:
// You can specify either an absolute path or a path relative // to the gradle.properties file. android.buildCacheDir=<path-to-directory>
When you’re done editing the file, click Sync Project
to create the new build cache directory.
Note: Avoid specifying a directory for your build cache that's inside
the <project-root>/build/ or
<project-root>/<module-root>/build/
directory because Gradle deletes these directories any time it runs the
clean task.
If you want to share the cache with only certain other projects, specify the
same build cache directory in the gradle.properties file of
those projects.
Clear the build cache
Similar to the Android plugin's clean task that clears your
project’s build/ directories, you can run the
cleanBuildCache task to clear your project’s build cache. If a
project specifies a non-default directory
for its build cache, running the task from that project
clears only that cache (and not the shared cache in the default location). To
execute the task, select View > Tool Windows > Terminal from the
menu bar and use one of the following commands:
- On Windows:
gradlew cleanBuildCache
- On Mac or Linux:
./gradlew cleanBuildCache
Note: The cleanBuildCache task is not available if you
disable the build cache.
Disable the build cache
Because the build cache speeds up your clean builds, disabling the feature is
not recommended. If you still want to disable the build cache for your
project, add the following to its gradle.properties file:
// To re-enable the build cache, either delete the following // line or set the property to 'true'. android.enableBuildCache=false
When you’re done editing the file, click Sync Project
to apply your changes.
Note: After you disable the build cache, the Android plugin ignores
the android.buildCacheDir property and the
cleanBuildCache task is no longer available. Also, disabling the
build cache does not automatically clear the cache directory. This allows you
to retain the cached files if you decide to re-enable the build cache.