LoginSignup
6
4

More than 5 years have passed since last update.

AndroidアプリをTravisCIでビルドしてDeployGateへアップロードするまで

Last updated at Posted at 2017-08-14

はじめに

TravisCIを利用したAndroidアプリのビルド・DeployGateへアップロードするまでの備忘録。
GithubはOrganizationプライベートリポジトリのため、travis-ci.comを利用している。

環境

  • Android Studio 2.3.3
  • compileSdkVersion 25
  • buildToolsVersion "25.0.2"

TravisCI側設定

GithubとTravisCIの連携については、TravisCIのサイト手順通り進めた。

.travis.ymlがない状態でビルドしないので、 リポジトリのSettings - Generalの「Build only if .travis.yml is present」はONにした。
TravisCIの環境変数は利用していない。publicリポジトリの場合、後述のようなbuild.gradleにトークンベタ書きは避けて環境変数でセットした方が良いと思われる。

Androidプロジェクト側設定

Lintのエラー対応

build.gradle
android {
    (中略)

    // Lintエラーになった場合でも、Gradleによるビルド処理を継続する
    lintOptions {
        abortOnError false
    }
}

travis-yamlの設定

Androidプロジェクトのトップレベルに.travis.ymlファイルを作成する。
最初にyml書いたらまずtravis-yaml検証サイトでチェックしておくと
componentsとすべきところをcomponentとtypoしていた、みたいなしょうもないミスが見つかる。

内容については、ググってコピペたものが多く、例えばcomponentsのtoolsを何故2つ記載しているか理解してない。。
→ 2017/8/18 追記:toolsは2つ記載する必要なかった。

travis.yml
language: android
jdk: oraclejdk8
android:
  components:
    - tools # to get the new 'repository-11.xml'
    # 下記は不要だった
#   - tools
    - platform-tools

    # The BuildTools version used by project
    - build-tools-25.0.2

    # The SDK version used to compile project
    - android-25
    - extra-google-m2repository
    - extra-android-m2repository

  licenses:
    - android-sdk-preview-license-.+
    - android-sdk-license-.+
    - google-gdk-license-.+

# developブランチのみ実行対象に入れる
branches:
  only:
    - develop

before_install:
  - mkdir "$ANDROID_HOME/licenses" || true
  - echo -e "\n8933bad161af4178b1185d1a37fbf41ea5269c55" > "$ANDROID_HOME/licenses/android-sdk-license"
  - echo -e "\n84831b9409646a918e30573bab4c9c91346d8abd" > "$ANDROID_HOME/licenses/android-sdk-preview-license"

install:
  # Update sdk tools to latest version and install/update components
  - echo yes | sdkmanager "tools"
  - echo yes | sdkmanager "platforms;android-25" # Latest platform required by SDK tools
  - echo yes | sdkmanager "extras;android;m2repository"
  - echo yes | sdkmanager "extras;google;m2repository"
  - echo yes | sdkmanager "extras;m2repository;com;android;support;constraint;constraint-layout;1.0.2"
  - echo yes | sdkmanager "extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.2"

script:
  - ./gradlew uploadDeployGateDevelopDebug

notifications:
  email: false
  slack: [slackチーム]:[キー]#[チャンネル]

before_installでは、stackoverflowのこの記事を参考にAndroid SDKのライセンス許諾について記述している。
これがないと以下のようなlogが吐かれてビルドが失敗した。

> You have not accepted the license agreements of the following SDK components:
  [ConstraintLayout for Android 1.0.2, Solver for ConstraintLayout 1.0.2].
  Before building your project, you need to accept the license agreements and complete the installation of the missing components using the Android Studio SDK Manager.
  Alternatively, to learn how to transfer the license agreements from one workstation to another, go to http://d.android.com/r/studio-ui/export-licenses.html

scriptでは、ビルド対象Taskを指定している。
後述のDeployGate Gradleプラグインを導入していると"uploadDeployGate[BuildVariant]"で作成したapkをDeployGateにアップロードしてくれる。

notificationsは、お好みで。
ビルド成功時はメール送信せずに失敗時のみ送信するといった設定も可能らしい。

DeployGateのGradleプラグイン導入

apkをDeployGateにアップロードするためにプラグインを導入する。
導入手順については、公式サイトを参照。
ビルド対象となるBuildVariantに応じてdevelopDebugは置き換える。

build.gradle
// Deploygate設定
deploygate {
    userName = "[ユーザー名]"
    token = "[API Key]"

    apks {
        // 'uploadDeployGateDevelopDebug'タスクで使用されます
        developDebug {

            // set target file
            sourceFile = file("${project.rootDir}/app/build/outputs/apk/app-develop-debug.apk")

            // 便利情報: 現在のコミットの git のハッシュ を使うとトラブルシューティングを楽にできます
            def hash = 'git rev-parse --short HEAD'.execute([], project.rootDir).in.text.trim()
            // ビルドのメッセージとして設定します
            message = "debug build ${hash}"

            // 配布ページを利用している場合、以下の設定で同時に更新することができます
            distributionKey = "[配布ページのURL末尾のハッシュ]"
            releaseNote = "debug build."
        }
    }
}

おわりに

詰まったところは、Android SDKのライセンス許諾周りで後は比較的スムーズに進められた。(ymlの内容については理解してない所が多々あるのでこの辺は要勉強。)
ビルドしてapkをDeployGateにアップロードしているだけでも、CIのはじめの一歩感があってモチベーション上がった。

6
4
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
6
4