LoginSignup
1
0

More than 3 years have passed since last update.

AndroidのマルチモジュールでLint結果をDangerでコメントする

Posted at

Androidアプリ開発をマルチモジュールですすめていた場合、Lintを実行すると各モジュールのbuildフォルダ配下にlintチェック結果が生成されてしまうため、Danger等でプルリクにコメントしていた場合にファイルを1つしか選択できず、特定のモジュールのみのコメントになってしまいます。そのため、各モジュールの結果を1つのファイルにマージしてあげてからDangerにそのファイルを指定してあげる ことで解決させます。

詳しい説明やBitriseでの設定などを ブログ のほうに記載したので、こちらでは方法のみを記載します。

lintの設定

各モジュールで以下のlint設定を行い、lint結果のファイルがプロジェクトのルートの build-reports というフォルダに集約されるようにします。

build.gradle
android {
    lintOptions {
        xmlReport true
        xmlOutput rootProject.file("./build-reports/lint-results-${project.getDisplayName()}.xml")
    }
}

lintチェック結果のXMLファイルをマージする

Lint実行後に、生成されたXMLファイルを全てマージした新しいXMLファイルを生成します。そのために、以下のRubyスクリプトをプロジェクトのルートで実行します。実行にはnokogiriが必要なのでインストールしておきます。

gem install nokogiri
merge_lint_results.rb
require 'nokogiri'

Dir.chdir('build-reports')

new_doc = nil

Dir::glob('lint-results-*.xml') do |item|
  file = File.new(item)

  if new_doc.nil?
    new_doc = Nokogiri.XML(file)
  else
    doc = Nokogiri.XML(file)
    issues = doc.search('issue')
    new_doc.at('issues').add_child(issues)
  end
end

File.open('lint-results.xml', 'w') do |file|
  unless new_doc.nil?
    file.puts(new_doc.to_xml(indent: 4))
  end
end
ruby ./merge_lint_results.rb

すると build-reportslint-results.xml というファイルが生成されます。

Dangerでファイルを指定する

あとはDangerfileで生成された lint-results.xml を指定してあげます。

android_lint.skip_gradle_task = true
android_lint.filtering = false
android_lint.report_file = "build-reports/lint-results.xml"
android_lint.lint(inline_mode: true)
1
0
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
1
0