您的位置:首页 > 移动开发 > Android开发

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

2015-10-26 16:07 519 查看
本人博客原文

英文原文: 

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文件的一个示例
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div><span class="pun" style="color: rgb(102, 102, 0);"><?</span><span class="pln">xml version</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(0, 136, 0);">"1.0"</span><span class="pln"> encoding</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(0, 136, 0);">"UTF-8"</span><span class="pun" style="color: rgb(102, 102, 0);">?></span></div><div><span class="tag" style="color: rgb(0, 0, 136);"><lint></span></div><div><span class="pln">    </span><span class="com" style="color: rgb(136, 0, 0);"><!-- Disable the given check in this project --></span></div><div><span class="pln">    </span><span class="tag" style="color: rgb(0, 0, 136);"><issue</span><span class="pln"> </span><span class="atn" style="color: rgb(102, 0, 102);">id</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"IconMissingDensityFolder"</span><span class="pln"> </span><span class="atn" style="color: rgb(102, 0, 102);">severity</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"ignore"</span><span class="pln"> </span><span class="tag" style="color: rgb(0, 0, 136);">/></span></div><div>
</div><div><span class="pln">    </span><span class="com" style="color: rgb(136, 0, 0);"><!-- Ignore the ObsoleteLayoutParam issue in the given files --></span></div><div><span class="pln">    </span><span class="tag" style="color: rgb(0, 0, 136);"><issue</span><span class="pln"> </span><span class="atn" style="color: rgb(102, 0, 102);">id</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"ObsoleteLayoutParam"</span><span class="tag" style="color: rgb(0, 0, 136);">></span></div><div><span class="pln">        </span><span class="tag" style="color: rgb(0, 0, 136);"><ignore</span><span class="pln"> </span><span class="atn" style="color: rgb(102, 0, 102);">path</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"res/layout/activation.xml"</span><span class="pln"> </span><span class="tag" style="color: rgb(0, 0, 136);">/></span></div><div><span class="pln">        </span><span class="tag" style="color: rgb(0, 0, 136);"><ignore</span><span class="pln"> </span><span class="atn" style="color: rgb(102, 0, 102);">path</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"res/layout-xlarge/activation.xml"</span><span class="pln"> </span><span class="tag" style="color: rgb(0, 0, 136);">/></span></div><div><span class="pln">    </span><span class="tag" style="color: rgb(0, 0, 136);"></issue></span></div><div>
</div><div><span class="pln">    </span><span class="com" style="color: rgb(136, 0, 0);"><!-- Ignore the UselessLeaf issue in the given file --></span></div><div><span class="pln">    </span><span class="tag" style="color: rgb(0, 0, 136);"><issue</span><span class="pln"> </span><span class="atn" style="color: rgb(102, 0, 102);">id</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"UselessLeaf"</span><span class="tag" style="color: rgb(0, 0, 136);">></span></div><div><span class="pln">        </span><span class="tag" style="color: rgb(0, 0, 136);"><ignore</span><span class="pln"> </span><span class="atn" style="color: rgb(102, 0, 102);">path</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"res/layout/main.xml"</span><span class="pln"> </span><span class="tag" style="color: rgb(0, 0, 136);">/></span></div><div><span class="pln">    </span><span class="tag" style="color: rgb(0, 0, 136);"></issue></span></div><div>
</div><div><span class="pln">    </span><span class="com" style="color: rgb(136, 0, 0);"><!-- Change the severity of hardcoded strings to "error" --></span></div><div><span class="pln">    </span><span class="tag" style="color: rgb(0, 0, 136);"><issue</span><span class="pln"> </span><span class="atn" style="color: rgb(102, 0, 102);">id</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"HardcodedText"</span><span class="pln"> </span><span class="atn" style="color: rgb(102, 0, 102);">severity</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"error"</span><span class="pln"> </span><span class="tag" style="color: rgb(0, 0, 136);">/></span></div><div><span class="tag" style="color: rgb(0, 0, 136);"></lint></span></div>


在这里检查规则(issue)的标示符(比如IconMissingDensityFolder, ObsoleteLayoutParam etc)就是检查规则的ID(issue ids)
, 它实际上也包含在lint命令的扫描结果中,比如:

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span style="line-height: 13px; color: rgb(0, 96, 0); font-size: 13px; background-color: rgb(239, 239, 239);"><span class="typ" style="line-height: 23px; color: rgb(102, 0, 102);">Warning</span><span class="pun" style="line-height: 23px; color: rgb(102, 102, 0);">:</span><span class="pln" style="line-height: 23px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 23px; color: rgb(102, 0, 102);">The</span><span class="pln" style="line-height: 23px; color: rgb(0, 0, 0);"> resource R</span><span class="pun" style="line-height: 23px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 23px; color: rgb(0, 0, 0);">drawable</span><span class="pun" style="line-height: 23px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 23px; color: rgb(0, 0, 0);">robot appears to be unused </span><span class="pun" style="line-height: 23px; color: rgb(102, 102, 0);">[</span></span><span style="line-height: 13px; color: rgb(0, 96, 0); font-size: 13px; background-color: rgb(239, 239, 239);"><span class="typ" style="line-height: 23px; color: rgb(102, 0, 102);">UnusedResources</span></span><span style="line-height: 13px; color: rgb(0, 96, 0); font-size: 13px; background-color: rgb(239, 239, 239);"><span class="pun" style="line-height: 23px; color: rgb(102, 102, 0);">]</span></span></p>

可以通过lint的--list选项来得到检查项类别和检查项id.
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div><span class="pln">$ lint </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln">list</span></div><div><span class="pun" style="color: rgb(102, 102, 0);">...</span></div><div><span class="str" style="color: rgb(0, 136, 0);">"ContentDescription"</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Ensures</span><span class="pln"> that image widgets provide a contentDescription</span></div><div><span class="str" style="color: rgb(0, 136, 0);">"DuplicateIncludedIds"</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Checks</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">for</span><span class="pln"> duplicate ids across layouts that are</span></div><div><span class="pln">      combined </span><span class="kwd" style="color: rgb(0, 0, 136);">with</span><span class="pln"> include tags</span></div><div><span class="str" style="color: rgb(0, 136, 0);">"DuplicateIds"</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Checks</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">for</span><span class="pln"> duplicate ids within a single layout</span></div><div><span class="str" style="color: rgb(0, 136, 0);">"StateListReachable"</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Looks</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">for</span><span class="pln"> unreachable states </span><span class="kwd" style="color: rgb(0, 0, 136);">in</span><span class="pln"> a </span><span class="str" style="color: rgb(0, 136, 0);"><selector></span></div><div><span class="str" style="color: rgb(0, 136, 0);">"InefficientWeight"</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Looks</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">for</span><span class="pln"> inefficient weight declarations </span><span class="kwd" style="color: rgb(0, 0, 136);">in</span></div><div><span class="pln">      </span><span class="typ" style="color: rgb(102, 0, 102);">LinearLayouts</span></div><div><span class="str" style="color: rgb(0, 136, 0);">"NestedWeights"</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Looks</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">for</span><span class="pln"> nested layout weights</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> which are costly</span></div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>

你可以通过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配置文件。

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div style="line-height: 13px; font-family: Arial, Verdana, sans-serif;"><code style="line-height: 23px; color: rgb(0, 96, 0);"><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln" style="color: rgb(0, 0, 0);">config </span><span class="str" style="color: rgb(0, 136, 0);"><filename></span><span class="pln" style="color: rgb(0, 0, 0);">      </span><span class="typ" style="color: rgb(102, 0, 102);">Use</span><span class="pln" style="color: rgb(0, 0, 0);"> the given configuration file to determine whether</span></code></div><div style="line-height: 13px; font-family: Arial, Verdana, sans-serif;"><code style="line-height: 23px; color: rgb(0, 96, 0);"><span class="pln" style="color: rgb(0, 0, 0);">                         issues are enabled </span><span class="kwd" style="color: rgb(0, 0, 136);">or</span><span class="pln" style="color: rgb(0, 0, 0);"> disabled</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">If</span><span class="pln" style="color: rgb(0, 0, 0);"> a project contains</span></code></div><div style="line-height: 13px; font-family: Arial, Verdana, sans-serif;"><code style="line-height: 23px; color: rgb(0, 96, 0);"><span class="pln" style="color: rgb(0, 0, 0);">                         a lint</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xml file</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">then</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pln" style="color: rgb(0, 0, 0);"> config file will be used</span></code></div><div><span style="line-height: 13px; color: rgb(0, 96, 0); font-family: Arial, Verdana, sans-serif;"><span class="pln" style="line-height: 23px; color: rgb(0, 0, 0);">                         </span><span class="kwd" style="line-height: 23px; color: rgb(0, 0, 136);">as</span><span class="pln" style="line-height: 23px; color: rgb(0, 0, 0);"> a fallback</span><span class="pun" style="line-height: 23px; color: rgb(102, 102, 0);">.</span></span><span class="pun" style="color: rgb(102, 102, 0);">。。</span></div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>

三、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



图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



 

图3-6



 

 在图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



 
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



当然我们也可以选中一个工程,其属性(Properties)界面的”Android lint Prerences“设定特定Android工程lint检查规则的检查级别,如图3-8所示。
图3-8



 
结束!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: