Robert
Robert

Reputation: 6226

Run lint when building android studio projects

I would like to be able to run the lint task when I'm building projects with the android studio to ensure the lint rules are being followed.

I have tried using task dependencies but with no luck. My TeamCity build server uses the build task which runs the lint task so that works great. However, the android studio appears to use generateDebugSources and compileDebugJava tasks interchangeably when I have selected the debug build variant.

Here is what I have tried in my build.gradle:

assemble.dependsOn lint

Upvotes: 52

Views: 46674

Answers (8)

AndroidEngineX
AndroidEngineX

Reputation: 1461

More performant answers follow Gradle task configuration avoidance is as below

Gradle kts version

android {
    applicationVariants.configureEach {
        val variantName =
            name.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.ENGLISH) else it.toString() }
        val lintTaskName = "lint$variantName"
        val lintTask = tasks.named("lint$variantName")
        assembleProvider.dependsOn(lintTask)
    }
}

Groovy version

android {
    applicationVariants.configureEach {
        def variantName = name.capitalize()
        def lintTaskName = "lint$variantName"
        def lintTask = tasks.named("lint$variantName")
        assembleProvider.configure {
            dependsOn lintTask
        }
    }
}

Pros:

  1. Didn't call assembleProvider.get() which defeats the purpose of configuration avoidance
  2. Lint task is accessed using a named function which lazily accesses the task when it is required(mentioned at Gradle migration guide)
  3. Uses configureEach which configure each task when required rather than eagarly

Upvotes: 2

Rushabh Shah
Rushabh Shah

Reputation: 670

Just modifying answer of @Yoel Gluschnaider

For me if I use Val it shows error like this : Could not set unknown property 'lintTask' for object of type com.android.build.gradle.internal.api.ApplicationVariantImpl.

So I replace it

applicationVariants.all {
    def lintTask = tasks["lint${name.capitalize()}"]
    assembleProvider.get().dependsOn.add(lintTask)
}

and it work fine!!

Upvotes: 4

stephane k.
stephane k.

Reputation: 1788

just run the "check" task

./gradlew clean check assembleRelease

Upvotes: 12

Yoel Gluschnaider
Yoel Gluschnaider

Reputation: 1814

To do this in build.gradle, add the following lines to your build.gradle:

android {
  applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def lintTask = tasks["lint${variant.name.capitalize()}"]
        output.assemble.dependsOn lintTask
    }
  }
  ...
}

This makes all of your assemble tasks depend on the lint task effectively running them before every assemble call that is executed by Android Studio.

Edit

With Android Gradle Plugin 3.3 and Gradle 5.x this is a revised version using Kotlin script:

applicationVariants.all {
  val lintTask = tasks["lint${name.capitalize()}"]
  assembleProvider.get().dependsOn.add(lintTask)
}

Upvotes: 28

Phileo99
Phileo99

Reputation: 5659

If you want to force Android Studio project to run the lint check before the default run configuration without affecting how your gradle tasks are configured, AND you want to do this in the gradle build system, then you can add the following block outside of the android block in the app module's build.gradle as follows:

android {
....
    lintOptions {
        abortOnError true
    }
}

tasks.whenTaskAdded { task ->
    if (task.name == 'compileDevDebugSources') {
        task.dependsOn lint
        task.mustRunAfter lint
    }
}

Replace compileDevDebugSources with the desired build variant that you have already defined, eg. compileReleaseSources, compileDebugSources, compileStagingDebugSources, etc.

This was tested working on Android Studio 3.0

Upvotes: 5

TmTron
TmTron

Reputation: 19381

here is my solution which also works when you click Build - Make Project in Android Studio:

android {
..
    afterEvaluate {
        applicationVariants.all {
            variant ->
                // variantName: e.g. Debug, Release
                def variantName = variant.name.capitalize()
                // now we tell gradle to always start lint after compile
                // e.g. start lintDebug after compileDebugSources
                project.tasks["compile${variantName}Sources"].doLast {
                    project.tasks["lint${variantName}"].execute()
                }
        }
    }
}

Upvotes: 6

Ross Hambrick
Ross Hambrick

Reputation: 5940

If you just want to configure your Android Studio project to run the lint check before the default run configuration without affecting how your gradle tasks are configured, you can follow these steps.

  1. Open the run configurations drop down and choose edit

enter image description here

  1. Select your app run configuration

enter image description here

  1. Press the '+' to add a new step

enter image description here

  1. Choose "Gradle-aware Make"

enter image description here

  1. Type 'check' and choose the option with your app module name and check. (Mine is :app:check)

enter image description here

  1. Press the up arrow to move the new check step before the existing Gradle-aware make step

enter image description here

Now, Android Studio will run the lint check and fail the build if any lint errors occur.

Upvotes: 44

Andrew Gable
Andrew Gable

Reputation: 2752

To runt lint and analyze your project, simply select Analyze > Inspect Code.

You should get a nice window with all issues.

enter image description here

Also check Run lint in Android Studio for more information.


I did a little more research, try adding this to your build.gradle.

lintOptions {
      abortOnError true
  } 

There are many options that you can apply to the build.gradle

Upvotes: 42

Related Questions