Android Lint 检查规则的定制(基本篇)

本人博客原文

英文原文: 

http://tools.android.com/tips/lint/suppressing-lint-warnings 
http://tools.android.com/recent/ignoringlintwarnings
一、前言
you can also ignore warnings using annotations (in Java files) and using special attributes (in XML files). 
你可以通过在java源码文件中使用注释( annotations  )或在XML文件使用属性(attributes)的方式忽略Lint的一些检查规则,以达到 Lint检查规则的基本定制
While Lint can often find real errors that must be fixed, it also flags issues that may or may not be a problem depending on the context. If you've manually verified that an issue is not a problem, you may want to mark the issue as verified such that lint does not keep pointing it out.
Lint经常会扫描出一些必需要修复的错误(issue).但是这些issue一旦结合实际的上下文,或许实际上不是真正的problem.
如果你结合上下文进行分析,确定该issue不是一个problem的话,它可以对此进行标记,以便lint不再继续 keep pointing it out..
二、命令行中
The lint command has three commands for controlling which checks are performed:  --enable--disable and  --check (see the  overview document for more details on these flags). However, these must be specified each time you run lint.
在命令行中,可以通过lint命令的三个选项( -enable --disable  和  --check )来进行基本的检查规则定制。然而它们只对当次lint命令有效,即下次在运行lint时又需要重新指定。关此的详细内容请阅读《 Android Lint简介 》中的第二章“ 命令行中使用Lint
To persistently configure which rules are run, you can create a file named  lint.xml  in the root directory of your project (next to the manifest file). Lint will automatically look at this file and use it to ignore warnings. Note that when you use Eclipse to suppress errors, it automatically creates this file for you, so you can use Eclipse to ignore warnings and then check in the resulting configuration file such that for example build server lint runs will ignore warnings you've manually verified.
为了让你对检查规则的定制进行能够持久化,你可以在你的Android工程的根目录(即和manfiest在同一目录)下创建一个名叫 lint.xml的文件。
当你在Eclipse中使用UI操作来对erro进行 suppress 时,Eclipse实际也只是自动的在其Android目录下为你生成 lint.xml 文件。因此你可以通过Exlipse来生成一个 lint.xml 文件的样本,再通过命令行来使用过该 lint.xml 文件。
Here's a sample  lint.xml file (the comments are obviously not needed; I've added them here to explain what each line does)
以下是lint.xml文件的一个示例

<? 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 given files -->
    <issue id = "ObsoleteLayoutParam" >
        <ignore path = "res/layout/activation.xml" />
        <ignore path = "res/layout-xlarge/activation.xml" />
    </issue>
    <!-- Ignore the UselessLeaf issue in the given 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>
在这里检查规则(issue)的标示符(比如IconMissingDensityFolder, ObsoleteLayoutParam etc)就是检查规则的ID(issue ids)
, 它实际上也包含在lint命令的扫描结果中,比如:

Warning: The resource R.drawable.robot appears to be unused [UnusedResources]

可以通过 lint--list选项来得到检查项类别和检查项id.

$ lint -- list
...
"ContentDescription" : Ensures that image widgets provide a contentDescription
"DuplicateIncludedIds" : Checks for duplicate ids across layouts that are
      combined with include tags
"DuplicateIds" : Checks for duplicate ids within a single layout
"StateListReachable" : Looks for unreachable states in a <selector>
"InefficientWeight" : Looks for inefficient weight declarations in
      LinearLayouts
"NestedWeights" : Looks for nested layout weights , which are costly

你可以通过 lint --show 选项后跟检查项id来得到一个检查项的详细说明.
另外你也可以通过 Android Lint 检查规则列表 》来查阅检查项的id等详细信息
This lint configuration file apply to the current project, as well as any library projects included into this project. If you want to also have a "global" configuration used for all projects, you can create an additional global file and then invoke lint with the --config flag:
lint.xml 文件对当前Android工程和他包含进来的library工程有效。当然你也可以使用Lint命令的 --config 选项来设置一个全局的 lint.xml 配置文件。

--config <filename>      Use the given configuration file to determine whether
                         issues are enabled or disabled. If a project contains
                         a lint.xml file, then this config file will be used
                         as a fallback. 。。

三、Eclipse中
当你在Eclipse中对erro进行suppress(即对检查规则进行基本定制),Eclipse实际也只是自动的在其Android目录下为你生成 lint.xml 文件。因此你可以通过Exlipse来生成一个 lint.xml 文件的样本,再通过命令行来使用过该 lint.xml 文件。

在Eclipse中你可以通过 lint窗口中进行检查规则的基本定制,如 图3-1所示.
关于 lint窗口的更多内容请参考 Android Lint简介 》中的第三章“ Eclispe中使用Lint”的第二节
图3-1
 红色圈中的按钮的的意义分别如下
  •  Suppress this issue with an attribute or annotation
  •  Ignore in this file (saves suppress information in lint.xml)
  •  Ignore in this project (ditto)
  •  Always ignore
在Eclipse的编辑窗口中( 如图3-2和3-3所示 ), 你可以选择如下的方式 对erro进行 suppress (即对检查规则进行基本定制)
  • Disable Check in this file only
  • Disable Check  in this project
  • IDisable Check .
  • Ignore warnings using annotations or attributes, as explained here.使用注释(annotations )或在XML文件使用属性(attributes)的方式忽略Lint的一些检查规则
图3-2
Android Lint 检查规则的定制(基本篇)_第1张图片
图3-3
 
As you can see, this offers to add the @SuppressLint annotation in three different places: the surrounding method and class contexts. The annotation will suppress any lint warnings of the given type for the scope that the annotation is annotating. (For this specific issue, "NewApi", there is an additional available annotation,  @TargetApi,  so the quickfix offers it. See the  Lint API Check  blog entry for details.)
正如你在 图3-3中看到的,  @SuppressLint注释可以添加三个地方:方法前(startAnimation)和类前(AnimationCloning和MyAimationView).当然在图3-3中,我们还可以通过 @TargetApi,注释来快速处理该issue.
The annotation takes a String parameter which lists the id of the issue to suppress. While the quickfix automatically supplies it, if you are editing manually you can also find the id listed at the end of each lint error message in the command line output as well as in the HTML reports. 

To suppress more than one issue, supply a String array like this:
@SuppressLint({"NewApi","StringFormatInvalid"})
and you can also use  @SuppressLint("all") to suppress everything. (The new annotations is provided in the new annotations.jar file which ships with Tools 17).
图3-2选择“ Add @suppressLint “”ResourceAsColor” to getView()后,
在函数getView()函数前会加上一行注释“ @SuppressLint("ResourceAsColor")“,如图3-4所示
图3-4
 
In XML files, you can use the new tools:ignore attribute in a similar way to suppress errors (see the third quickfix in the list which offers the suppress attribut
在XML文件中你可以使用 tools:ignore在做JAVA源码中 @SuppressLint注释同样的工作。
在XML把光标移动到有lint 所标记的地方,点击鼠标右键出现如 图3-5所示的下拉菜单,在下拉菜单中选中”Quick Fix“项出现如 图3-6所示的画面,
图3-5
Android Lint 检查规则的定制(基本篇)_第2张图片
 
图3-6
Android Lint 检查规则的定制(基本篇)_第3张图片
 
 在图3-6中选中" add ingore  '"项的话,XML文件将做如下修改
  • If necessary, it adds the namespace xmlns:tools="http://schemas.android.com/tools"在xml文件开始上声明命令空间tools
  • If adds the attribute tools:ignore="HardcodedText" on the given element. 为其对应的element加上tools:ignore="HardcodedText"
以上修改将如 图3-7所示
图3-7
Android Lint 检查规则的定制(基本篇)_第4张图片
 
Just as with Java annotations, this attribute is inherited, so you can specify it on the root of an XML document to suppress all warnings of the given type within the document. And just like the annotation, you can supply a comma separated list of issues (or "all") to suppress the given list of issues.
@SuppressLint注释一样, tools:ignore 属性也还是继承,即对其xml节点的所有子节点都有效果。另外,在 tools:ignore 中可以指定多个lint检查的issue id,他们之间用逗号(",") 隔开,"all"表示所有的 lint检查的issue,关于lint检查的issue id 请参考《 Android Lint 检查规则列表
Note that the new tools namespace is special. AAPT in Tools 17 will deliberately skip these attributes, so they do not end up in the compiled XML shipped with your application, so there is no cost at runtime.
另外注意命名空间 tools是is special.的。AAPT在编译xml文件的时候最终会忽略掉它们。
图3-6中,我们注意到在xml文件和在java源码文件中一样,你也可以做以下选择
  • Disable Check in this file only
  • Disable Check  in this project
  • IDisable Check .
另外,在Eclipse中可以在菜单Window->Preference->“ Lint Eerro checking ”中设置所有项目默认的 lint 检查规则的检查级别,如 图3-7 所示。
把检查级别(Severity)设为”ignore“,其实就是忽略( suppress )该检查规则
图3-8
Android Lint 检查规则的定制(基本篇)_第5张图片
当然我们也可以选中一个工程,其属性(Properties)界面的”Android lint Prerences“设定特定Android工程 lint 检查规则的检查级别,如 图3-8 所示
图3-8
Android Lint 检查规则的定制(基本篇)_第6张图片
 
结束!

你可能感兴趣的:(Android Lint 检查规则的定制(基本篇))