android[工具or命令] 代码优化-Android lint工具
2014-06-23 14:59
357 查看
作为移动应用开发者,我们总希望发布的apk文件越小越好,不希望资源文件没有用到的图片资源也被打包进apk,不希望应用中使用了高于minSdk的api,也不希望AndroidManifest文件存在异常,lint就能解决我们的这些问题。Android lint是在ADT
16提供的新工具,它是一个代码扫描工具,能够帮助我们识别代码结构存在的问题,主要包括:
1)布局性能(以前是 layoutopt工具,可以解决无用布局、嵌套太多、布局太多)
2)未使用到资源
3)不一致的数组大小
4)国际化问题(硬编码)
5)图标的问题(重复的图标,错误的大小)
6)可用性问题(如不指定的文本字段的输入型)
7)manifest文件的错误
Android lint可以解决如上的问题,当然还有更多,具体的可以参考Android
Lint Checks。Android官方也总结了lint能解决的问题,如下图。
lint是命令工具,它已经完美的集成到了Eclipse中,我们可以方便的使用。通过lint,我们可以检测出每个问题的说明和问题的严重性,根据检测报告可以对程序作出改进。下面介绍下在Eclipse怎么使用lint。
lint的使用可以通过两个途径,Eclipse左上角的打钩的按钮或者选择项目->右键->Android Tools,如下图所示:
图一 图二
lint工具简单实用,自动化分析,分析完成会给我们分析报告:
分析包括中会包括错误和警告,会给出具体的描述、类别、位置。上图是一个错误的描述,下图给出警告描述。
Android lint是对android开发者很有帮助的一款工具,对于项目打包发布前优化代码、查找没用到的资源、查找错误等非常有帮助。作为开发者是必须掌握的工具之一,如果想了解更多可以参考lint。
二、命令行中使用Lint
2.1、基本使用
4000
There is a command line tool in the SDK tools/ directory called
If you have the SDK
on your path, you can invoke it as “
project directory. You can also point to a random directory, which (if it is not an Android project) will be searched recursively and all projects under that directory will be checked. (And you can also specify multiple projects separated by spaces)
在Android SDK的tools下有个叫lint.bat的文件,它就是lint的命令行工具。
lint命令后可以带一个或多个参数,参数之间用空格隔开,参数表示的是需要使用lint进行扫描的Android项目的目录。
示例1
linux命令行
示例2
Window命令行
2.2、Disabling Checks(--disable)
在执行lint命令时可以通过--disable选项要指定关闭的检查规则项。--disable选项后接要关闭的检查规则项的id(比如示例3中的MissingTranslation)或检查规则项的类别(比如示例3中的Usability:Icons和示例4的Internationalization)。
关于lint检查项的id和类别(Category)等信息请参考《Android
Lint 检查规则列表》
示例3
示例4
2.3、enabling Checks(--enable和--check)
lint的有些检查项默认是关闭的(disable),在执行lint命令时可以通过--enable选项开启它。-enable选项后接要开启的检查规则项的id(比如示例5中的MissingTranslation )或检查规则项的类别(示例5中的Usability:Icons)
示例5
在执行lint命令时可以通过--check选项来指定只进行某些检查。-check选项后接要开启的检查规则项的id(比如示例6中的MissingPrefix)
示例6
$ lint --check MissingPrefix /src/astrid/
2.4、检查项类别和检查项id
可以通过lint的--list选项来得到检查项类别和检查项id.
比如:
可以通过lint的--show选项后跟检查项id来得到一个检查项的详细说明.
比如:
当然你也可以通过《Android
Lint 检查规则列表》来查阅检查项的id等详细信息
2.4、html形式的report
在lint中,我们可以通过--html选项接文件路径的形式把代码扫描结果以html文件的形式进行输出。
示例6
html输出报告如图2所示
图2
By default, links to source files will just use local file:// path resources. You can remap the URLs to a different prefix with the --url option. For example:
2.5、命令行帮助
在lint中,你可以使用--help选项来得到lint命令的一些帮助信息。
示例7
lint --help
三、Eclispe中使用Lint
从ADT16开始,lint就集成到了ADT中。该它在lint命令行的基础上新增了以下功能
Automatic fixes for many warnings自动修正大量警告
Lint gets run automatically on various editing operations当编辑操作完成后,立即自动运行
Ability to suppress types of errors as well as specific instances of an error可以suppress(忽略)一种类型的erro,也可以suppress(忽略)特定的一个erro
Ability to configure issue severities能够配置issue(问题)的severities(严重性)
Jump directly to the problem source from the lint view通过lint视图能直接跳转到其问题对应的源码处
3.1、Automatic Lint
Lint将在以下情况下自动运行:
Export an APK. In this case it runs lint in a special mode which only looks for fatal errors (which is faster) and aborts the export if any fatal errors are found. You can turn off this in the Lint Options.在导出APK文件的时候,lint会做快速的扫描,以寻找fatal的错误。如果发现有fatal的错误,导出APK的操作将被迫终止
Edit and Save and XML file, such as a layout file or a manifest file. In this case, all the file-scope checks that apply to the given file are run and editor markers are added for any issues found.编辑和保存XML文件,lint也会自动扫描这些文件。另外从ADT20开始,对于java源码文件在编辑和保存后,lint也会对他们进行扫描。Use the layout editor. After every UI operation, file-scope
checks (such as the various layoutopt rules) are run on the layout file and the results are shown in a special lint window (which can be opened from the error marker which shows in the top right corner of the layout editor when errors are found).对于使用layout
editor来操作布局文件时,在每个UI操作后,lint也会自动扫描该布局文件。
3.2、Lint Window
在Eclipse中,你可以通过两种方式来手动进行lint的扫描:
一种方式是通过工具栏,双击图3-1中红色箭头指向的按钮,然后出现图3-2所示的下拉框,在该下拉选择要进行lint扫描的工程
图3-1
图3-2
一种方式是选中一个Android工程,单击右键,在下拉菜单中选择“Android tools”->"Run lint:check common erro",如图3-3所示
图3-3
运行lint之后,你将看到如图3-4或图3-5所示的lint 窗口
图3-4
图3-5
默认情况下,同一个类型的issue都是折叠成一块的,图3-4和图3-5是我手动展开的。
lint窗口的工具栏上有一些按钮,如图3-6所示
图3-6
它们的意义分别如下
Refresh,
which re-runs the current analysis on the same projects
Fix,
which automatically fixes the issue (this applies to issues where a quickfix is available)
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
Delete
this lint marker
Delete
all lint markers
Expand All,
Collapse
All
Configure
Columns用于设置在lint Window中对于检查出的issue的哪些项显示哪些项不显示,如图3-7所示
Edit
Options Edit Options 点击该按钮会弹出Lint
Preference dialog,如图1或图3-8在里面你可以定制默认/全局的Android Lint的基本检查规则,在其中可以设置所有项目默认的lint检查规则的检查级别,把检查级别(Severity)设为”ignore“,其实就是忽略(suppress)该检查规则
Configure Columns lets you edit which columns are visible. There are several new columns you can display, such as Category, Priority, etc, and you can
click on column headers to sort the display by the given column. There's also a new "Location" column, shown by default, which incorporates several different pieces of information: the file name, the line number, the parent folder name (useful when looking
at translation or configuration issues), and the project name:
Configure Columns用于设置在lint Window中对于检查出的issue的哪些项显示哪些项不显示,如图3-7所示
图3-7
The Edit Options actions brings up the Lint Preference dialog, which has also been improved. You can now search through the options by filter:
点击Edit Options按钮会弹出处理Lint Preference dialog,在里面可以定制默认/全局的Android Lint的基本检查规则
图3-8
3.3、Quick Fixes
Many lint warnings have automatic fixes. For example, the various layoutopt fixes suggest replacements (e.g. replace wrap_content with 0dp).
From the lint view(如图3-6所示), click the lightbulb
to invoke a fix.
From the layout editor warning summary, click the Fix button to fix.
And from the XML source editor, invoke the Quick Fix (Ctrl-1 or Command-1) and pick the quick fix associated with the warning.
3.4、Suppressing Errors(检查规则的基本定制)
From the editor quick fix menu(如图3-9所示),
you can also choose to
Ignore the warning in this file only
Ignore the warning in this project
Ignore the warning, period.
Ignore warnings using annotations or attributes, as explained here.
图3-9
(If you do not see the lint fix action in the quickfix list, see the Known Bugs section)
These choices are stored in a file named
the project, which is also read by the command line tool. Thus, you can ignore warnings from the UI, and check in the
with your source projects, and others running lint will not see warnings you have ignored (presumably because they have been manually verified).
你的选择在被存在Android工程目录下的
16提供的新工具,它是一个代码扫描工具,能够帮助我们识别代码结构存在的问题,主要包括:
1)布局性能(以前是 layoutopt工具,可以解决无用布局、嵌套太多、布局太多)
2)未使用到资源
3)不一致的数组大小
4)国际化问题(硬编码)
5)图标的问题(重复的图标,错误的大小)
6)可用性问题(如不指定的文本字段的输入型)
7)manifest文件的错误
Android lint可以解决如上的问题,当然还有更多,具体的可以参考Android
Lint Checks。Android官方也总结了lint能解决的问题,如下图。
lint是命令工具,它已经完美的集成到了Eclipse中,我们可以方便的使用。通过lint,我们可以检测出每个问题的说明和问题的严重性,根据检测报告可以对程序作出改进。下面介绍下在Eclipse怎么使用lint。
lint的使用可以通过两个途径,Eclipse左上角的打钩的按钮或者选择项目->右键->Android Tools,如下图所示:
图一 图二
lint工具简单实用,自动化分析,分析完成会给我们分析报告:
分析包括中会包括错误和警告,会给出具体的描述、类别、位置。上图是一个错误的描述,下图给出警告描述。
Android lint是对android开发者很有帮助的一款工具,对于项目打包发布前优化代码、查找没用到的资源、查找错误等非常有帮助。作为开发者是必须掌握的工具之一,如果想了解更多可以参考lint。
二、命令行中使用Lint
2.1、基本使用
4000
There is a command line tool in the SDK tools/ directory called
lint.
If you have the SDK
tools/directory
on your path, you can invoke it as “
lint”. Just point to a specific Android
project directory. You can also point to a random directory, which (if it is not an Android project) will be searched recursively and all projects under that directory will be checked. (And you can also specify multiple projects separated by spaces)
在Android SDK的tools下有个叫lint.bat的文件,它就是lint的命令行工具。
lint命令后可以带一个或多个参数,参数之间用空格隔开,参数表示的是需要使用lint进行扫描的Android项目的目录。
示例1
linux命令行
<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">src</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln">astrid</span><span class="pun" style="color: rgb(102, 102, 0);">/</span></div><div><span class="typ" style="color: rgb(102, 0, 102);">Scanning</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">GreenDroid</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="typ" style="color: rgb(102, 0, 102);">GoogleAPIs</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">..</span></div><div><span class="typ" style="color: rgb(102, 0, 102);">Scanning</span><span class="pln"> stream</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">...</span></div><div><span class="typ" style="color: rgb(102, 0, 102);">Scanning</span><span class="pln"> api</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">...........................</span></div><div><span class="typ" style="color: rgb(102, 0, 102);">Scanning</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">GDCatalog</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">.......................</span></div><div><span class="typ" style="color: rgb(102, 0, 102);">Scanning</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">GreenDroid</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">...........................................................</span></div><div><span class="typ" style="color: rgb(102, 0, 102);">Scanning</span><span class="pln"> tests</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">...</span></div><div><span class="typ" style="color: rgb(102, 0, 102);">Scanning</span><span class="pln"> filters</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">....</span></div><div><span class="typ" style="color: rgb(102, 0, 102);">Scanning</span><span class="pln"> tests</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">.....</span></div><div><span class="typ" style="color: rgb(102, 0, 102);">Scanning</span><span class="pln"> astrid</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">....................................................................................................................................................</span></div><div><span class="typ" style="color: rgb(102, 0, 102);">Scanning</span><span class="pln"> simple</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">.......</span></div><div><span class="pln">api</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln">res</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln">values</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">ca</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Error</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Locale</span><span class="pln"> ca </span><span class="kwd" style="color: rgb(0, 0, 136);">is</span><span class="pln"> missing translations </span><span class="kwd" style="color: rgb(0, 0, 136);">for</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> sync_SPr_bgwifi_key</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> sync_SPr_forget_key</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> sync_SPr_interval_values</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> sync_SPr_logged_in_prefix</span><span class="pun" style="color: rgb(102, 102, 0);">...</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pln"> more</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">MissingTranslation</span><span class="pun" style="color: rgb(102, 102, 0);">]</span></div><div><span class="pln">astrid</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln">res</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln">values</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">ca</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Error</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Locale</span><span class="pln"> ca </span><span class="kwd" style="color: rgb(0, 0, 136);">is</span><span class="pln"> missing translations </span><span class="kwd" style="color: rgb(0, 0, 136);">for</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> DLG_cancel</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> DLG_dismiss</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> DLG_ok</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">EPr_deactivated</span><span class="pun" style="color: rgb(102, 102, 0);">...</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">117</span><span class="pln"> more</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">MissingTranslation</span><span class="pun" style="color: rgb(102, 102, 0);">]</span></div><div><span class="pln">api</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln">res</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln">values</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">cs</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Error</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Locale</span><span class="pln"> cs </span><span class="kwd" style="color: rgb(0, 0, 136);">is</span><span class="pln"> missing translations </span><span class="kwd" style="color: rgb(0, 0, 136);">for</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> sync_SPr_bgwifi_key</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> sync_SPr_forget_key</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> sync_SPr_interval_values</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> sync_SPr_logged_in_prefix</span><span class="pun" style="color: rgb(102, 102, 0);">...</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pln"> more</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">MissingTranslation</span><span class="pun" style="color: rgb(102, 102, 0);">]</span></div><div><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln">many lines omitted</span><span class="pun" style="color: rgb(102, 102, 0);">)</span></div><div><span class="lit" style="color: rgb(0, 102, 102);">43</span><span class="pln"> errors</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> </span><span class="lit" style="color: rgb(0, 102, 102);">466</span><span class="pln"> warnings</span></div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>
示例2
Window命令行
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div><span class="pln">C</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">\Documents </span><span class="kwd" style="color: rgb(0, 0, 136);">and</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Settings</span><span class="pln">\Administrator</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln">lint D</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">\workspace\Test</span> <span class="typ" style="color: rgb(102, 0, 102);">Scanning</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Test</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">.........................................................................................................</span> <span class="pun" style="color: rgb(102, 102, 0);">........................................................................................................................</span> <span class="pun" style="color: rgb(102, 102, 0);">...................</span> <span class="typ" style="color: rgb(102, 0, 102);">Scanning</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Test</span><span class="pln"> </span><span class="pun" style 21721 ="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Phase</span><span class="pln"> </span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pun" style="color: rgb(102, 102, 0);">):</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">......</span> <span class="pln">res\layout\internet_image_demo</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">xml</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="lit" style="color: rgb(0, 102, 102);">9</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">The</span><span class="pln"> id </span><span class="str" style="color: rgb(0, 136, 0);">"button1"</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">is</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">not</span><span class="pln"> referring to any views </span><span class="kwd" style="color: rgb(0, 0, 136);">in</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pln"> layout </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">UnknownIdI</span> <span class="pln">nLayout</span><span class="pun" style="color: rgb(102, 102, 0);">]</span> <span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">layout_alignLeft</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(0, 136, 0);">"@+id/button1"</span> <span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">^</span> <span class="pln">res\layout\internet_image_demo</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">xml</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">The</span><span class="pln"> id </span><span class="str" style="color: rgb(0, 136, 0);">"textView1"</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">is</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">not</span><span class="pln"> referring to any views </span><span class="kwd" style="color: rgb(0, 0, 136);">in</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pln"> layout </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">Unknown</span> <span class="typ" style="color: rgb(102, 0, 102);">IdInLayout</span><span class="pun" style="color: rgb(102, 102, 0);">]</span> <span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">layout_below</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(0, 136, 0);">"@+id/textView1"</span> <span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">^</span> <span class="typ" style="color: rgb(102, 0, 102);">AndroidManifest</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">xml</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="lit" style="color: rgb(0, 102, 102);">52</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Exported</span><span class="pln"> receiver does </span><span class="kwd" style="color: rgb(0, 0, 136);">not</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">require</span><span class="pln"> permission </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">ExportedReceiver</span><span class="pun" style="color: rgb(102, 102, 0);">]</span> <span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="pln">receiver android</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(0, 136, 0);">".AlarmReceiver"</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">></span> <span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">^</span> <span class="pln">res\menu\activity_main</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">xml</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">The</span><span class="pln"> resource R</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">menu</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">activity_main appears to be unused </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">UnusedResources</span><span class="pun" style="color: rgb(102, 102, 0);">]</span> <span class="pln">res\drawable</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">hdpi\ic_action_search</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">png</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">The</span><span class="pln"> resource R</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">drawable</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">ic_action_search appears to be unused </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">UnusedRe</span> <span class="pln">sources</span><span class="pun" style="color: rgb(102, 102, 0);">]</span> <span class="pln">res\values\strings</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">xml</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="lit" style="color: rgb(0, 102, 102);">7</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">The</span><span class="pln"> resource R</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">string</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">hello appears to be unused </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">UnusedResources</span><span class="pun" style="color: rgb(102, 102, 0);">]</span> <span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="kwd" style="color: rgb(0, 0, 136);">string</span><span class="pln"> name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(0, 136, 0);">"hello"</span><span class="pun" style="color: rgb(102, 102, 0);">>你好!</</span><span class="kwd" style="color: rgb(0, 0, 136);">string</span><span class="pun" style="color: rgb(102, 102, 0);">></span> <span class="pun" style="color: rgb(102, 102, 0);">^</span> <span class="pln">res\drawable</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">mdpi</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Missing</span><span class="pln"> the following drawables </span><span class="kwd" style="color: rgb(0, 0, 136);">in</span><span class="pln"> drawable</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">mdpi</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> icon</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">png</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> icon2</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">png </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">IconDensities</span><span class="pun" style="color: rgb(102, 102, 0);">]</span> <span class="pln">res\drawable</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">xhdpi</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Missing</span><span class="pln"> the following drawables </span><span class="kwd" style="color: rgb(0, 0, 136);">in</span><span class="pln"> drawable</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">xhdpi</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> icon</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">png</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> icon2</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">png </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">IconDensities</span><span class="pun" style="color: rgb(102, 102, 0);">]</span> <span class="pln">res\layout\internet_image_demo</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">xml</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="lit" style="color: rgb(0, 102, 102);">5</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">Accessibility</span><span class="pun" style="color: rgb(102, 102, 0);">]</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Missing</span><span class="pln"> contentDescription attribute on image </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">ContentDes</span> <span class="pln">cription</span><span class="pun" style="color: rgb(102, 102, 0);">]</span> <span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="typ" style="color: rgb(102, 0, 102);">ImageView</span> <span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">^</span> <span class="pln">res\layout\activity_main</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">xml</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="lit" style="color: rgb(0, 102, 102);">17</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="pln">I18N</span><span class="pun" style="color: rgb(102, 102, 0);">]</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Hardcoded</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">string</span><span class="pln"> </span><span class="str" style="color: rgb(0, 136, 0);">"go Hello"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> should </span><span class="kwd" style="color: rgb(0, 0, 136);">use</span><span class="pln"> </span><span class="lit" style="color: rgb(0, 102, 102);">@string</span><span class="pln"> resource </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">HardcodedText</span> <span class="pun" style="color: rgb(102, 102, 0);">]</span> <span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">text</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(0, 136, 0);">"go Hello"</span> <span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">^</span> <span class="pln">res\layout\activity_main</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">xml</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="lit" style="color: rgb(0, 102, 102);">23</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="pln">I18N</span><span class="pun" style="color: rgb(102, 102, 0);">]</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Hardcoded</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">string</span><span class="pln"> </span><span class="str" style="color: rgb(0, 136, 0);">"打印所有任务栈信息"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> should </span><span class="kwd" style="color: rgb(0, 0, 136);">use</span><span class="pln"> </span><span class="lit" style="color: rgb(0, 102, 102);">@string</span><span class="pln"> resource </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">Har</span> <span class="pln">dcodedText</span><span class="pun" style="color: rgb(102, 102, 0);">]</span> <span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">text</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(0, 136, 0);">"打印所有任务栈信息"</span> <span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">^</span> <span class="pln">res\layout\activity_main</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">xml</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="lit" style="color: rgb(0, 102, 102);">29</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="pln">I18N</span><span class="pun" style="color: rgb(102, 102, 0);">]</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Hardcoded</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">string</span><span class="pln"> </span><span class="str" style="color: rgb(0, 136, 0);">"打印所有服务信息"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> should </span><span class="kwd" style="color: rgb(0, 0, 136);">use</span><span class="pln"> </span><span class="lit" style="color: rgb(0, 102, 102);">@string</span><span class="pln"> resource </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">Hardc</span> <span class="pln">odedText</span><span class="pun" style="color: rgb(102, 102, 0);">]</span> <span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">text</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(0, 136, 0);">"打印所有服务信息"</span> <span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">^</span> <span class="pln">res\layout\activity_main</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">xml</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="lit" style="color: rgb(0, 102, 102);">35</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="pln">I18N</span><span class="pun" style="color: rgb(102, 102, 0);">]</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Hardcoded</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">string</span><span class="pln"> </span><span class="str" style="color: rgb(0, 136, 0);">"打印进程信息"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> should </span><span class="kwd" style="color: rgb(0, 0, 136);">use</span><span class="pln"> </span><span class="lit" style="color: rgb(0, 102, 102);">@string</span><span class="pln"> resource </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">Hardcoded</span> <span class="typ" style="color: rgb(102, 0, 102);">Text</span><span class="pun" style="color: rgb(102, 102, 0);">]</span> <span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">text</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(0, 136, 0);">"打印进程信息"</span> <span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">^</span> <span class="pln">res\layout\hello</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">xml</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="lit" style="color: rgb(0, 102, 102);">23</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="pln">I18N</span><span class="pun" style="color: rgb(102, 102, 0);">]</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Hardcoded</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">string</span><span class="pln"> </span><span class="str" style="color: rgb(0, 136, 0);">"please click me"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> should </span><span class="kwd" style="color: rgb(0, 0, 136);">use</span><span class="pln"> </span><span class="lit" style="color: rgb(0, 102, 102);">@string</span><span class="pln"> resource </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">HardcodedText</span><span class="pun" style="color: rgb(102, 102, 0);">]</span> <span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">text</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(0, 136, 0);">"please click me"</span> <span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">^</span> <span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pln"> errors</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> </span><span class="lit" style="color: rgb(0, 102, 102);">14</span><span class="pln"> warnings</span> </div><div> </div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>
2.2、Disabling Checks(--disable)
在执行lint命令时可以通过--disable选项要指定关闭的检查规则项。--disable选项后接要关闭的检查规则项的id(比如示例3中的MissingTranslation)或检查规则项的类别(比如示例3中的Usability:Icons和示例4的Internationalization)。
关于lint检查项的id和类别(Category)等信息请参考《Android
Lint 检查规则列表》
示例3
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln">$ lint </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln">disable </span><span class="typ" style="color: rgb(102, 0, 102);">MissingTranslation</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="typ" style="color: rgb(102, 0, 102);">UnusedIds</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="typ" style="color: rgb(102, 0, 102);">Usability</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="typ" style="color: rgb(102, 0, 102);">Icons</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln">src</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln">astrid</span><span class="pun" style="color: rgb(102, 102, 0);">/</span></p>
示例4
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div><span class="pln">C</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">\Documents </span><span class="kwd" style="color: rgb(0, 0, 136);">and</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Settings</span><span class="pln">\Administrator</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln">lint </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln">disable </span><span class="typ" style="color: rgb(102, 0, 102);">Internationalization</span><span class="pln"> D</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">\workspace\Test</span></div><div> </div><div><span class="typ" style="color: rgb(102, 0, 102);">Scanning</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Test</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">.........................................................................................................</span></div><div><span class="pun" style="color: rgb(102, 102, 0);">........................................................................................................................</span></div><div><span class="pun" style="color: rgb(102, 102, 0);">...................</span></div><div><span class="typ" style="color: rgb(102, 0, 102);">Scanning</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Test</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Phase</span><span class="pln"> </span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pun" style="color: rgb(102, 102, 0);">):</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">......</span></div><div><span class="pln">res\layout\internet_image_demo</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">xml</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="lit" style="color: rgb(0, 102, 102);">9</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">The</span><span class="pln"> id </span><span class="str" style="color: rgb(0, 136, 0);">"button1"</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">is</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">not</span><span class="pln"> referring to any views </span><span class="kwd" style="color: rgb(0, 0, 136);">in</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pln"> layout </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">UnknownIdI</span></div><div><span class="typ" style="color: rgb(102, 0, 102);">nLayout</span><span class="pun" style="color: rgb(102, 102, 0);">]</span></div><div><span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">layout_alignLeft</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(0, 136, 0);">"@+id/button1"</span></div><div><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">^</span></div><div><span class="pln">res\layout\internet_image_demo</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">xml</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">The</span><span class="pln"> id </span><span class="str" style="color: rgb(0, 136, 0);">"textView1"</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">is</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">not</span><span class="pln"> referring to any views </span><span class="kwd" style="color: rgb(0, 0, 136);">in</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pln"> layout </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">Unknown</span></div><div><span class="typ" style="color: rgb(102, 0, 102);">IdInLayout</span><span class="pun" style="color: rgb(102, 102, 0);">]</span></div><div><span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">layout_below</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(0, 136, 0);">"@+id/textView1"</span></div><div><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">^</span></div><div><span class="typ" style="color: rgb(102, 0, 102);">AndroidManifest</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">xml</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="lit" style="color: rgb(0, 102, 102);">52</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Exported</span><span class="pln"> receiver does </span><span class="kwd" style="color: rgb(0, 0, 136);">not</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">require</span><span class="pln"> permission </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">ExportedReceiver</span><span class="pun" style="color: rgb(102, 102, 0);">]</span></div><div><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="pln">receiver android</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(0, 136, 0);">".AlarmReceiver"</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">></span></div><div><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">^</span></div><div><span class="pln">res\menu\activity_main</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">xml</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">The</span><span class="pln"> resource R</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">menu</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">activity_main appears to be unused </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">UnusedResources</span><span class="pun" style="color: rgb(102, 102, 0);">]</span></div><div><span class="pln">res\drawable</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">hdpi\ic_action_search</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">png</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">The</span><span class="pln"> resource R</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">drawable</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">ic_action_search appears to be unused </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">UnusedRe</span></div><div><span class="typ" style="color: rgb(102, 0, 102);">sources</span><span class="pun" style="color: rgb(102, 102, 0);">]</span></div><div><span class="pln">res\values\strings</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">xml</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="lit" style="color: rgb(0, 102, 102);">7</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">The</span><span class="pln"> resource R</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">string</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">hello appears to be unused </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">UnusedResources</span><span class="pun" style="color: rgb(102, 102, 0);">]</span></div><div><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="kwd" style="color: rgb(0, 0, 136);">string</span><span class="pln"> name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(0, 136, 0);">"hello"</span><span class="pun" style="color: rgb(102, 102, 0);">>你好!</</span><span class="kwd" style="color: rgb(0, 0, 136);">string</span><span class="pun" style="color: rgb(102, 102, 0);">></span></div><div><span class="pun" style="color: rgb(102, 102, 0);">^</span></div><div><span class="pln">res\drawable</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">mdpi</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Missing</span><span class="pln"> the following drawables </span><span class="kwd" style="color: rgb(0, 0, 136);">in</span><span class="pln"> drawable</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">mdpi</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> icon</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">png</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> icon2</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">png </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">IconDensities</span><span class="pun" style="color: rgb(102, 102, 0);">]</span></div><div><span class="pln">res\drawable</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">xhdpi</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Missing</span><span class="pln"> the following drawables </span><span class="kwd" style="color: rgb(0, 0, 136);">in</span><span class="pln"> drawable</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">xhdpi</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> icon</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">png</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> icon2</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">png </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">IconDensities</span><span class="pun" style="color: rgb(102, 102, 0);">]</span></div><div><span class="pln">res\layout\internet_image_demo</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">xml</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="lit" style="color: rgb(0, 102, 102);">5</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Warning</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">Accessibility</span><span class="pun" style="color: rgb(102, 102, 0);">]</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Missing</span><span class="pln"> contentDescription attribute on image </span><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">ContentDes</span></div><div><span class="typ" style="color: rgb(102, 0, 102);">cription</span><span class="pun" style="color: rgb(102, 102, 0);">]</span></div><div><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);"><</span><span class="typ" style="color: rgb(102, 0, 102);">ImageView</span></div><div><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">^</span></div><div><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pln"> errors</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> </span><span class="lit" style="color: rgb(0, 102, 102);">9</span><span class="pln"> warnings</span></div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>
2.3、enabling Checks(--enable和--check)
lint的有些检查项默认是关闭的(disable),在执行lint命令时可以通过--enable选项开启它。-enable选项后接要开启的检查规则项的id(比如示例5中的MissingTranslation )或检查规则项的类别(示例5中的Usability:Icons)
示例5
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln">$ lint </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln">disable </span><span class="typ" style="color: rgb(102, 0, 102);">MissingTranslation</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="typ" style="color: rgb(102, 0, 102);">UnusedIds</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="typ" style="color: rgb(102, 0, 102);">Usability</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="typ" style="color: rgb(102, 0, 102);">Icons</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln">src</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln">astrid</span><span class="pun" style="color: rgb(102, 102, 0);">/</span></p>
在执行lint命令时可以通过--check选项来指定只进行某些检查。-check选项后接要开启的检查规则项的id(比如示例6中的MissingPrefix)
示例6
$ lint --check MissingPrefix /src/astrid/
2.4、检查项类别和检查项id
可以通过lint的--list选项来得到检查项类别和检查项id.
比如:
$
lint
--list
Valid issue categories:
Correctness
Security
Performance
Usability
Usability:Icons
Accessibility
Internationalization
Valid issue id's:
"ContentDescription": Ensures that image widgets provide a contentDescription
"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
"ScrollViewSize": Checks that ScrollViews use wrap_content in scrolling dimension
"MergeRootFrame": Checks whether a root <FrameLayout> can be replaced with a <merge> tag
...
可以通过lint的--show选项后跟检查项id来得到一个检查项的详细说明.
比如:
$ lint --show MissingPrefix
MissingPrefix
-------------
Summary: Detect XML attributes not using the Android namespace
Priority: 8 / 10
Severity: Warning
Category: Correctness
Most Android views have attributes in the Android namespace. When
referencing these attributes you *must* include the namespace prefix,
or your attribute will be interpreted by aapt as just a custom
attribute.
当然你也可以通过《Android
Lint 检查规则列表》来查阅检查项的id等详细信息
2.4、html形式的report
在lint中,我们可以通过--html选项接文件路径的形式把代码扫描结果以html文件的形式进行输出。
示例6
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div><span class="pln">C</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">\Documents </span><span class="kwd" style="color: rgb(0, 0, 136);">and</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Settings</span><span class="pln">\Administrator</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln">lint </span><span class="pun" style="color: rgb(102, 102, 0);">--</span><span class="pln">html D</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">\workspace\Test\report</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">htm</span></div><div><span class="pln">l D</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">\workspace\Test</span></div><div> </div><div><span class="typ" style="color: rgb(102, 0, 102);">Scanning</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Test</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">.................................................................</span></div><div><span class="pun" style="color: rgb(102, 102, 0);">................................................................................</span></div><div><span class="pun" style="color: rgb(102, 102, 0);">................................................................................</span></div><div><span class="pun" style="color: rgb(102, 102, 0);">...................</span></div><div><span class="typ" style="color: rgb(102, 0, 102);">Scanning</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Test</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Phase</span><span class="pln"> </span><span class="lit" style="color: rgb(0, 102, 102);">2</span><span class="pun" style="color: rgb(102, 102, 0);">):</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">......</span></div><div><span class="typ" style="color: rgb(102, 0, 102);">Wrote</span><span class="pln"> HTML report to D</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">\workspace\Test\report</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">html</span></div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>
html输出报告如图2所示
图2
By default, links to source files will just use local file:// path resources. You can remap the URLs to a different prefix with the --url option. For example:
$ lint --html /tmp/report.html --url /src/MyProj=http://buildserver/src/MyProj
2.5、命令行帮助
在lint中,你可以使用--help选项来得到lint命令的一些帮助信息。
示例7
lint --help
三、Eclispe中使用Lint
从ADT16开始,lint就集成到了ADT中。该它在lint命令行的基础上新增了以下功能
Automatic fixes for many warnings自动修正大量警告
Lint gets run automatically on various editing operations当编辑操作完成后,立即自动运行
Ability to suppress types of errors as well as specific instances of an error可以suppress(忽略)一种类型的erro,也可以suppress(忽略)特定的一个erro
Ability to configure issue severities能够配置issue(问题)的severities(严重性)
Jump directly to the problem source from the lint view通过lint视图能直接跳转到其问题对应的源码处
3.1、Automatic Lint
Lint将在以下情况下自动运行:
Export an APK. In this case it runs lint in a special mode which only looks for fatal errors (which is faster) and aborts the export if any fatal errors are found. You can turn off this in the Lint Options.在导出APK文件的时候,lint会做快速的扫描,以寻找fatal的错误。如果发现有fatal的错误,导出APK的操作将被迫终止
Edit and Save and XML file, such as a layout file or a manifest file. In this case, all the file-scope checks that apply to the given file are run and editor markers are added for any issues found.编辑和保存XML文件,lint也会自动扫描这些文件。另外从ADT20开始,对于java源码文件在编辑和保存后,lint也会对他们进行扫描。Use the layout editor. After every UI operation, file-scope
checks (such as the various layoutopt rules) are run on the layout file and the results are shown in a special lint window (which can be opened from the error marker which shows in the top right corner of the layout editor when errors are found).对于使用layout
editor来操作布局文件时,在每个UI操作后,lint也会自动扫描该布局文件。
3.2、Lint Window
在Eclipse中,你可以通过两种方式来手动进行lint的扫描:
一种方式是通过工具栏,双击图3-1中红色箭头指向的按钮,然后出现图3-2所示的下拉框,在该下拉选择要进行lint扫描的工程
图3-1
图3-2
一种方式是选中一个Android工程,单击右键,在下拉菜单中选择“Android tools”->"Run lint:check common erro",如图3-3所示
图3-3
运行lint之后,你将看到如图3-4或图3-5所示的lint 窗口
图3-4
图3-5
默认情况下,同一个类型的issue都是折叠成一块的,图3-4和图3-5是我手动展开的。
lint窗口的工具栏上有一些按钮,如图3-6所示
图3-6
它们的意义分别如下
Refresh,
which re-runs the current analysis on the same projects
Fix,
which automatically fixes the issue (this applies to issues where a quickfix is available)
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
Delete
this lint marker
Delete
all lint markers
Expand All,
Collapse
All
Configure
Columns用于设置在lint Window中对于检查出的issue的哪些项显示哪些项不显示,如图3-7所示
Edit
Options Edit Options 点击该按钮会弹出Lint
Preference dialog,如图1或图3-8在里面你可以定制默认/全局的Android Lint的基本检查规则,在其中可以设置所有项目默认的lint检查规则的检查级别,把检查级别(Severity)设为”ignore“,其实就是忽略(suppress)该检查规则
Configure Columns lets you edit which columns are visible. There are several new columns you can display, such as Category, Priority, etc, and you can
click on column headers to sort the display by the given column. There's also a new "Location" column, shown by default, which incorporates several different pieces of information: the file name, the line number, the parent folder name (useful when looking
at translation or configuration issues), and the project name:
Configure Columns用于设置在lint Window中对于检查出的issue的哪些项显示哪些项不显示,如图3-7所示
图3-7
The Edit Options actions brings up the Lint Preference dialog, which has also been improved. You can now search through the options by filter:
点击Edit Options按钮会弹出处理Lint Preference dialog,在里面可以定制默认/全局的Android Lint的基本检查规则
图3-8
3.3、Quick Fixes
Many lint warnings have automatic fixes. For example, the various layoutopt fixes suggest replacements (e.g. replace wrap_content with 0dp).
From the lint view(如图3-6所示), click the lightbulb
to invoke a fix.
From the layout editor warning summary, click the Fix button to fix.
And from the XML source editor, invoke the Quick Fix (Ctrl-1 or Command-1) and pick the quick fix associated with the warning.
3.4、Suppressing Errors(检查规则的基本定制)
From the editor quick fix menu(如图3-9所示),
you can also choose to
Ignore the warning in this file only
Ignore the warning in this project
Ignore the warning, period.
Ignore warnings using annotations or attributes, as explained here.
图3-9
(If you do not see the lint fix action in the quickfix list, see the Known Bugs section)
These choices are stored in a file named
lint.xmlin
the project, which is also read by the command line tool. Thus, you can ignore warnings from the UI, and check in the
lint.xmlfile
with your source projects, and others running lint will not see warnings you have ignored (presumably because they have been manually verified).
你的选择在被存在Android工程目录下的
lint.xml文件中
相关文章推荐
- Android代码优化——使用Android lint工具
- 【lint】Android代码优化——使用Android lint工具
- Android代码优化——使用Android lint工具
- Android代码优化——使用Android lint工具
- Android代码优化工具——Android lint
- 使用lint工具优化Android代码
- Android代码优化——使用Android lint工具
- Android代码优化——使用Android lint工具
- -------------------Android代码优化——使用Android lint工具
- 使用lint工具优化Android代码
- Android代码优化——使用Android lint工具
- Android 静态代码工具-lint命令
- android studio lint工具实现代码检查优化
- AndroidStudio代码优化之Lint工具的使用
- Android代码优化——使用Android lint工具
- Android代码优化——使用Android lint工具
- Android代码优化工具——Android lint
- Android代码优化——使用Android lint工具
- Android代码优化—使用Android lint工具
- Android Lint工具 优化代码 <12>