英文原文:http://tools.android.com/tips/lint html
參照文章:http://blog.csdn.net/thl789/article/details/8037473java
1、簡介linux
Android Lint是SDK Tools 16 (ADT 16)以後才引入的工具,經過它對Android工程源代碼進行掃描和檢查,可發現潛在的問題,以便程序員及早修正這個問題。Android Lint提供了命令行方式執行,還可與IDE(如Eclipse)集成,並提供了html形式的輸出報告。android
因爲Android Lint在最初設計時就考慮到了independent於IDE,因此它能夠很方便的與項目中的其餘自動系統(配置/ Build / 測試等)集成.程序員
Android Lint主要用於檢查如下這些錯誤:api
一、Missing translations (and unused translations)沒有翻譯的文本數組
二、Layout performance problems (all the issues the old layoutopt tool used to find, and more)app
三、Unused resources未使用的冗餘資源dom
四、Inconsistent array sizes (when arrays are defined in multiple configurations)在多個配置中的數組大小不一致文件ide
五、Accessibility and internationalization problems (hardcoded strings, missing contentDescription, etc)
六、Icon problems (like missing densities, duplicate icons, wrong sizes, etc)
七、Usability problems (like not specifying an input type on a text field)
八、Manifest errors
固然Android Lint遠遠不至檢查以上的錯誤,更多的內容請參考《Android Lint 檢查規則列表》
在Eclipse中能夠在菜單Window->Preference->「Lint Eerro checking」中設置規則的檢查級別,如圖1所示。
檢查級別能夠是:
Default
Fatal
Errro
Waring
Information
Ingore(即不檢查)
圖1
若是你只是想對lint的檢查規則作些簡單的定製,請參考《Android Lint 檢查規則的定製(基本篇)》或英文官方文檔
若是你想對lint的檢查規則作些高級的定製,請參考官方文檔 Writing New Lint Checks and Writing Custom Lint Rules.
2、命令行中使用Lint
2.一、基本使用
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\TestScanning 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 [UnknownIdInLayout] 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 [UnknownIdInLayout] 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 [UnusedResources]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 [ContentDescription] <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 [HardcodedText] android:text="打印全部任務棧信息" ^res\layout\activity_main.xml:29: Warning: [I18N] Hardcoded string "打印全部服務信息", should use @string resource [HardcodedText] android:text="打印全部服務信息" ^res\layout\activity_main.xml:35: Warning: [I18N] Hardcoded string "打印進程信息", should use @string resource [HardcodedText] 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.二、Disabling Checks(--disable)
在執行lint命令時能夠經過--disable選項要指定關閉的檢查規則項。--disable選項後接要關閉的檢查規則項的id(好比示例3中的MissingTranslation)或檢查規則項的類別(好比示例3中的Usability:Icons和示例4的Internationalization)。
關於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\TestScanning 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 [UnknownIdInLayout] 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 [UnknownIdInLayout] 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 [UnusedResources]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 [ContentDescription] <ImageView ^0 errors, 9 warnings
2.三、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.四、檢查項類別和檢查項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.四、html形式的report
在lint中,咱們能夠經過--html選項接文件路徑的形式把代碼掃描結果以html文件的形式進行輸出。
示例6
C:\Documents and Settings\Administrator>lint --html D:\workspace\Test\report.html D:\workspace\TestScanning Test: ....................................................................................................................................................................................................................................................Scanning Test (Phase 2): ......Wrote HTML report to D:\workspace\Test\report.html
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.五、命令行幫助
在lint中,你可使用--help選項來獲得lint命令的一些幫助信息。
示例7
lint --help
3、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.一、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.二、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.三、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.四、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.xml
in the project, which is also read by the command line tool. Thus, you can ignore warnings from the UI, and check in thelint.xml
file 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
文件中