Android中Lint的原理是什么

发布时间:2021-04-13 15:35:13 作者:Leah
来源:亿速云 阅读:294

这篇文章将为大家详细讲解有关Android中Lint的原理是什么,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

Lint 的工作过程

lint 工具的代码扫描工作流:

Android中Lint的原理是什么

Lint 的工作过程由 Lint Tool(检测工具),Source Files(项目源文件) 和 lint.xml(配置文件) 三个部分组成,Lint Tool 读取 Source Files,根据 lint.xml 配置的规则(issue)输出最终的结果。

Lint 的功能

Lint 可以检查并发现以下几类问题:

问题等级

Lint 发现的每个问题都有描述信息和等级,我们可以很方便地定位问题,同时按照严重程度进行解决。当然,我们也可以手动配置每个问题的严重级别。Lint 本身包含大量已经封装好的接口,能提供丰富的代码信息,开发者可以基于这些信息进行自定义规则的编写。

Lint 会按照问题的严重程度分为几个等级:

问题严重程序由高到低依次降低。

从命令行运行 lint

如果你使用的是 Android Studio 或 Gradle,你可以在项目的根目录下输入以下某个命令,使用 Gradle 封装容器对项目调用 lint 任务:

在 Windows 上:

gradlew lint

在 Linux 或 Mac 上:

./gradlew lint

lint 工具完成其检查后,会提供 XML 和 HTML 版 lint 报告的路径。然后,我们可以转到 HTML 报告并在浏览器中将其打开

Android Studio 中使用 Lint

Lint 已经被集成到 Android Studio,所以可以直接使用,使用非常方便。lint 的代码扫描工具,可帮助你发现并更正代码结构质量的问题,而无需您实际执行应用,也不必编写测试用例。系统会报告该工具检测到的每个问题并提供问题的描述消息和严重级别,以便你可以快速确定需要优先进行的关键改进。此外,你还可以降低问题的严重级别以忽略与项目无关的问题,或者提高严重级别以突出特定问题。

从菜单栏,选择Analyze > Inspect Code

选择检查范围

Android中Lint的原理是什么

选择后,点击"OK",稍等一会就会生成扫描结果:

Android中Lint的原理是什么

左侧是问题分类,选中一个问题条目,则右侧会展示具体的问题代码,这样就可以很方便的进行问题排查、定位和更改了。

Android 的规则类别:

lint 配置

配置 lint 文件

我们可以在 lint.xml 文件中进行 lint 配置。我们可以手动创建该文件,并放置在 Android 项目的根目录下。

lint.xml 文件由封闭的 父标记组成,此标记包含一个或多个 子元素。lint 会为每个 定义唯一的 id 属性值。

<?xml version="1.0" encoding="UTF-8"?>
<lint>
        <!-- list of issues to configure -->
</lint>

我们可以通过在 标记中设置严重性级别属性来更改某个问题的严重性级别或对该问题停用 lint 检查。

下面来看一个示例:

<?xml version="1.0" encoding="UTF-8"?>
<lint>
    <!-- Disable the given check in this project -->
    <issue id="IconMissingDensityFolder" severity="ignore" />

    <!-- Ignore the ObsoleteLayoutParam issue in the specified files -->
    <issue id="ObsoleteLayoutParam">
        <ignore path="res/layout/activation.xml" />
        <ignore path="res/layout-xlarge/activation.xml" />
    </issue>

    <!-- Ignore the UselessLeaf issue in the specified file -->
    <issue id="UselessLeaf">
        <ignore path="res/layout/main.xml" />
    </issue>

    <!-- Change the severity of hardcoded strings to "error" -->
    <issue id="HardcodedText" severity="error" />
</lint>

禁用某个文件或方法进行 lint 检查

如果我们在 Android 项目中想对某个类或方法禁用 lint 检查,可以请向该代码添加 @SuppressLint 注解。

以下示例展示了如何对 onCreate 方法中的 NewApi 问题停用 lint 检查。lint 工具会继续检查该类的其他方法中的 NewApi 问题。

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

以下示例展示了如何对 FeedProvider 类中的 ParserError 问题停用 lint 检查:

@SuppressLint("ParserError")
public class FeedProvider extends ContentProvider {

要禁止 lint 检查文件中的所有问题,请使用 all 关键字,如下所示:

@SuppressLint("all")

xml 文件的 lint 检测配置

我们可以使用 tools:ignore 属性对 XML 文件的特定部分停用 lint 检查。在 lint.xml 文件中添加以下命名空间值,以便 lint 工具能够识别该属性:

namespace xmlns:tools="http://schemas.android.com/tools"

以下示例展示了如何对 XML 布局文件的 元素中的 UnusedResources 问题停用 lint 检查。如果某个父元素声明了 ignore 属性,则该元素的子元素会继承此属性。在本示例中,也会对 子元素停用 lint 检查。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="UnusedResources" >

    <TextView
        android:text="@string/auto_update_prompt" />
</LinearLayout>

要禁止检查多个问题,请使用以英文逗号分隔的字符串列出要禁止检查的问题。例如:

tools:ignore="NewApi,StringFormatInvalid"

要禁止 lint 检查 XML 元素中的所有问题,请使用 all 关键字,如下所示:

tools:ignore="all"

通过 Gradle 配置 lint 选项

通过 Android Plugin for Gradle,我们可以使用模块级 build.gradle 文件中的 lintOptions {} 代码块配置某些 lint 选项,例如要运行或忽略哪些检查。

例如:

android {
  ...
  lintOptions {
    // Turns off checks for the issue IDs you specify.
    disable 'TypographyFractions','TypographyQuotes'
    // Turns on checks for the issue IDs you specify. These checks are in
    // addition to the default lint checks.
    enable 'RtlHardcoded','RtlCompat', 'RtlEnabled'
    // To enable checks for only a subset of issue IDs and ignore all others,
    // list the issue IDs with the 'check' property instead. This property overrides
    // any issue IDs you enable or disable using the properties above.
    check 'NewApi', 'InlinedApi'
    // If set to true, turns off analysis progress reporting by lint.
    quiet true
    // if set to true (default), stops the build if errors are found.
    abortOnError false
    // if true, only report errors.
    ignoreWarnings true
  }
}
...

在 Android Studio 中修改 lint 配置文件

我们可以很方便的在 Android Studio 中修改 lint 检查时的配置。

Android Studio 附带了许多 lint 及其他检查配置文件,这些配置文件可通过 Android 更新进行更新。我们可以原封不动地使用这些配置文件,也可以修改它们的名称、说明、严重级别和范围。当然,还可以激活和禁用整组的配置文件或一组配置文件中的个别配置文件。

依次选择 Analyze > Inspect Code,在 Specify Scope 对话框的 Inspection Profile 下,点击 More。

此时将显示 Inspections 对话框,其中列出了支持的检查及其说明:

Android中Lint的原理是什么

  1. 选择 Profile 下拉列表,以在 Default (Android Studio) 与 Project Default(活动项目)检查之间切换。

  2. 在左侧窗格的 Inspections 对话框中,选择一个顶级配置文件类别,或展开一个组并选择特定的配置文件。选择一个配置文件类别后,我们可以将该类别中的所有检查项目当作一个检查项目进行修改。

  3. 选择 Manage 下拉列表,以复制检查项目、对检查项目进行重命名、向检查项目添加说明以及导出/导入检查项目。

  4. 操作完成后,点击 OK。

关于Android中Lint的原理是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. Android中Notification机制的原理是什么
  2. Android中 ANR在线监控的原理是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android lint

上一篇:怎么在c#中使用Linq查询语句

下一篇:怎么在Unity3d中利用Gizmos画一个圆

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》