LoginSignup
3
2

More than 5 years have passed since last update.

CircleCI 2.0でAndroidアプリのversionCodeにビルド番号を付ける

Posted at

CircleCI 1.0と2.0で環境変数の使い方が異なり、小一時間ハマったのでメモ。

参考

やり方

.circleci/config.yml
version: 2
jobs:
  build:
    working_directory: ~/code
    docker:
      - image: circleci/android:api-26-alpha
    environment:
      JVM_OPTS: -Xmx3200m
    steps:
      - checkout
      - restore_cache:
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}
+     - run:
+         name: Export version code
+         command: echo 'export ORG_GRADLE_PROJECT_VERSION_CODE="$CIRCLE_BUILD_NUM"' >> $BASH_ENV
      - run:
          name: Download Dependencies
          command: ./gradlew androidDependencies
      - save_cache:
          paths:
            - ~/.gradle
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}
      - run:
          name: Run Builds
          command: ./gradlew assembleDebug
      - store_artifacts:
          path: ./app/build/outputs/apk/app-debug.apk
          destination: /app-debug.apk
app/build.gradle
android {
    defaultConfig {
        applicationId "com.example.hoge"
        minSdkVersion 26
        targetSdkVersion 26
-       versionCode 1
+       versionCode VERSION_CODE as int
        versionName "1.0"
    }
}
gradle.properties
+ VERSION_CODE=1

何をしているか

.circleci/config.yml

  • build.gradleで置き換えるVERSION_CODE(CIRCLE_BUILD_NUM)をBASH_ENVへ出力している
    • 「ORG_GRADLE_PROJECT_」というプレフィックスで始まる変数を定義しておくと、Gradleのビルドスクリプト内から、プロジェクトのプロパティとして参照できるようになる

app/build.gradle

  • VERSION_CODEをCIRCLE_BUILD_NUMに置き換えている

gradle.properties

  • ローカル環境でビルドエラーとならないように、デフォルト値を設定している
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2