Slide 1

Slide 1 text

Cache Once and Use Everywhere Sinan Kozak Delivery Hero @snnkzk Leveraging Gradle build cache

Slide 2

Slide 2 text

How to speed up Android build times? Avoid building as much as possible

Slide 3

Slide 3 text

Lifecycle of build Not every compile is equal 1. Pull the source code 2. Start build 3. Download dependencies 4. Compile without any cache 5. Change something 6. Build everything again

Slide 4

Slide 4 text

Gradle task execution output Execution of task can be avoided ● No source ● From source ● Up to date ● Incremental from build output ● Local cache ● Remote cache

Slide 5

Slide 5 text

Cache is hardware or software component that stores data so that future requests for that data can be served faster; the data stored in a cache might be the result of an earlier computation or a copy of data stored elsewhere. https://en.wikipedia.org/wiki/Cache_(computing) What is cache?

Slide 6

Slide 6 text

Which cache is useful for development? There are different caches ● Android Studio ● Downloaded dependencies ● Android build pipeline ● Incremental build ● Gradle configuration ● Gradle build

Slide 7

Slide 7 text

Which cache is broken? What is not related to build Android Studio cache Build output ./gradlew clean Incremental build

Slide 8

Slide 8 text

The Gradle build cache is a cache mechanism that aims to save time by reusing outputs produced by other builds. The build cache works by storing (locally or remotely) build outputs and allowing builds to fetch these outputs from the cache when it is determined that inputs have not changed, avoiding the expensive work of regenerating them. https://docs.gradle.org/current/userguide/build_cache.html

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Benefits of cache Remote and Local cache usage ● Less compile ● Less time to develop ● Less context switch because of waiting ● Less energy usage ● Less money spending More productivity ☀🌈

Slide 12

Slide 12 text

“The world ain't all sunshine and rainbows” Rocky Balboa

Slide 13

Slide 13 text

Why does build miss cache? Some examples Not deterministic generated files ● Time ● Commit hash ● Machine host name Non deterministic configuration

Slide 14

Slide 14 text

Cache miss About Gradle tasks No input or output Non deterministic task input Same input and output target Not properly declared input or output Room library - https://issuetracker.google.com/issues/132245929

Slide 15

Slide 15 text

How to fix cache issues? Is it easy to find cache miss reason?

Slide 16

Slide 16 text

Cache on CI Local vs CI ● Single source for cache ● Consistent cache without local changes ● Less upload and bandwidth usage for local devices ● Only affected Unit Tests will run

Slide 17

Slide 17 text

Gradle remote cache server How can I use? A. Gradle Enterprise ○ Build in cache server node B. You can manage own cache server with persistent storage and ○ Jar ○ Docker ○ Kubernetes https://docs.gradle.com/build-cache-node/

Slide 18

Slide 18 text

Don’t want to setup a server; Use Google Cloud Storage buckets https://github.com/androidx/gcp-gradle-build-cache

Slide 19

Slide 19 text

AndroidX repo uses Google Cloud Storage solution. https://github.com/androidx/androidx

Slide 20

Slide 20 text

Is remote cache always better? Local build can be faster Some tasks could execute faster than using remote cache Copy, package, merge …

Slide 21

Slide 21 text

Cache or not to Cache Is cache a Golden Hammer? 🔨 ● DataBindingMergeDependencyArtifacts ● BundleLibraryClasses ● BundleLibraryClassesDir Those were cacheable until AGP 7.2.0-alpha06 Executing them locally is unlikely slower than cache usage Android Gradle Cache fix plugin helps lots of issues https://github.com/gradle/android-cache-fix-gradle-plugin

Slide 22

Slide 22 text

Lifecycle of remote cache Download and zip could be slow Remote cache ● Download ● Unzip ● Use Big cache entries will also affect network usage and bandwidth usage Gradle Doctor helps to detect negative gains https://github.com/runningcode/gradle-doctor

Slide 23

Slide 23 text

Gradle build cache How much we can reuse? Hitting 80%-90% range of cache usage is great When we started our remote cache usage was 30% Our current cache usage is above 85%

Slide 24

Slide 24 text

How to go forward? Disabling cache should be a temporary solution It will cost more time and money in long run If you have reproducible case, please report issue Millions of developers will benefit from it Consider remote cache and calculate the difference between Start with suitable solution

Slide 25

Slide 25 text

Grazie mille Q/A? Sinan Kozak Delivery Hero @snnkzk