Launch lint when creating Android studio projects

I would like to be able to run the lint task when I build projects using android studio to ensure that lint rules are followed.

I tried using task dependencies but no luck. My teamcity build server uses a build task that runs the lint task, so this works great. However, Android Studio suggests using the generateDebugSources and compileDebugJava functions interchangeably when I chose the debug build option.

Here is what I tried in my build.gradle:

assemble.dependsOn lint 
+34
android android-studio gradle android-lint
Apr 03 '14 at 22:41
source share
7 answers

If you just want to configure your Android Studio project to run a lens test before configuring the default launch without affecting your gradle task settings, you can follow these steps.

  • Open the run configuration and select edit

enter image description here

  1. Select your application configuration.

enter image description here

  1. Click "+" to add a new step.

enter image description here

  1. Select "Gradle -aware Make"

enter image description here

  1. Enter "check" and select the option with the name of your application module and check. (Mine :app:check )

enter image description here

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

enter image description here

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

+32
Nov 21 '14 at 19:16
source share

To start and analyze a project, simply select Analyze > Inspect Code .

You should get a good window with all the problems.

enter image description here

Also check Launch lint in Android Studio for more information.




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

 lintOptions { abortOnError true } 

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

+29
Apr 04 '14 at 4:05
source share

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 your build tasks dependent on the lint task, effectively executing them before every build call that Android Studio runs.

edit

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

 applicationVariants.all { val lintTask = tasks["lint${name.capitalize()}"] assembleProvider.get().dependsOn.add(lintTask) } 
+12
Jan 15 '16 at 15:21
source share

just run the check task

./gradlew clean check assembleRelease

+7
Dec 08 '14 at 2:51
source share

If you want the Android Studio project to run a lint check before configuring the default launch, without affecting how your gradation tasks are configured. And you want to do this in the gradient building system, you can add the following block outside the android block in the app module 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 option that you have already defined, for example. compileReleaseSources , compileDebugSources , compileStagingDebugSources , etc.

This has been tested on Android Studio 3.0.

+4
Jan 29 '18 at 4:33
source share

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

 android { .. afterEvaluate { applicationVariants.all { variant -> // variantName: eg Debug, Release def variantName = variant.name.capitalize() // now we tell gradle to always start lint after compile // eg start lintDebug after compileDebugSources project.tasks["compile${variantName}Sources"].doLast { project.tasks["lint${variantName}"].execute() } } } } 
+3
Jan 30 '17 at 9:53 on
source share

Just changing the answer @Yoel Gluschnaider

For me, if I use val, it throws an error like this: Failed to set unknown property 'lintTask' for an object like com.android.build.gradle.internal.api.ApplicationVariantImpl.

So I replace

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

and his work is excellent !!

0
Jun 12 '19 at 8:04 on
source share



All Articles