Technical docs‎ > ‎

Build Cache

Update for Android Studio 2.3 Canary:

  • The build cache is now enabled by default.
  • There is a new Gradle task called cleanBuildCache for you to more easily clean the build cache. You can use it by typing the following in your terminal: ./gradlew cleanBuildCache

Introduction

Android Studio 2.2 Beta 3 introduces a new build cache feature that can speed up build times for clean builds by storing and reusing files/directories that were created in previous builds of the same or different Android project.

The build cache is meant to be used across all Android projects. The developer can enable/disable the build cache and specify its location in the gradle.properties file. Currently, the build cache contains only pre-dexed libraries; in the future, we will use it for caching other types of files as well.


Note that the build cache works independently of Gradle’s cache management (e.g., reporting up-to-date statuses). When a task is executed, whether the build cache is used or not during its execution will be unknown to Gradle (i.e., even when the cache is hit, Gradle’s profile report will not show the task as up-to-date). However, you should expect to get faster build times when the build cache is used.

Although there are currently no known issues, we would like to provide the community more time to provide feedback on it. This feature is currently considered to be experimental and is disabled by default (update: this feature enabled by default starting with Android Studio 2.3 Canary 1). With your feedback, we are aiming to graduate it to stable and enable it by default in Android Studio 2.3 or 2.4.

How to use the Build Cache

Step 0

Ensure that android.dexOptions.preDexLibraries is either not set or set to true; otherwise the libraries will not be pre-dexed and therefore the build cache will not be used.

Step 1

In an Android project, open the gradle.properties file and add the following parameters:


android.enableBuildCache=true # Set to true or false to enable or disable the build cache. If this parameter is not set, the build cache is disabled by default.


android.buildCacheDir=<path-to-build-cache-directory> # Optional, specify the absolute path of the build cache directory. If you use a relative path, it will be relative to the root directory of the Android project. If this parameter is not set, the default build cache directory at <user-home-directory>/.android/build-cache will be used. Multiple Android projects can share the same cache if they specify the same build cache location (in that case, it is recommended that the default location or a location outside the projects is used, although it is still possible to use a location inside one of the projects). In any case, it is important that the build cache location is not in a “build” directory; otherwise, it would be erased after running a clean task. This parameter is ignored if android.enableBuildCache=false.

Step 2

Build your Android project (on the terminal, type ./gradlew assemble) and check the following locations to see whether the build cache took effect.

  • The cached files are stored in the build cache directory set by the android.buildCacheDir parameter above. By default, this is <user-home-directory>/.android/build-cache.

  • The final pre-dexed files are stored in <project-dir/module-dir>/build/intermediates/pre-dexed/debug and <project-dir/module-dir>/build/intermediates/pre-dexed/release. You need to run the command line to see the “pre-dexed” directory; if you click the “Run” button from the Android Studio menu, you will not see this directory as it will be deleted.


Note: If you are using Multi-dex with minSdk >= 21, the dexed files will be stored directly into the <project-dir/module-dir>/build/intermediates/transforms/dex directory without being stored in <project-dir/module-dir>/build/intermediates/pre-dexed.

Cleaning the Build Cache

To clean the build cache, you can navigate to the build cache directory and delete all the files in that directory. The build cache directory is either located at the path specified by the android.buildCacheDir property in your gradle.properties file, or it is located at <user-home-directory>/.android/build-cache by default.


Update: starting with Android Studio 2.3 Canary 1, there is a now a Gradle task called cleanBuildCache that you can use to more conveniently clean the build cache. You can use it by typing the following in your terminal:


    ./gradlew cleanBuildCache


Feedback

We encourage you to try this feature and to see how it's working for you. Please file a bug to us if you find any issues or if you have other feedback about this feature for us. Thanks!
Comments