Help us understand the problem. What is going on with this article?

CircleCIでAndroid SDKとGradleをキャッシュする

More than 1 year has passed since last update.

CircleCIで普通にビルドしていると毎回Android SDKとGradleのダウンロードが行われてビルド時間が長くなってしまいます。CircleCIにはデフォルトのキャッシュ以外にも自分でキャッシュの設定ができるので、Android SDKとGradleをキャッシュすることでビルドを短くできます。~/.gradleはデフォルトでキャッシュされるようになっているようなので設定は不要です。

dependencies:
  override:
   ...
  cache_directories:
    - /usr/local/android-sdk-linux/tools
    - /usr/local/android-sdk-linux/platforms/android-25
    - /usr/local/android-sdk-linux/platforms/android-23
    - /usr/local/android-sdk-linux/platforms/android-16
    - /usr/local/android-sdk-linux/build-tools/25.0.2

キャッシュを使うかダウンロードするかの判定

tools, platforms/android-xxにはsource.propertiesというファイルがあります。これにバージョンがPkg.Revision=25.2.5のように書かれているので、この値を見ることでキャッシュを使うかダウンロードするかを決めます。build-toolsとGradleは
フォルダがあるかどうかで判定します。

dependencies:
  override:
    - if ! $(grep -q "Pkg.Revision=25.2.5" $ANDROID_HOME/tools/source.properties); then echo y | android update sdk --no-ui --all --filter "tools"; fi
    - if [ ! -e $ANDROID_HOME/build-tools/25.0.2 ]; then echo y | android update sdk --no-ui --all --filter "build-tools-25.0.2"; fi
    - if ! $(grep -q "Pkg.Revision=3" $ANDROID_HOME/platforms/android-25/source.properties); then echo y | android update sdk --no-ui --all --filter "android-25"; fi
    - if ! $(grep -q "Pkg.Revision=3" $ANDROID_HOME/platforms/android-23/source.properties); then echo y | android update sdk --no-ui --all --filter "android-23"; fi
    - if ! $(grep -q "Pkg.Revision=5" $ANDROID_HOME/platforms/android-16/source.properties); then echo y | android update sdk --no-ui --all --filter "android-16"; fi
    - if [ ! -e ~/.gradle/wrapper/dists/gradle-3.5-all ]; then ./gradlew init; fi
  cache_directories:
    - /usr/local/android-sdk-linux/tools
    - /usr/local/android-sdk-linux/platforms/android-25
    - /usr/local/android-sdk-linux/platforms/android-23
    - /usr/local/android-sdk-linux/platforms/android-16
    - /usr/local/android-sdk-linux/build-tools/25.0.2

注意点として先にAndroid SDKのアップデートをしないとGradleの初期化中にAndroid SDKの使用許諾が取れていなくて失敗します。

circle.yml

machine:
  java:
    version: openjdk8
  environment:
    ANDROID_HOME: /usr/local/android-sdk-linux
dependencies:
  override:
    - if ! $(grep -q "Pkg.Revision=25.2.5" $ANDROID_HOME/tools/source.properties); then echo y | android update sdk --no-ui --all --filter "tools"; fi
    - if [ ! -e $ANDROID_HOME/build-tools/25.0.2 ]; then echo y | android update sdk --no-ui --all --filter "build-tools-25.0.2"; fi
    - if ! $(grep -q "Pkg.Revision=3" $ANDROID_HOME/platforms/android-25/source.properties); then echo y | android update sdk --no-ui --all --filter "android-25"; fi
    - if ! $(grep -q "Pkg.Revision=3" $ANDROID_HOME/platforms/android-23/source.properties); then echo y | android update sdk --no-ui --all --filter "android-23"; fi
    - if ! $(grep -q "Pkg.Revision=5" $ANDROID_HOME/platforms/android-16/source.properties); then echo y | android update sdk --no-ui --all --filter "android-16"; fi
    - if [ ! -e ~/.gradle/wrapper/dists/gradle-3.5-all ]; then ./gradlew init; fi
  cache_directories:
    - /usr/local/android-sdk-linux/tools
    - /usr/local/android-sdk-linux/platforms/android-25
    - /usr/local/android-sdk-linux/platforms/android-23
    - /usr/local/android-sdk-linux/platforms/android-16
    - /usr/local/android-sdk-linux/build-tools/25.0.2
test:
  override:
    - ./gradlew assembleDebug
    - cp -r ~/$CIRCLE_PROJECT_REPONAME/app/build/outputs/apk/* $CIRCLE_ARTIFACTS
      # unit test
    - ./gradlew testDebugUnitTest 
    - cp -r ~/$CIRCLE_PROJECT_REPONAME/app/build/test-results/testDebugUnitTest/* $CIRCLE_TEST_REPORTS
deployment:
  master:
    branch: master
    commands:
      - ./gradlew assembleRelease

参考

https://circleci.com/docs/1.0/how-cache-works/
https://discuss.circleci.com/t/installing-android-build-tools-23-0-2/924/6

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away