How to cache Gradle dependencies

build-cache

#1

Setup

  1. Put the Cache:Pull step after the Git Clone step
  2. Put the Cache:Push step to the very end of the Workflow

If you use v1.0 or newer of the Cache steps and the latest Gradle Runner step then that’s all you have to do.

If you use an older Cache or Gradle Runner step:

  1. Select the Cache:Push step and specify the following paths for caching (Cache paths input):
    $HOME/.gradle
    ./.gradle
    
  2. Select the Cache:Push step and specify the following paths for update-check ignore (Ignore Paths from change check input):
    $HOME/.gradle/caches/*.lock
    $HOME/.gradle/caches/*.bin
    ./.gradle/*.lock
    ./.gradle/*.bin
    

Example workflow

example:
  steps:
  - git-clone: {}
  - cache-pull: {}
  - gradle-runner:
      inputs:
      - gradle_file: "$BITRISE_PROJECT_PATH"
      - gradle_task: "$GRADLE_TASK"
  - cache-push:
      inputs:
      - cache_paths: |-
          $HOME/.gradle
          ./.gradle
      - ignore_check_on_paths: |-
          $HOME/.gradle/caches/*.lock
          ./.gradle/*.lock
          ./.gradle/*.bin

For more information about the Bitrise Build Cache see the DevCenter.


Android build and gradle
Could not resolve all files for configuration
Could not resolve all files for configuration ':classpath'
#2

Hi Viktor,
I encounter an issue with the Cache:Pull step, the step seems failed to download or extract the cache archive, it produce this log

2017/09/28 04:19:46 => > Downloading and extracting cache archive …
2017/09/28 04:19:46 [!] Unable to download or uncompress cache: failed to extract tar archive, output: gzip: stdin: not in gzip format
tar: Child died with signal 13
tar: Error is not recoverable: exiting now, error: exit status 2, retrying…
2017/09/28 04:20:33 => [DONE]
2017/09/28 04:20:33 => Took: 47.71805638s
2017/09/28 04:20:33 => Finished

I’ve checked the Cache:Push step from the previous build and it finished successfully.


#3

Hi @fchristysen,

Can you please create an issue report at #issues:build-issues, or via email or the onsite chat?


#4

Hi, im trying to cache gradle with this below. but it seems it’s not working.
it takes the same amount of time every build.

  - cache-push:
      inputs:
      - cache_paths: |-
          $HOME/.gradle
          ./.gradle
      - ignore_check_on_paths: |-
          $HOME/.gradle/caches/*.lock
          ./.gradle/*.lock
          ./.gradle/*.bin

especially i want to cache $HOME/.gradle, which contains local caches of gradle.

does this have something to do with this?
http://devcenter.bitrise.io/faq/why-my-build-takes-longer-on-bitrise-than-on-my-mac/


#5

@taku Without more info it’s really hard to say anything useful.

I’d suggest you to upgrade all the related steps in the workflow, and if that wouldn’t help please create a #issues:build-issues report so that we’ll have enough info to debug it :wink:


#6

sorry, i figured it out. it was because PR. PR is not cashed, unless I set run_if: true


#7

Indeed, the Cache:Push step is configured to not to push back cache in case of PR builds. In general we’d suggest you to keep that default behaviour, but of course if you have a use case which requires / benefits from pushing back from pull request builds the run_if: true option can be used :slight_smile:


#8

Hi @viktorbenei, does this eliminate cleanBuildCache or clean in Gradle build?
For example
Given

  • ./graldew clean cleanBuildCache assembleDebug takes 20 mins for my app
  • ./graldew clean assembleDebug takes 10+ mins for my app
  • ./graldew cleanBuildCache assembleDebug takes 10+ mins for my app
  • ./graldew assembleDebug takes <10 mins for my app
    By using these cache-pull and cache-pull steps, what will my build time be?

#9

You have to actually run a build to find out what the build time will be. :wink: I have noticed a good improvement by caching the gradle dependencies.


#10

Hi @viktorbenei

The caching gradle system works fine on my Android project! Thanks!
I would like to know where I can find the logic of Bitrise for detecting when it can re-use the pulled cache or not; I assume it is somewhere in the Steps but I still can’t find it… sorry… :frowning:

What I would like to do is to make sure NOT to re-use the cached data if certain project-specific file in the project(e.g. gradle file) is modified whenever changes are pushed.

Thanks in advance!

Shohei