英文原文:
http://tools.android.com/tips/lint/suppressing-lint-warnings
红色圈中的按钮的
的意义分别如下
一、前言
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 arecombined 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 inLinearLayouts"NestedWeights" : Looks for nested layout weights , which are costly
你可以通过
lint
的
--show
选项后跟检查项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所示.
图3-1
![Android Lint 检查规则的定制 - hubingforever - 民主与科学](http://img6.ph.126.net/yHGXkoXU3bRBNMvoeJjCvw==/6597697587796053726.jpg)
-
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简介 - hubingforever - 民主与科学](http://img7.ph.126.net/5VgW4FichbrcjMlxBKEyRQ==/6597883405261307307.jpg)
图3-3
![Android Lint 检查规则的定制(基本篇) - hubingforever - 民主与科学](http://img5.ph.126.net/kJFMfjaQ9zZBzX0f8zdVjA==/6597643711726446085.jpg)
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
![Android Lint 检查规则的定制(基本篇) - hubingforever - 民主与科学](http://img0.ph.126.net/-_biAu6G-q10V3yCFIpOHA==/6597837225772943420.jpg)
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 检查规则的定制(基本篇) - hubingforever - 民主与科学](http://img1.ph.126.net/_DoFGOMF9AxQ0E2iB95-ig==/6597791046284573661.jpg)
图3-6
![Android Lint 检查规则的定制(基本篇) - hubingforever - 民主与科学](http://img5.ph.126.net/lmQ9a1W3XKGomSChA13NqQ==/6597862514540390205.jpg)
在图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 检查规则的定制(基本篇) - hubingforever - 民主与科学](http://img8.ph.126.net/N3n6oHaPdknqbcqWYiLyNw==/6597573342982274818.jpg)
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-8
所示。
把检查级别(Severity)设为”ignore“,其实就是忽略(
suppress
)该检查规则
图3-8
![Android Lint 检查规则列表 - hubingforever - 民主与科学](http://img3.ph.126.net/ixdhWYM4vHcDGSW396NbPg==/606297099852548503.jpg)
当然我们也可以选中一个工程,其属性(Properties)界面的”Android lint Prerences“设定特定Android工程
lint
检查规则的检查级别,如
图3-8
所示。
图3-9
![Android Lint 检查规则的定制(基本篇) - hubingforever - 民主与科学](http://img7.ph.126.net/EX2UCm459lkbIBK6NQKyIw==/6597960371075262887.jpg)