詳解Android Lint的原理及其使用
Android Lint 是 ADT 16中引入的新工具,用于掃描 Android 項目源中的潛在錯誤。
Lint 是 Android 提供的一個強大的,用于靜態掃描應用源碼并找出其中的潛在問題的實用工具。lint 工具可以檢查你的 Android 項目源文件是否有潛在的錯誤,以及在正確性、安全性、性能、易用性、無障礙性和國際化方面是否需要優化改進。
Lint 既可以用作命令行工具,也可以與 Eclipse 和 IntelliJ 集成在一起。它被設計成獨立于 IDE 的工具,我們可以在 Android Studio 中非常方便的使用它。
Lint 的工作過程lint 工具的代碼掃描工作流:
Lint 的工作過程由 Lint Tool(檢測工具),Source Files(項目源文件) 和 lint.xml(配置文件) 三個部分組成,Lint Tool 讀取 Source Files,根據 lint.xml 配置的規則(issue)輸出最終的結果。
Lint 的功能Lint 可以檢查并發現以下幾類問題:
缺少翻譯(和未使用的翻譯) 布局性能問題(所有以前實用 layoutopt 工具用來查找的問題,等等) 數組大小不一致 可訪問性和國際化問題(硬編碼字符串,缺少 contentDescription 等) 圖標問題(例如缺少密度,重復的圖標,錯誤的尺寸等) 可用性問題(如未在文本字段中指定輸入類型) Manifest 錯誤問題等級Lint 發現的每個問題都有描述信息和等級,我們可以很方便地定位問題,同時按照嚴重程度進行解決。當然,我們也可以手動配置每個問題的嚴重級別。Lint 本身包含大量已經封裝好的接口,能提供豐富的代碼信息,開發者可以基于這些信息進行自定義規則的編寫。
Lint 會按照問題的嚴重程度分為幾個等級:
Fatal Error Warning Information Ignore問題嚴重程序由高到低依次降低。
從命令行運行 lint如果你使用的是 Android Studio 或 Gradle,你可以在項目的根目錄下輸入以下某個命令,使用 Gradle 封裝容器對項目調用 lint 任務:
在 Windows 上:
gradlew lint
在 Linux 或 Mac 上:
./gradlew lint
lint 工具完成其檢查后,會提供 XML 和 HTML 版 lint 報告的路徑。然后,我們可以轉到 HTML 報告并在瀏覽器中將其打開
Android Studio 中使用 LintLint 已經被集成到 Android Studio,所以可以直接使用,使用非常方便。lint 的代碼掃描工具,可幫助你發現并更正代碼結構質量的問題,而無需您實際執行應用,也不必編寫測試用例。系統會報告該工具檢測到的每個問題并提供問題的描述消息和嚴重級別,以便你可以快速確定需要優先進行的關鍵改進。此外,你還可以降低問題的嚴重級別以忽略與項目無關的問題,或者提高嚴重級別以突出特定問題。
從菜單欄,選擇Analyze > Inspect Code
選擇檢查范圍
選擇后,點擊'OK',稍等一會就會生成掃描結果:
左側是問題分類,選中一個問題條目,則右側會展示具體的問題代碼,這樣就可以很方便的進行問題排查、定位和更改了。
Android 的規則類別:
Accessibility 輔助選項,例如 ImageView 缺少 contentDescription 描述,String 編碼字符串等問題。 Compliance 合規性,違反了Google Play的要求,比如使用了過期的庫版本,性能、安全性、API等級等沒有遵循新系統的要求等。 Correctness 不夠完美的編碼,比如硬編碼、使用過時API等。 Internationalization 國際化,如直接使用漢字,沒有使用資源引用等。 Interoperability 互操作性,比如和Kotln的交互等。 Performance 性能,例如:靜態引用,循環引用等。 Security 安全性,例如沒有使用 HTTPS 連接 Gradle,AndroidManifest 中的權限問題等。 Usability 易用性,有更好的替換的,例如缺少某些倍數的切圖,排版、圖標格式建議.png格式等等。lint 配置配置 lint 文件我們可以在 lint.xml 文件中進行 lint 配置。我們可以手動創建該文件,并放置在 Android 項目的根目錄下。
lint.xml 文件由封閉的 父標記組成,此標記包含一個或多個 子元素。lint 會為每個 定義唯一的 id 屬性值。
<?xml version='1.0' encoding='UTF-8'?><lint><!-- list of issues to configure --></lint>
我們可以通過在 標記中設置嚴重性級別屬性來更改某個問題的嚴重性級別或對該問題停用 lint 檢查。
下面來看一個示例:
<?xml version='1.0' encoding='UTF-8'?><lint> <!-- Disable the given check in this project --> <issue 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 severity='error' /></lint>禁用某個文件或方法進行 lint 檢查
如果我們在 Android 項目中想對某個類或方法禁用 lint 檢查,可以請向該代碼添加 @SuppressLint 注解。
以下示例展示了如何對 onCreate 方法中的 NewApi 問題停用 lint 檢查。lint 工具會繼續檢查該類的其他方法中的 NewApi 問題。
@SuppressLint('NewApi')@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
以下示例展示了如何對 FeedProvider 類中的 ParserError 問題停用 lint 檢查:
@SuppressLint('ParserError')public class FeedProvider extends ContentProvider {
要禁止 lint 檢查文件中的所有問題,請使用 all 關鍵字,如下所示:
@SuppressLint('all')xml 文件的 lint 檢測配置
我們可以使用 tools:ignore 屬性對 XML 文件的特定部分停用 lint 檢查。在 lint.xml 文件中添加以下命名空間值,以便 lint 工具能夠識別該屬性:
namespace xmlns:tools='http://schemas.android.com/tools'
以下示例展示了如何對 XML 布局文件的 元素中的 UnusedResources 問題停用 lint 檢查。如果某個父元素聲明了 ignore 屬性,則該元素的子元素會繼承此屬性。在本示例中,也會對 子元素停用 lint 檢查。
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools' tools:ignore='UnusedResources' > <TextViewandroid:text='@string/auto_update_prompt' /></LinearLayout>
要禁止檢查多個問題,請使用以英文逗號分隔的字符串列出要禁止檢查的問題。例如:
tools:ignore='NewApi,StringFormatInvalid'
要禁止 lint 檢查 XML 元素中的所有問題,請使用 all 關鍵字,如下所示:
tools:ignore='all'通過 Gradle 配置 lint 選項
通過 Android Plugin for Gradle,我們可以使用模塊級 build.gradle 文件中的 lintOptions {} 代碼塊配置某些 lint 選項,例如要運行或忽略哪些檢查。
例如:
android { ... lintOptions { // Turns off checks for the issue IDs you specify. disable ’TypographyFractions’,’TypographyQuotes’ // Turns on checks for the issue IDs you specify. These checks are in // addition to the default lint checks. enable ’RtlHardcoded’,’RtlCompat’, ’RtlEnabled’ // To enable checks for only a subset of issue IDs and ignore all others, // list the issue IDs with the ’check’ property instead. This property overrides // any issue IDs you enable or disable using the properties above. check ’NewApi’, ’InlinedApi’ // If set to true, turns off analysis progress reporting by lint. quiet true // if set to true (default), stops the build if errors are found. abortOnError false // if true, only report errors. ignoreWarnings true }}...在 Android Studio 中修改 lint 配置文件
我們可以很方便的在 Android Studio 中修改 lint 檢查時的配置。
Android Studio 附帶了許多 lint 及其他檢查配置文件,這些配置文件可通過 Android 更新進行更新。我們可以原封不動地使用這些配置文件,也可以修改它們的名稱、說明、嚴重級別和范圍。當然,還可以激活和禁用整組的配置文件或一組配置文件中的個別配置文件。
依次選擇 Analyze > Inspect Code,在 Specify Scope 對話框的 Inspection Profile 下,點擊 More。
此時將顯示 Inspections 對話框,其中列出了支持的檢查及其說明:
以上就是詳解Android Lint的原理及其使用的詳細內容,更多關于Android Lint的原理的資料請關注好吧啦網其它相關文章!
相關文章:
