LoginSignup
7
2

More than 5 years have passed since last update.

CircleCI2.0でDangerを使いPullRequestにAndroidLintをかけて学んだこと

Last updated at Posted at 2017-10-20

Dangerとは

GitHub等のPullRequestをCI上から検査し、問題点をコメントするツール

  • プルリクエストの内容が長すぎると注意をコメントしてくれる
  • AndroidLintの結果をコメントしてくれる

参考: https://qiita.com/noboru_i/items/011fd5ef1808bea3d6ba

学び1: KotlinのAndroidLintはコマンドラインでは動かないのでDangerもコメントしない

cf. https://developer.android.com/studio/preview/kotlin-issues.html

Lint checks with Kotlin don't work from command line Lint checks on Kotlin files only work from the Android Studio IDE.
The currently do not run when you run lint directly from Gradle.

なので頑張ってセットアップしたのにxml系しかコメントしてくれない(辛)

追記

AndroidStudio3.1からコマンドラインからでも動くようになりそうです
https://developer.android.com/studio/preview/features/index.html

When you run lint from the command line, lint now also analyzes your Kotlin classes.

学び2: 複数のモジュールを管理している場合の設定方法

状況

以下のようなスタンダードな構成ではなく

(Project Root)
 |
 |--app

以下のように複数のモジュールを管理している状況

(Project Root)
 |
 |--app1
 |
 |--app2
 | 
 |--app3

問題点

DangerのAndroidLintプラグインが一つのlintレポートファイルしか参照しない
cf. https://github.com/loadsmart/danger-android_lint/blob/master/lib/android_lint/plugin.rb#L37

    # Location of lint report file
    # If your Android lint task outputs to a different location, you can specify it here.
    # Defaults to "app/build/reports/lint/lint-result.xml".
    # @return [String]
    attr_accessor :report_file
    # A getter for `report_file`.
    # @return [String]
    def report_file
      return @report_file || 'app/build/reports/lint/lint-result.xml'
    end

なので https://qiita.com/noboru_i/items/2ad7c07d8b4266c9eb5b などを参考にしてDangerfileを

android_lint.gradle_task = "app:lint"
android_lint.report_file = "app/build/reports/lint-results.xml"
android_lint.filtering = true
android_lint.lint(inline_mode: true)

と設定しても一つのモジュールに対してしか動作してくれない。

解決法

DangerFileをモジュールごとに作成し、CIからモジュールごとにDangerを動かすようにした。

CircleCI(2.0)での設定の例

.circle/config.yml (一部)
###
# コメントが上書きされないように別々のdanger_idを付与。danger_idがないとapp1やapp2に対するDangerのコメントが、app3に対するDangerのコメントで上書きされてしまう。
###
     - run:
         name: Run danger for app1
         command: bundle exec danger --dangerfile="app1/Dangerfile" --danger_id=1
     - run:
         name: Run danger for app2
         command: bundle exec danger --dangerfile="app2/Dangerfile" --danger_id=2
     - run:
         name: Run danger for app3
         command: bundle exec danger --dangerfile="app3/Dangerfile" --danger_id=3
app1モジュールのDangerfileの例
# PRの箇所のみコメントする
github.dismiss_out_of_range_messages

# Android Lint
android_lint.gradle_task = ":app1:lintDebug"
android_lint.report_file = "app1/build/reports/lint-results-debug.xml"
android_lint.filtering = true
android_lint.lint(inline_mode: true) 
7
2
2

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
7
2