android lint_什么是Android Lint?

android lint

A lot of us probably do know what actually Android Lint is, even if we don’t know the name. Because it is the thing that shows warning messages in the IDE. It may be warnings or errors that will actually fail the build. The following images shows an example of Lint suggestions.

即使我们不知道Android Lint的名称,我们中的很多人也可能确实知道。 因为它是在IDE中显示警告消息的东西。 可能是警告或错误实际上会使构建失败。 下图显示了皮棉建议的示例。

Image for post
Gradle version out of date
Gradle版本已过时
Image for post
Not calling super method where expected
没有在预期的地方调用超级方法

Android Studio provides a code scanning tool called lint that can help you to identify and correct problems with the structural quality of your code. For example, Lint can help us clean up these issues

Android Studio提供了一个名为lint的代码扫描工具,可以帮助您识别和纠正代码结构质量方面的问题。 例如,皮棉可以帮助我们清理这些问题

  • XML resource files containing unused namespaces

    包含未使用的名称空间的XML资源文件
  • Use of deprecated elements

    使用过时的元素
  • API calls that are not supported by the target API versions, might lead to code failing to run correctly

    目标API版本不支持的API调用可能会导致代码无法正确运行
  • Lot more..

    更多..

The problems detected in our code by the Lint will be reported to us with some suggestions and a warning level. We can use the suggestion to correct our code. Lint can customized to see a particular type of error in our project. When using Android Studio, inspection process run whenever we build our project. However, we can manually run inspections or run lint from the command line.

Lint在我们的代码中检测到的问题将向我们报告,并提供一些建议和警告级别。 我们可以使用该建议来更正我们的代码。 Lint可以自定义以查看我们项目中的特定类型的错误。 使用Android Studio时,只要我们构建项目,就会运行检查过程。 但是,我们可以从命令行手动运行检查或运行lint。

The basic topics that is covered are:

涵盖的基本主题是:

  • How to use Lint

    如何使用皮棉
  • Working of Lint

    皮棉的工作
  • How to Configure lint

    如何配置皮棉
  • Using Baseline

    使用基准

如何使用皮棉 (How to use Lint)

By Android Studio:

通过Android Studio:

In order to manually inspect a file in Android Studio, click on Analyze > Inspect Code. Below window will be opened:

为了在Android Studio中手动检查文件,请单击“ 分析”>“检查代码”。 下面的窗口将打开:

Image for post

Here we can select your inspection scope i.e. whole project or module or simply a file. After selecting the scope click on OK.

在这里,我们可以选择您的检查范围,即整个项目或模块或仅一个文件。 选择范围后,单击确定。

Image for post

Under Inspection Results, in the left pane tree view, error categories, types, and issues is displayed.

在“检查结果”下,在左窗格树视图中,显示错误类别,类型和问题。

The right pane displays the inspection report for the selected error category, type, or issue and provides the name and location of the error.

右窗格显示所选错误类别,类型或问题的检查报告,并提供错误的名称和位置。

Run lint from the command line:

从命令行运行lint:

Invoke the lint task for your project by entering one of the following commands from the root directory of your project:

通过从项目的根目录输入以下命令之一,为项目调用lint任务:

On Windows:

在Windows上:

gradlew lint

On Linux or Mac:

在Linux或Mac上:

./gradlew lint

When the lint tool completes its checks, it provides paths to the XML and HTML versions of the lint report.

棉绒工具完成检查后,将提供棉绒报告的XML和HTML版本的路径。

皮棉的工作 (Working of Lint)

  1. The lint.xml file: We can customize the Lint checks in the lint.xml file. In this file, we can define severity for the issue such as error, ignore, warning. Apart from this, we can customize the Lint checks manually also. We will see how to do manual Lint checks in the later part of this blog.

    lint.xml文件:我们可以在lint.xml文件中自定义Lint检查。 在此文件中,我们可以为问题定义严重性,例如错误,忽略,警告。 除此之外,我们还可以手动自定义Lint检查。 我们将在此博客的后面部分中介绍如何进行Lint手动检查。

  2. Application source file: It is the source files on which we want to perform the Lint check. It can be .java file or .kt file or any xml file of the project.

    应用程序源文件:这是我们要在其上执行Lint检查的源文件。 它可以是项目的.java文件或.kt文件或任何xml文件。

  3. The Lint tool: Finally, the lint tool takes the source and lint.xml files and checks for structural code problems and suggest some improvements in the code if any. The tool that can be run on our Android project either from the command line or manually in Android Studio

    Lint工具:最后,lint工具将获取源文件和lint.xml,并检查结构性代码问题,并建议对代码进行一些改进(如果有)。 该工具可以从命令行在Android项目上运行,也可以在Android Studio中手动运行

The following image shows the working of lint tool:

下图显示了棉绒工具的工作原理:

Code scanning workflow with the lint tool
Code scanning workflow with the lint tool
使用棉绒工具进行代码扫描工作流程

如何配置皮棉: (How to Configure lint:)

When you run a lint scan, the tool checks for all issues that lint supports by default. It takes lots of processing time. We can also restrict the issues for lint to check and assign the severity level for those issues. For example, we can suppress lint checking for specific issues that are not relevant to our project. Lint checking can be performed at different levels such as Globally(entire project), Project module, Test module etc.

运行lint扫描时,该工具默认情况下会检查lint支持的所有问题。 这需要大量的处理时间。 我们还可以限制问题的数量,以便皮棉检查和分配这些问题的严重性级别。 例如,我们可以禁止检查与项目无关的特定问题。 可以在不同级别(例如,全局(整个项目),项目模块,测试模块等)执行棉绒检查。

Configuring lint in Android Studio:

在Android Studio中配置皮棉:

We can select and deselect the issues to be checked by Lint on Inspection Results window by clicking Analyze > Inspect Code

我们可以通过单击分析>检查代码来选择和取消选择Lint在“ 检查结果”窗口中要检查的问题。

  1. Select Analyze > Inspect Code.

    选择“ 分析”>“检查代码”。

  2. In the Specify Scope dialog under Inspection Profile, click More.

    在“ 检验概要”下的“ 指定范围”对话框中,单击“ 更多”

The Inspections dialog appears with a list of the supported inspections and their descriptions.

随即出现“ 检查”对话框,其中列出了受支持的检查及其说明。

Image for post
  • Select the Profile drop-down list to toggle between Default (Android Studio) and Project Default (the active project) inspections

    选择配置文件下拉列表以在默认 (Android Studio)和项目默认 (活动项目)检查之间切换

  • In the Inspections dialog in the left pane, we can edit the inspections

    在左窗格的“ 检查”对话框中,我们可以编辑检查

  • Click OK when done.

    完成后单击“ 确定”

Configuring the lint file:

配置lint文件:

We can specify manual checks in our app by adding the list of issues to be configured in the lint.xml file. While creating a new lint.xml file, it should be placed into the root directory of our Android project.

我们可以 通过添加要在lint.xml文件中配置的问题列表,在我们的应用程序中指定手动检查。 在创建新的lint.xml文件时,应将其放置在我们的Android项目的根目录中。

Click here to view the list of available lint checks and corresponding issue id’s.

单击此处以查看可用的棉绒检查列表以及相应的问题ID。

<lint>
    <issue id="MissingTranslation" severity="error"/>


    <issue id="HardcodedText" severity="error" />


    <issue id="Typos" severity="ignore" />


    <issue id="IconMissingDensityFolder" severity="ignore" />


    <issue id="RtlSymmetry" severity="error"/>
</lint>

Configuring lint checking in Java or Kotlin:

在Java或Kotlin中配置棉绒检查:

To disable lint checking specifically for a class or method in our Android project, add the @SuppressLint annotation to that code.

要为我们的Android项目中的类或方法专门禁用lint检查,请在该代码中添加@SuppressLint批注。

The following example shows how we can turn off lint checking for the NewApi issue in the onCreate method. However the lint tool continues to check for the NewApi issue in other methods of this class.

以下示例显示了如何在onCreate方法中关闭对NewApi问题的lint检查。 但是,lint工具将继续检查NewApi的其他方法中的NewApi问题。

@SuppressLint("NewApi")
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main)

使用基准: (Using Baseline:)

If we are working on a large project and interested in finding the future errors that might arise while adding some more codes to our project then we can add a baseline to the project and by doing so the lint will generate the errors that have occurred after that baseline. So, lint will ignore the previous code issues and will warn us only about the new lines of code added after the baseline.

如果我们正在做一个大型项目,并且有兴趣在向项目中添加更多代码的同时发现将来可能发生的错误,那么我们可以为项目添加基线,这样,皮棉将生成在此之后发生的错误。基线。 因此,lint将忽略先前的代码问题,只会警告我们有关基线之后添加的新代码行的信息。

To create a baseline snapshot, modify your project’s build.gradle file as follows

要创建基准快照,请按如下所示修改项目的build.gradle文件

android {
lintOptions {
baseline file("lint-baseline.xml")
}
}

结论 (Conclusion)

Android Lint is a powerful tool where we can also write our own custom Lint checks. Lint tool can be utilized effectively to build an app with greater optimization.

Android Lint是一个功能强大的工具,我们还可以编写自己的自定义Lint检查。 Lint工具可以有效地用于构建优化程度更高的应用程序。

翻译自: https://medium.com/swlh/what-is-android-lint-17fa0d87abb2

android lint

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值