1. 程式人生 > >Using Gradle build cache with Kotlin

Using Gradle build cache with Kotlin

Eric Wendelin
This is a guest blog post from Eric Wendelin
software engineer for Gradle

A build cache allows Gradle to reuse task output from any previous invocation, including those from other machines. Kotlin 1.2.21 allows Kotlin projects to make use of build caching.

The build cache works by storing compiled classes, test outputs, and other build artifacts in a cache, taking into account all task inputs, including input file contents, relevant classpaths, and task configuration.

Build Cache topological diagram

This frequently results in faster builds. The following chart shows aggregated build time with and without the build cache for part of Gradle’s CI:

Build minutes saved with Gradle build cache

In this post, we’ll explain how you can use Gradle’s build cache to avoid unnecessary Kotlin compilation to speed up your builds.

Quick demo with Spek

You can try out build caching with Gradle right now. Just follow these steps:

Clone Spek

123 git clonehttps://github.com/spekframework/spek.gitcd spek

The Spek 2.x

branch (which is the default) already has all prerequisites for build caching that we’ll describe later.

Build and populate cache

The following command builds Spek and populates the local build cache.

12345 ./gradlew assemble--build-cacheBUILD SUCCESSFUL in10s21actionable tasks:21executed

Using the --build-cache flag is one way to tell Gradle to store outputs in a separate task output cache.

Remove/change build outputs

This simulates being on another machine or perhaps making a change and stashing it. The quickest way to demonstrate is use the clean task.

12 ./gradlew clean

Re-build and resolve from build cache

This time when we re-build, all Kotlin compiled sources are pulled from the build cache.

12345 ./gradlew assemble--build-cacheBUILD SUCCESSFUL in2s21actionable tasks:11executed,10from cache

Voilà! You just used Gradle’s build cache to reuse Kotlin compiled classes instead of recompiling again! The build was about 5x faster!

You can see from this build scan that Kotlin compile tasks were pulled from the build cache; :jar and :processResources tasks were not because it’s faster to generate JARs and copy files locally than pull from a cache. Note that caching :test tasks is also supported.

The Gradle build cache is particularly effective when a CI instance populates a shared build cache, which developers can pull from. Links to more resources for achieving this are listed below.

Enabling the build cache for your projects

I hope you’re excited to try this out on your project — you can follow these steps to enable the build cache.

First, you need to ensure you’re using Gradle 4.3 or above, so the Kotlin Gradle Plugin can opt-in to using new APIs in Gradle. You can upgrade Gradle easily using the Gradle wrapper.

Next, we need to ensure we are compiling with Kotlin version 1.2.20 or above. You might have something like this declared in your buildscript {} block in build.gradle:

1234 dependencies{classpath"org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.21"}

Next, we need to tell Gradle to use the build cache. There are 3 ways to do this:

  • Enable for the current build using --build-cache on the command-line.
  • Enable for the project by adding org.gradle.caching=true to $PROJECT_ROOT/gradle.properties.
  • Enable for all builds for the current user by adding org.gradle.caching=true to $GRADLE_HOME/gradle.properties.

NOTE: Android developers still need to do this even if android.enableBuildCache=true is set, because Gradle’s build cache is separate from the Android build cache.

We can optionally take advantage of the build cache from IDEs by delegating run and test actions to Gradle.

Enabling build caching in IntelliJ

If you use IntelliJ to execute Gradle actions, you will need to “Delegate IDE build/run actions to Gradle” in your IDE settings to take advantage of build caching when building and running tests from IntelliJ.

Delegate IDE build/run to Gradle

NOTE: Android Studio does this by default.

Caching kapt tasks

Caching for kapt is currently disabled by default, even with --build-cache, because Gradle does not yet have a way to map inputs and outputs for annotation processors. You can explicitly enable use of the build cache for Kotlin annotation processing tasks by setting useBuildCache to true in kapt configuration.

1234 kapt{useBuildCache=true}

Further reading

You can learn more about leveraging the Gradle build cache through these resources:

Conclusion

Compiling Kotlin code using kotlin-gradle-plugin version 1.2.20 and above can take advantage of Gradle’s --build-cache feature to speed up your development cycle. Work continues to expand the set of tasks that support build caching.

Onward!

相關推薦

Using Gradle build cache with Kotlin

This is a guest blog post from Eric Wendelin software engineer for Gradle A build cache allows Gradle to reuse task output from any previ

android studio 不能生成apk:Gradle build finished with 1 error(s) in 5 sec Unexpected error Local path do

大概原因是sdk版本過高無法自動生成apk(ps:無法生成還弄那麼高版本也是醉了); 解決辦法是:先將android studio 關閉,找一個低版本的sdk,將sdk中的tools檔案複製;                         將android stu

android studio編譯報錯Gradle build finished with 100 error(s) in 14s 629ms

 這兩天開始接觸android  studio,發現這工具還是有很多問題,經常有版本不匹配的問題,今天就遇到了一個  Gradle build finished with 100 error(s) in 14s 629ms。無法編譯生成apk,最後查詢資料,發現是bui

Android--Gradle build finished with 232 error(s) in 1m 43s

Android–Gradle build finished with 232 error(s) in 1m 43s What is this?(這是啥?) 這些很多可能性是我們在Android 的開發過程中使用了過時的javaAPI,或者不太規範的程式設

intellij idea更新gradle專案報錯:Could not run build action using Gradle distribution

intellij idea中有一個gradle專案,之前都一直是好好的。 今天在build.gradle中添加了一個dependency,由於我並沒有設定auto-import,所以選擇自己來refresh整個gradle專案 結果gradle task後臺stuck在

解決【Error:Could not run build action using Gradle distribution 】問題記錄

1.問題詳情:Error:Could not run build action using Gradle distribution 'https://services.gradle.org/distributions/gradle-4.1-all.zip'.Consult I

android studio報錯:Could not execute build using Gradle installation

問題出現在更新到gradle1.8之後。 報錯資訊主要是  Could not execute build using Gradle installation '/Users/Admin/.gradle/wrap... 分析問題,不是配置的問題,因為之前gradle1.8還

加快AS的Gradle Build速度

led read href mod should java se enable mic 推薦 隨著項目的代碼以及依賴庫的日益增多,構建的速度卻是越來越慢,Android Studio2.0 之後提供了Instant Run貌似福利來了,但經過一段時間的使用發現不盡如人意,最

Gradle Build速度加快終極方法(android studio)

all ref bsp .net rms perm lan ase fcm Android Studio用起來越來越順手,但是卻發現Build的速度實在不敢恭維,在google和度娘了幾把(....)之後,大體就是分配更高的內存,步驟:Setting-->搜索grad

androidstudio一直卡在gradle build進度的解決方法

gradle uil ids enc maxperm tof studio prop odi 打開項目的 gradle配置文件 gradle.properties 配置: org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=1024

【轉載】Android Gradle Build Error:Some file crunching failed, see logs for details解決辦法

mage erl 博客 details efault roi 成了 options 文件 錯誤日誌:Error:java.lang.RuntimeException: Some file crunching failed, see logs for details Lo

FAILURE: Build failed with an exception.

exceptio jdk run span eva project conf code nbsp 執行react-native run-android報錯: A problem occurred configuring project ‘:app‘. Failed to

修改了SpringBoot的主類名稱後,gradle build報錯的解決辦法

ica app ida following images .com blog oot sin Unable to find a single main class from the following candidates [*.*Application] 修改了Sp

build tree with balanced parenthesis

ati nta else count tor form sharp ssi nod Given a tree string expression in balanced parenthesis format:[A[B[C][D]][E][F]].Construct a

using ThreadLocal to cache data in request scope

thread stack;reques/** * aim to cache the data that‘s accessed frequently and costly. * @param <K> * @param <V> */ public interface Cache&l

Up-to-date cache with EclipseLink and Oracle

property included conflict rec main enable previous party toc Up-to-date cache with EclipseLink and Oracle One of the most useful fe

Gradle Build速度加快方法匯總

open gradle echo PV 內容 containe 生效 增量 info Android Studio用起來越來越順手,但是卻發現Build的速度實在不敢恭維,在google和度娘了幾把(....)之後,大體就是分配更高的內存,步驟:Setting-->搜

Mac android studio 一直卡在Gradle:Build Running的解決辦法

1.找到路徑/Users/michael/.gradle/wrapper/dists/gradle-4.1-all/bzyivzo6n839fup2jbap0tjew,在此資料夾下有一個gradle版本資料夾,開啟後是一個名字很長的資料夾,例如我的/Users/michael/.gradle/wrapper/

Author name disambiguation using a graph model with node splitting and merging based on bibliographic information

分隔 需要 sin 相似性度量 進行 ati 判斷 特征向量 edi Author name disambiguation using a graph model with node splitting and merging based on bibliographic

Build LightGBM with GPU support

official tutorial 1) Build LightGBM git clone --recursive https://github.com/Microsoft/LightGBM cd LightGBM mkdir build ; cd build cmake -DUS