標籤:工具 android 代碼
一、概述
Android lint工具是一個靜態程式碼分析工具,用來檢查代碼中潛在的問題並且提高代碼的正確性,安全性,可用性,國際化和效能。
確保代碼中沒有結構性的錯誤,發現潛在的問題。Android-Lint提供了命令列方式執行,還可與IDE(eclipse、Idea、AndroidStudio)整合,並提供了html形式的輸出報告。Android-Lint可以方便的與項目中的其他自動系統(配置/ Build / 測試等)整合。
二、應用2.1 自動執行
以AndroidStudio為例,當build應用時,lint就會再自動運行。並且如果報錯的話就會停止build。我們可以在項目的gradle設定檔中配置lint選項
android { lintOptions { // set to true to turn off analysis progress reporting by lint quiet true // if true, stop the gradle build if errors are found abortOnError false // if true, only report errors ignoreWarnings true } ... }
上面的代碼代表靜默執行,並且忽略lint報錯。
2.2手動執行
當然,我們還可以手動執行lint ,操作Analyze > Inspect Code。
2.3程式碼執行
lint提供了命令列執行,不知道怎麼執行的話可以 lint –help下。
Android-Lint所檢查的潛在問題,可以通過命令列$lint –show來獲得。可以參考這裡:
http://tools.android.com/tips/lint
檢查大概的格式是這樣的:
lint [flags] <project directory>
最簡單的一種用法:
lint Android項目目錄
例子:
lint app/ Scanning 1.0.1: .....Scanning 1.0.1 (Phase 2): ..Scanning umengupdate-release: ......................................Scanning unspecified: ...............................................Scanning unspecified (Phase 2): ....................build/intermediates/exploded-aar/umengupdate-release/res/layout/umeng_update_dialog.xml:47: Warning: Consider adding android:layout_marginEnd="8dp" to better support right-to-left layouts [RtlHardcoded] android:layout_marginRight="8dp" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~build/intermediates/exploded-aar/com.afollestad/material-dialogs/0.7.3.1/AndroidManifest.xml: Warning: The project references RTL attributes, but does not explicitly enable or disable RTL support with android:supportsRtl in the manifest [RtlEnabled]26 errors, 444 warnings
如下的命令可以檢測在xml檔案中沒有命名首碼的錯誤:
lint --check MissingPrefix myproject
具體的命令請參考官網
http://developer.android.com/tools/help/lint.html
三、lint配置3.1在設定中配置
在AndroidStudio中,我們可以在 File > Settings > Project Settings中配置lint在我們項目中得配置。
3.2 lint.xml
引用官方文檔中的一張圖:
很直觀的表達了lint.xml檔案的作用,通過lint.xml和lint Tool共同作用就可以檢查代碼中的問題。
一個例子:
<?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 specified files --> <issue id="ObsoleteLayoutParam"> <ignore path="res/layout/activation.xml" /> <ignore path="res/layout-xlarge/activation.xml" /> </issue> <!-- Ignore the UselessLeaf issue in the specified 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>
3.3在Java代碼和XML代碼中配置lint
當然在Java代碼中依然可以配置lint,例子:
@SuppressLint("NewApi")@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
上面的代碼代表在onCreate方法中關閉lint檢查”NewApi”,但是lint依然會檢查沒有 @SuppressLint(“NewApi”) 標記的其他方法。
類似的還有:
@SuppressLint("ParserError")public class FeedProvider extends ContentProvider {
關閉”ParserError”檢查。
如果想關閉搜尋的檢查項,可以這樣設定:
@SuppressLint("all")
在XML中,我們可以使用tools:ignore 來關閉相應lint檢查項,要使用”tools:”,首先要加上相應地namespaces
namespace xmlns:tools="http://schemas.android.com/tools"
例子如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:ignore="UnusedResources" > <TextView android:text="@string/auto_update_prompt" /></LinearLayout>
避免檢查沒有用到的資源檔。同樣地還有:
tools:ignore="NewApi,StringFormatInvalid"
相應地關閉所有的檢查項:
tools:ignore="all"
四、總結
lint工具對於個人開發人員個人感覺用處相對不大,但是如果是Team 專案,那麼它的作用就很大了,因為每個人的代碼習慣都是不一樣的,並且會定義很多的資源檔,這樣日積月累下來,apk的大小就會不必要的增大很多。個人感覺lint工具最重要的一個功能就是差unUseResources,可以刪掉好多不用的資源檔,給apk瘦身。
檢查結果在 Android Lint –> Unused resources.
這是我們項目中沒用的資源檔,好多沒用的圖片啊!!
刪掉之後清爽好多!
使用lint工具最佳化Android代碼