Class: Danger::DangerAndroidLint

Inherits:
Plugin
  • Object
show all
Defined in:
lib/android_lint/plugin.rb

Overview

Lint files of a gradle based Android project. This is done using the Android’s [Lint](developer.android.com/studio/write/lint.html) tool. Results are passed out as tables in markdown.

Examples:

Running AndroidLint with its basic configuration


android_lint.lint

Running AndroidLint with a specific gradle task


android_lint.gradle_task = "lintMyFlavorDebug"
android_lint.lint

Running AndroidLint for a specific severity level and up


# options are ["Warning", "Error", "Fatal"]
android_lint.severity = "Error"
android_lint.lint

See Also:

  • loadsmart/danger-android_lint

Constant Summary collapse

SEVERITY_LEVELS =
["Warning", "Error", "Fatal"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filteringObject

Enable filtering Only show messages within changed files.



55
56
57
# File 'lib/android_lint/plugin.rb', line 55

def filtering
  @filtering
end

#gradle_taskString

Custom gradle task to run. This is useful when your project has different flavors. Defaults to “lint”.

Returns:

  • (String)


44
45
46
# File 'lib/android_lint/plugin.rb', line 44

def gradle_task
  @gradle_task
end

#report_fileString

A getter for ‘report_file`.

Returns:

  • (String)


33
34
35
# File 'lib/android_lint/plugin.rb', line 33

def report_file
  @report_file
end

#severityString

A getter for ‘severity`, returning “Warning” if value is nil.

Returns:

  • (String)


98
99
100
# File 'lib/android_lint/plugin.rb', line 98

def severity
  @severity || SEVERITY_LEVELS.first
end

#skip_gradle_taskObject

Skip gradle task



58
59
60
# File 'lib/android_lint/plugin.rb', line 58

def skip_gradle_task
  @skip_gradle_task
end

Instance Method Details

#lint(inline_mode: false) ⇒ void

This method returns an undefined value.

Calls lint task of your gradle project. It fails if ‘gradlew` cannot be found inside current directory. It fails if `severity` level is not a valid option. It fails if `xmlReport` configuration is not set to `true` in your `build.gradle` file.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/android_lint/plugin.rb', line 66

def lint(inline_mode: false)
  if !skip_gradle_task && !gradlew_exists?
    fail("Could not find `gradlew` inside current directory")
    return
  end

  unless SEVERITY_LEVELS.include?(severity)
    fail("'#{severity}' is not a valid value for `severity` parameter.")
    return
  end

  system "./gradlew #{gradle_task || 'lint'}" unless skip_gradle_task

  unless File.exists?(report_file)
    fail("Lint report not found at `#{report_file}`. "\
      "Have you forgot to add `xmlReport true` to your `build.gradle` file?")
  end

  issues = read_issues_from_report
  filtered_issues = filter_issues_by_severity(issues)

  if inline_mode
    # Report with inline comment
    send_inline_comment(filtered_issues)
  else
    message = message_for_issues(filtered_issues)
    markdown("### AndroidLint found issues\n\n" + message) unless message.to_s.empty?
  end
end