Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Once upon a time, particularly in Eclipse-land, Lint would run on every build, and so if you failed Lint checks, you would find out immediately. With Android Studio (tested on 1.3), Lint does not run by default on a build. Newcomers might make mistakes that Lint would check, but since Lint doesn't actually run, the newcomers don't find out about them.

(IOW, if a Lint test did not run in a forest, is there really a Lint test?)

Comments on this blog post show how to kinda sorta get Lint to run as part of a build:

  • Edit the project configuration
  • In the General tab of the configuration, scroll down and fold open the "Before launch" panel
  • In that panel, add a step to "Run Gradle Task" named lint for your module

However, this runs command-line Lint, resulting in reports written in XML and HTML to your hard drive. That works, but it would be cleaner to have Android Studio run its in-IDE Lint checks, so the results show up in an IDE panel.

Is there a way to set up a project build to perform the in-IDE Lint checks?

Bonus points if it could be set up to only run Lint, not the full analysis done by Analyze > Inspect Code. While the full analysis is occasionally useful, Lint is slow enough as it is, let alone the dubiously-useful other analysis performed by Android Studio (e.g., spellchecking).

While setting this up would not be a great plan for all projects (Lint scans are slow), for newcomers to Android, it might be an appropriate move.

like image 361
CommonsWare Avatar asked Sep 17 '15 13:09

CommonsWare


People also ask

How do I run lint on Android?

Run lint using the standalone tool If you're not using Android Studio or Gradle, you can use the standalone lint tool after you install the Android SDK Command-Line Tools from the SDK Manager. You can then locate the lint tool at android_sdk /cmdline-tools/ version /bin/lint .

What is build configuration in Android?

The Android build system compiles app resources and source code, and packages them into APKs or Android App Bundles that you can test, deploy, sign, and distribute.

What is default config in Android Studio?

Specifies defaults for properties that the Android application plugin applies to all build variants. Specifies defaults for properties that the Android dynamic-feature plugin applies to all build variants. Specifies defaults for properties that the Android library plugin applies to all build variants.


2 Answers

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.

like image 177
Ray Hunter Avatar answered Sep 26 '22 09:09

Ray Hunter


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 info dirToLint=$(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 check echo "Performing pre-commit lint check of ""$dirToLint" ./gradlew lint lintStatus=$?  if [ $lintStatus -ne 0 ] then   echo "Lint failure, git push aborted."   echo "Open ${projectDir}${lintReportPath} in a browser to see Lint Report"   exit 1 fi  exit $lintStatus 
like image 23
Lawrence Martin Avatar answered Sep 23 '22 09:09

Lawrence Martin