在构建 android studio 项目时运行 lint

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22850333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:30:07  来源:igfitidea点击:

Run lint when building android studio projects

androidandroid-studiogradleandroid-lint

提问by Robert

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.

我希望能够在使用 android studio 构建项目时运行 lint 任务,以确保遵循 lint 规则。

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 generateDebugSourcesand compileDebugJavatasks interchangeably when I have selected the debug build variant.

我曾尝试使用任务依赖项,但没有运气。我的 TeamCity 构建服务器使用运行 lint 任务的构建任务,因此效果很好。但是,当我选择了调试构建变体时,android studio 似乎可以互换使用generateDebugSourcescompileDebugJava任务。

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

这是我在 build.gradle 中尝试过的:

assemble.dependsOn lint

采纳答案by Ross Hambrick

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.

如果您只是想将您的 Android Studio 项目配置为在默认运行配置之前运行 lint 检查而不影响您的 gradle 任务的配置方式,您可以按照以下步骤操作。

  1. Open the run configurations drop down and choose edit
  1. 打开运行配置下拉菜单并选择编辑

enter image description here

在此处输入图片说明

  1. Select your app run configuration
  1. 选择您的应用程序运行配置

enter image description here

在此处输入图片说明

  1. Press the '+' to add a new step
  1. 按“+”添加新步骤

enter image description here

在此处输入图片说明

  1. Choose "Gradle-aware Make"
  1. 选择“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)
  1. 输入“check”并选择带有您的应用程序模块名称的选项并检查。(我的是:app:check

enter image description here

在此处输入图片说明

  1. Press the up arrow to move the new checkstep before the existing Gradle-aware makestep
  1. 按向上箭头将新check步骤移动到现有Gradle-aware make步骤之前

enter image description here

在此处输入图片说明

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

现在,Android Studio 将运行 lint 检查并在发生任何 lint 错误时使构建失败。

回答by AndyGable

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

要运行 lint 并分析您的项目,只需选择Analyze > Inspect Code.

You should get a nice window with all issues.

你应该得到一个很好的窗口来解决所有问题。

enter image description here

在此处输入图片说明

Also check Run lint in Android Studiofor more information.

另请查看在 Android Studio 中运行 lint 以获取更多信息。



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

我做了更多的研究,尝试将其添加到您的build.gradle.

lintOptions {
      abortOnError true
  } 

There are many optionsthat you can apply to the build.gradle

有许多选项可以应用于build.gradle

回答by Yoel Gluschnaider

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

要在 build.gradle 中执行此操作,请将以下几行添加到您的 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.

这使得您的所有 assemble 任务都依赖于在 Android Studio 执行的每个 assemble 调用之前有效运行它们的 lint 任务。

Edit

编辑

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

对于 Android Gradle Plugin 3.3 和 Gradle 5.x,这是使用 Kotlin 脚本的修订版:

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

回答by stephane k.

just run the "check" task

只需运行“检查”任务

./gradlew clean check assembleRelease

回答by TmTron

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

这是我的解决方案,当您在 Android Studio 中单击Build- Make Project时也可以使用该解决方案:

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()
                }
        }
    }
}

回答by Phileo99

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 androidblock in the app module's build.gradle as follows:

如果您想强制 Android Studio 项目在默认运行配置之前运行 lint 检查而不影响您的 gradle 任务的配置方式,并且您想在 gradle 构建系统中执行此操作,那么您可以在块之外添加以下android块在 app 模块的 build.gradle 中,如下所示:

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

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

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

替换compileDevDebugSources为您已经定义的所需构建变体,例如。compileReleaseSourcescompileDebugSourcescompileStagingDebugSources,等。

This was tested working on Android Studio 3.0

这是在 Android Studio 3.0 上测试过的

回答by Rushabh Shah

Just modifying answer of @Yoel Gluschnaider

只是修改@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.

对我来说,如果我使用 Val 它会显示这样的错误:无法为类型的对象设置未知属性“lintTask” 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!!

它工作正常!!