Use the build cache to speed up builds

Google original link https://developer.android.com/studio/build/build-cache.html

The build cache can store specific outputs (such as unpackaged AARs and pre-dexed remote dependencies) that the Android Plugin for Gradle generates when building your project. Your clean builds will be significantly faster when using caches because the build system can directly reuse these cached files during subsequent builds without recreating them. The build cache also works on a continuous integration server and is suitable for running multiple build processes on one local machine.

Projects using  Android Plugin 2.3.0 and higher have build caching enabled by default (unless you explicitly disable build caching ). However, if you set one of the following build properties to a different value than shown below, the plugin will disable caching of pre-dexed remote dependencies. (These are the default settings for each property, so if you don't declare them at all, caching for pre-dexed remote dependencies will remain enabled.)

android {
  defaultConfig {// If you do enable multidex, you must also set// minSdkVersion to 21 or higher.multiDexEnabledfalse}
  buildTypes {<build-type>{minifyEnabledfalse}}
  dexOptions {preDexLibrariestrue}...}...
    
    
     
  
     
       
    
  
     
  
  

Note : If your project uses Android plugin version 2.2.2 or 2.2.3, it is using an 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 you can make your builds faster, read Optimizing Your Build Speed .

Change the location of the build cache


By default, the Android plugin saves your cache  <user-home>/.android/build-cache/ in . If you configure one of the following path variables (listed in order of decreasing priority), Android Studio will use  <path-variable>/.android/build-cache/:

  • ANDROID_SDK_HOME
  • user.home
  • HOME

The Android plugin will use a default location for the build cache, so that cached files are shared among all your projects that use the Android plugin 2.3.0 or later (and have not disabled the build cache ). For example, after one of your projects builds and caches a pre-dexed dependency, other projects that also use that dependency can copy it from the shared build cache, again skipping pre-dexing.

If you want a project to create its own cache (and not share that cache with other projects), you can  gradle.properties specify a unique location for the cache in the project's files 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 are finished editing the file, click  Sync Project   to create a new build cache directory.

Note : Avoid specifying directories for your build cache in  <project-root>/build/ or   directory, as Gradle   deletes these directories each time the task is run.<project-root>/<module-root>/build/clean

如果您仅希望将缓存与特定的其他项目共享,请在这些项目的 gradle.properties 文件中指定相同的构建缓存目录。

清除构建缓存


Android 插件的 clean 任务可以清除您的项目的 build/ 目录,与之类似,您可以运行 cleanBuildCache 任务来清除您的项目的构建缓存。如果项目为其构建缓存指定非默认目录,从该项目运行此任务仅会清除相关缓存,而无法清除默认位置中的共享缓存。要执行此任务,请从菜单栏中选择 View > Tool Windows > Terminal,并使用以下命令之一:

  • 在 Windows 上:
    gradlew cleanBuildCache
  • 在 Mac 或 Linux 上:
    ./gradlew cleanBuildCache

:如果您停用构建缓存,cleanBuildCache 任务将不可用。

停用构建缓存


由于构建缓存可以加快您的干净构建的速度,因此不建议停用此功能。如果您仍希望为您的项目停用构建缓存,请将以下内容添加到项目的 gradle.properties 文件中:

// To re-enable the build cache, either delete the following
// line or set the property to 'true'.
android.enableBuildCache=false

完成文件的编辑后,请点击 Sync Project  以应用您的更改。

:在您停用构建缓存后,Android 插件将忽略 android.buildCacheDir 属性,cleanBuildCache 任务将不再可用。同样,停用构建缓存不会自动清除构建目录。如果您决定重新启用构建缓存,这样可以让您保留缓存的文件。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324581717&siteId=291194637