小酌雞湯
青春須早為躬它,豈能長少年肪康。
本文來源《Android 性能優(yōu)化 全家桶》
為什么通過 lint 檢查改進代碼宜岛?
?除了通過構(gòu)建測試來確保您的應(yīng)用符合其功能要求之外竣稽,您務(wù)必還要通過 lint 運行您的代碼來確保代碼不存在結(jié)構(gòu)問題囱怕。結(jié)構(gòu)不合理的代碼會影響 Android 應(yīng)用的可靠性和效率,并使您的代碼更難以維護毫别,而 lint 工具有助于找到這些代碼娃弓。
?例如,如果 XML 資源文件包含未使用的命名空間岛宦,則不僅占用空間台丛,還會導(dǎo)致不必要的處理。其他結(jié)構(gòu)問題(如使用目標 API 版本不支持的已棄用的元素或 API 調(diào)用)可能會導(dǎo)致代碼無法正常運行砾肺。 lint 可幫助您解決這些問題挽霉。
lint 工具如何處理應(yīng)用源文件?
- 應(yīng)用源文件:源文件包含組成 Android 項目的文件变汪,包括 Java侠坎、Kotlin 和 XML 文件、圖標以及 ProGuard 配置文件裙盾。
- lint.xml 文件:一個配置文件实胸,可用于指定要排除的任何 lint 檢查以及自定義問題嚴重級別他嫡。
- lint 工具:一個靜態(tài)代碼掃描工具,您可以從命令行或在 Android Studio 中對 Android 項目運行該工具
- lint 檢查結(jié)果:您可以在控制臺或 Android Studio 的 Inspection Results 窗口中查看 lint 檢查結(jié)果庐完。
現(xiàn)在钢属,就一起實操體驗 Lint ~
(1)lint實操環(huán)境(可選項,用自己的環(huán)境和代碼也一樣)
- SamplePop環(huán)境如下:
?Android Studio 4.0
?Gradle version 6.1.1
?Android API version 30
(2)從命令行運行 lint
//windows
gradlew lint
//Linux 或 Mac
./gradlew lint
//如果您只想對某個特定的構(gòu)建變體運行 lint 任務(wù)门躯,則在大寫變體名稱并在其前面加上 lint 前綴
gradlew lintDebug
//會有如下輸出:
> Task :lint:lint
Ran lint on variant debug: 9 issues found
Ran lint on variant release: 9 issues found
Wrote HTML report to file:///F:/Git/Blog/SamplePop/lint/build/reports/lint-results.html
Wrote XML report to file:///F:/Git/Blog/SamplePop/lint/build/reports/lint-results.xml
?進入文件夾淆党,找到lint-results.html,雙擊這個文件
常見問題可以分為如下幾個大類:
(1)Accessibility 輔助選項生音,比如ImageView的contentDescription往往建議在屬性中定義等宁否。
(2)Compliance 合規(guī)性窒升,違反了Google Play的要求缀遍,比如使用了過期的庫版本,性能饱须、安全性域醇、API等級等沒有遵循新系統(tǒng)的要求等。
(3)Correctness 不夠完美的編碼蓉媳,比如硬編碼譬挚、使用過時API等。
(4)Internationalization 國際化酪呻,直接使用漢字减宣,沒有使用資源引用等
(5)Interoperability 互操作性,比如和Kotln的交互等玩荠。
(6)Performanc 對性能有影響的編碼漆腌,比如:靜態(tài)引用,循環(huán)引用等
(7)Security 不安全的編碼阶冈,比如在 WebView 中允許使用 JavaScriptInterface等
(8)Usability 可用的闷尿,有更好的替換的 比如排版、圖標格式建議.png格式等
(3)使用獨立工具運行 lint(不實用女坑,忽略即可)
//工具路徑:android_sdk/tools/
//要對項目目錄中的文件列表運行 lint
lint [flags] <project directory>
//例如:
lint --check MissingPrefix myproject
//查看幫助
lint --help
(4)Lint配置
您可以配置不同級別的 Lint 檢查:
- 全局(整個項目)
- 項目模塊
- 生產(chǎn)模塊
- 測試模塊
- 打開的文件
- 類層次結(jié)構(gòu)
- 版本控制系統(tǒng) (VCS) 范圍
(4.1)Android Studio 中配置 Lint(內(nèi)置的 Lint)
- 在AS代碼編輯器中的彈出文本查看填具。lint 發(fā)現(xiàn)問題后,會用黃色突出顯示有問題的代碼匆骗,而對于更嚴重的問題劳景,則會在代碼下面添加紅色下劃線。
- 依次點擊 Analyze > Inspect Code 后碉就,在 lint Inspection Results 窗口中查看盟广。
(4.2)配置 lint.xml 文件
//lint.xml文件建議放置在 Android 項目的根目錄下,build.gradle配置如下:
android {
lintOptions {
lintConfig file("lint.xml")
}
}
//lint.xml示例
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<!-- Disable the given check in this project -->
<issue id="IconMissingDensityFolder" severity="ignore" />
<!-- 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>
(4.3)配置 Java铝噩、Kotlin 和 XML 源文件的 lint 檢查
//1 配置 Java 或 Kotlin 的 lint 檢查
//1.1 停用 lint 檢查衡蚂,請向該代碼添加 @SuppressLint 注釋
@SuppressLint("NewApi")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
//1.2 對 FeedProvider 類中的 ParserError 問題關(guān)閉 lint 檢查
@SuppressLint("ParserError")
class FeedProvider : ContentProvider() {
//1.3 禁止 lint 檢查文件中的所有問題
@SuppressLint("all")
//2 配置 XML 的 lint 檢查
//2.1 tools:ignore 屬性對 XML 文件的特定部分停用 lint 檢查
//布局文件的 <LinearLayout> 元素中的 UnusedResources 問題關(guān)閉 lint 檢查
<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>
//2.2 禁止檢查多個問題窿克,請使用以逗號分隔的字符串列出要禁止檢查的問題
tools:ignore="NewApi,StringFormatInvalid"
//2.3 要禁止 lint 檢查 XML 元素中的所有問題,請使用 all 關(guān)鍵字
tools:ignore="all"
(4.4)通過 Gradle 配置 lint 選項
//build.gradle配置如下:
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
}
}
(5)創(chuàng)建警告基準
?您可以為項目的當前警告集創(chuàng)建快照毛甲,然后將該快照用作將來運行檢查的基準年叮,以便只報告新問題。 有了基準快照玻募,您便可開始使用 lint 讓構(gòu)建失敗只损,而不必先返回并解決所有現(xiàn)有問題。
?個人感覺尤其是對多人的項目協(xié)同時七咧,此基準特別有用跃惫。
//要創(chuàng)建基準快照,請修改項目的 build.gradle 文件艾栋,如下所示:
android {
lintOptions {
baseline file("lint-baseline.xml")
}
}
//自定義基準:如果要將某些問題類型(而不是全部)添加到基準
android {
lintOptions {
check 'NewApi', 'HandlerLeak'
baseline file("lint-baseline.xml")
}
}
?首次添加此代碼行時爆存,系統(tǒng)會創(chuàng)建 lint-baseline.xml 文件以建立基準。此后蝗砾,lint 工具僅讀取該文件以確定基準先较。如果要創(chuàng)建新基準,請手動刪除該文件并再次運行 lint 以重新創(chuàng)建它悼粮。
?實行基準時闲勺,您會收到一條信息性警告,告知您一個或多個問題已被過濾掉扣猫,因為它們已在基準中列出菜循。之所以發(fā)出這條警告,是為了幫您記住您已配置基準申尤,因為理想情況下癌幕,您希望在某一時刻解決所有問題掂之。
(6)手動運行檢查
?您可以通過依次選擇 Analyze > Inspect Code械媒,手動運行配置的 lint 及其他 IDE 檢查政溃。檢查結(jié)果將顯示在 Inspection Results 窗口中航唆。
?追求細節(jié)請查閱文章末尾的谷歌官網(wǎng)(最全最詳細)且改。
趕緊對自己的項目代碼實操開始吧~ 會有驚喜~
小編的擴展鏈接
參考鏈接
姿容清麗厭奢華隔嫡,淡淡平平不自夸
?
舉手之勞饰恕,贊有余香林艘!???比心??
?