How Do We Configure Android Studio to Run Its Lint on Every Build? How Do We Configure Android Studio to Run Its Lint on Every Build? android android

How Do We Configure Android Studio to Run Its Lint on Every Build?


Lint should be running in Android Studio unless you have configured it to be off via the lintOptions in your build.gradle file.

Here is from the documentation found at http://developer.android.com/tools/debugging/improving-w-lint.html#studio

In Android Studio, the configured lint and IDE inspections run automatically whenever you build your app. The IDE inspections are configured along with the lint checks to run IntelliJ code inspections to streamline code review.

Note: To view and modify inspection severity levels, use the File > Settings > Project Settings menu to open the Inspection Configuration page with a list of the supported inspections.

With Android Studio, you can also run lint inspections for a specific build variant, or for all build variants from the build.gradle file. Add the lintOptions property to the android settings in the build file. This code snippet from a Gradle build file shows how to set the quiet option to true and the abortOnError option to false.

android {    lintOptions {       // set to true to turn off analysis progress reporting by lint       quiet true       // if true, stop the gradle build if errors are found       abortOnError false       // if true, only report errors       ignoreWarnings true       }       ...}

To manually run inspections in Android Studio, from the application or right-click menu, choose Analyze > Inspect Code. The Specify Inspections Scope dialog appears so you can specify the desired inspection scope and profile.

Here are other lint options that you can add to your lintOptions block in your gradle build.gradle file: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Lint-support

Here is more information on android lint: http://developer.android.com/tools/help/lint.html

It use to be that you could add gradle tasks after certain actions in android studio.

  • Open up the Gradle tab on the right side of the screen
  • Select your task

Android Studio Gradle Tab

  • Right click on the task
  • Choose your execution action

Android Studio Execution Action

This should run the task when it has been scheduled for execution.


I accomplished this previously by adding a pre-push git hook that would run lint automatically on push, and fail to push if Lint errors were found. The pre-push hook script was stored in the Android project repo and was installed to a user's local machine automatically via gradle.

install-git-hooks.gradle

def hookDest = new File("${rootProject.rootDir}/.git/hooks/pre-push")def prePushHook = new File("${rootProject.rootDir}/pre-push")task installGitHooksTask(type: Copy) {    hookDest.delete()    hookDest << prePushHook.text}task gitExecutableHooks() {    Runtime.getRuntime().exec("chmod -R +x ${hookDest}");    println "gitExecutableHooks"}gitExecutableHooks.dependsOn installGitHooksTask

Than in your app build.gradle

apply from: rootProject.file('gradle/install-git-hooks.gradle')

pre-push

#!/bin/sh## This hook is for Android project git repos.## You can use git config variables to customize this hook.# -----------------------------------------------------------# Change hooks.lintTargetDirectory to point at a non . directory# Change hooks.lintArgs to add any custom lint arguments you prefer# Get custom infodirToLint=$(git config hooks.lintTargetDirectory)lintArgs=$(git config hooks.lintArgs)projectDir=$(git rev-parse --show-toplevel)lintReportPath="/app/lint-report.html"# If user has not defined a preferred directory to lint against, make it .if [ -z "$dirToLint"]  then  dirToLint="."fi# Perform lint checkecho "Performing pre-commit lint check of ""$dirToLint"./gradlew lintlintStatus=$?if [ $lintStatus -ne 0 ]then  echo "Lint failure, git push aborted."  echo "Open ${projectDir}${lintReportPath} in a browser to see Lint Report"  exit 1fiexit $lintStatus


Create inspection profile for Android Lint

  1. Go to File -> Settings -> Editor/Inspection
  2. Select Manage -> Copy
  3. Give it name "Android Lint Profile" and press Enter
  4. Leave marked only Android Lint rules on this tab

Android Lint Profile

Now you can run inspection only with Android Lint rules via Analyze->Inspect Code... by selecting "Android Lint Profile".

On the next step, let's record Macro (Edit->Macros->Start Macro Recording) with next steps.

  1. Analyze->Inspect Code...
  2. Select "Android Lint Profile"
  3. Press Enter (it is important to use keyboard because Macro recorder doesn't catch mouse clicks on this windows)
  4. Click on "Run Selected Configuration" (green play button)

Stop the macro recording and give it the name "Lint and Run".

Final thing that should be done is mapping "Shift+F10" to our macro. Go to File->Settings...->Keymap. Find our macro and change shortcut.

macro keymapping

Now lint will be run before each build when you press Shift+F10 and results will be in Android Studio panel.

But this solution have one big drawback. If you run your build by clicking on the Run button lint analysis will not be executed.

May be this info will be helpful and someone will provide better solution using it.