现在的位置: 首页 > 综合 > 正文

Android Lint简介

2013年10月12日 ⁄ 综合 ⁄ 共 7328字 ⁄ 字号 评论关闭

本人博客原文

英文原文:http://tools.android.com/tips/lint 

参照文章:http://blog.csdn.net/thl789/article/details/8037473

一、简介
Android Lint是SDK Tools 16 (ADT 16)之后才引入的工具,通过它对Android工程源代码进行扫描和检查,可发现潜在的问题,以便程序员及早修正这个问题。Android Lint提供了命令行方式执行,还可与IDE(如Eclipse)集成,并提供了html形式的输出报告。
由于Android Lint在最初设计时就考虑到了independent于IDE,所以它可以很方便的与项目中的其他自动系统(配置/ Build / 测试等)集成.
Android Lint主要用于检查以下这些错误:
1、Missing translations (and unused translations)没有翻译的文本
2、Layout performance problems (all the issues the old layoutopt tool used to find, and more)
3、Unused resources未使用的冗余资源
4、Inconsistent array sizes (when arrays are defined in multiple configurations)在多个配置中的数组大小不一致文件
5、Accessibility and internationalization problems (hardcoded strings, missing contentDescription, etc)
6、Icon problems (like missing densities, duplicate icons, wrong sizes, etc)
7、Usability problems (like not specifying an input type on a text field)
8、Manifest errors
当然Android Lint远远不至检查以上的错误,更多的内容请参考《Android
Lint 检查规则列表
在Eclipse中可以在菜单Window->Preference->“Lint Eerro checking”中设置规则的检查级别,如图1所示。
检查级别可以是:
Default
Fatal
Errro
Waring
Information
Ingore(即不检查)
图1
Android Lint 检查规则列表 - hubingforever - 民主与科学
 

如果你只是想对lint的检查规则做些简单的定制,请参考《Android Lint 检查规则的定制(基本篇)或英文官方文档
如果你想对lint的检查规则做些高级的定制,请参考官方文档 Writing
New Lint Checks
 and Writing
Custom Lint Rules
.
二、命令行中使用Lint
2.1、基本使用
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命令行

 lint /src/astrid/
Scanning GreenDroid-GoogleAPIs: ..
Scanning stream: ...
Scanning api: ...........................
Scanning GDCatalog: .......................
Scanning GreenDroid: ...........................................................
Scanning tests: ...
Scanning filters: ....
Scanning tests: .....
Scanning astrid: ....................................................................................................................................................
Scanning simple: .......
api/res/values-ca: Error: Locale ca is missing translations for: sync_SPr_bgwifi_key, sync_SPr_forget_key, sync_SPr_interval_values, sync_SPr_logged_in_prefix... (2 more) [MissingTranslation]
astrid/res/values-ca: Error: Locale ca is missing translations for: DLG_cancel, DLG_dismiss, DLG_ok, EPr_deactivated... (117 more) [MissingTranslation]
api/res/values-cs: Error: Locale cs is missing translations for: sync_SPr_bgwifi_key, sync_SPr_forget_key, sync_SPr_interval_values, sync_SPr_logged_in_prefix... (2 more) [MissingTranslation]
(many lines omitted)
43 errors, 466 warnings

示例2
Window命令行

C:\Documents and Settings\Administrator>lint D:\workspace\Test Scanning Test: ......................................................................................................... ........................................................................................................................ ................... Scanning Test (Phase 2): ...... res\layout\internet_image_demo.xml:9: Warning: The id "button1" is not referring to any views in this layout [UnknownIdI nLayout] android:layout_alignLeft="@+id/button1" ^ res\layout\internet_image_demo.xml:10: Warning: The id "textView1" is not referring to any views in this layout [Unknown IdInLayout] android:layout_below="@+id/textView1" ^ AndroidManifest.xml:52: Warning: Exported receiver does not require permission [ExportedReceiver] <receiver android:name=".AlarmReceiver" > ^ res\menu\activity_main.xml: Warning: The resource R.menu.activity_main appears to be unused [UnusedResources] res\drawable-hdpi\ic_action_search.png: Warning: The resource R.drawable.ic_action_search appears to be unused [UnusedRe sources] res\values\strings.xml:7: Warning: The resource R.string.hello appears to be unused [UnusedResources] <string name="hello">你好!</string> ^ res\drawable-mdpi: Warning: Missing the following drawables in drawable-mdpi: icon.png, icon2.png [IconDensities] res\drawable-xhdpi: Warning: Missing the following drawables in drawable-xhdpi: icon.png, icon2.png [IconDensities] res\layout\internet_image_demo.xml:5: Warning: [Accessibility] Missing contentDescription attribute on image [ContentDes cription] <ImageView ^ res\layout\activity_main.xml:17: Warning: [I18N] Hardcoded string "go Hello", should use @string resource [HardcodedText ] android:text="go Hello" ^ res\layout\activity_main.xml:23: Warning: [I18N] Hardcoded string "打印所有任务栈信息", should use @string resource [Har dcodedText] android:text="打印所有任务栈信息" ^ res\layout\activity_main.xml:29: Warning: [I18N] Hardcoded string "打印所有服务信息", should use @string resource [Hardc odedText] android:text="打印所有服务信息" ^ res\layout\activity_main.xml:35: Warning: [I18N] Hardcoded string "打印进程信息", should use @string resource [Hardcoded Text] android:text="打印进程信息" ^ res\layout\hello.xml:23: Warning: [I18N] Hardcoded string "please click me", should use @string resource [HardcodedText] android:text="please click me" ^ 0 errors, 14 warnings

2.2、Disabling Checks(--disable
在执行lint命令时可以通过--disable选项要指定关闭的检查规则项。--disable选项后接要关闭的检查规则项的id(比如示例3中的MissingTranslation)或检查规则项的类别(比如示例3中的Usability:Icons示例4Internationalization)。
关于lint检查项的id类别Category)等信息请参考《Android
Lint 检查规则列表
示例3

$ lint --disable MissingTranslation,UnusedIds,Usability:Icons /src/astrid/

示例4

C:\Documents and Settings\Administrator>lint --disable Internationalization D:\workspace\Test
Scanning Test: .........................................................................................................
........................................................................................................................
...................
Scanning Test (Phase 2): ......
res\layout\internet_image_demo.xml:9: Warning: The id "button1" is not referring to any views in this layout [UnknownIdI
nLayout]
        android:layout_alignLeft="@+id/button1"
        ^
res\layout\internet_image_demo.xml:10: Warning: The id "textView1" is not referring to any views in this layout [Unknown
IdInLayout]
        android:layout_below="@+id/textView1"
        ^
AndroidManifest.xml:52: Warning: Exported receiver does not require permission [ExportedReceiver]
        <receiver android:name=".AlarmReceiver" >
        ^
res\menu\activity_main.xml: Warning: The resource R.menu.activity_main appears to be unused [UnusedResources]
res\drawable-hdpi\ic_action_search.png: Warning: The resource R.drawable.ic_action_search appears to be unused [UnusedRe
sources]
res\values\strings.xml:7: Warning: The resource R.string.hello appears to be unused [UnusedResources]
<string name="hello">你好!</string>
^
res\drawable-mdpi: Warning: Missing the following drawables in drawable-mdpi: icon.png, icon2.png [IconDensities]
res\drawable-xhdpi: Warning: Missing the following drawables in drawable-xhdpi: icon.png, icon2.png [IconDensities]
res\layout\internet_image_demo.xml:5: Warning: [Accessibility] Missing contentDescription attribute on image [ContentDes
cription]
    <ImageView
    ^
0 errors, 9 warnings

2.3、enabling Checks(--enable--check)
lint的有些检查项默认是关闭的(disable),在执行lint命令时可以通过--enable选项开启它。-enable选项后接要开启的检查规则项的id(比如示例5中的MissingTranslation 检查规则项的类别(示例5中的Usability:Icons
示例5

$ lint --disable MissingTranslation,UnusedIds,Usability:Icons /src/astrid/

在执行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
"StateLi

抱歉!评论已关闭.