小酌雞湯
青春須早為,豈能長少年。
本文來源《Android 性能優(yōu)化 全家桶》
為什么通過 lint 檢查改進(jìn)代碼?
?除了通過構(gòu)建測(cè)試來確保您的應(yīng)用符合其功能要求之外,您務(wù)必還要通過 lint 運(yùn)行您的代碼來確保代碼不存在結(jié)構(gòu)問題。結(jié)構(gòu)不合理的代碼會(huì)影響 Android 應(yīng)用的可靠性和效率,并使您的代碼更難以維護(hù),而 lint 工具有助于找到這些代碼。
?例如,如果 XML 資源文件包含未使用的命名空間,則不僅占用空間,還會(huì)導(dǎo)致不必要的處理。其他結(jié)構(gòu)問題(如使用目標(biāo) API 版本不支持的已棄用的元素或 API 調(diào)用)可能會(huì)導(dǎo)致代碼無法正常運(yùn)行。 lint 可幫助您解決這些問題。
lint 工具如何處理應(yīng)用源文件?
- 應(yīng)用源文件:源文件包含組成 Android 項(xiàng)目的文件,包括 Java、Kotlin 和 XML 文件、圖標(biāo)以及 ProGuard 配置文件。
- lint.xml 文件:一個(gè)配置文件,可用于指定要排除的任何 lint 檢查以及自定義問題嚴(yán)重級(jí)別。
- lint 工具:一個(gè)靜態(tài)代碼掃描工具,您可以從命令行或在 Android Studio 中對(duì) Android 項(xiàng)目運(yùn)行該工具
- lint 檢查結(jié)果:您可以在控制臺(tái)或 Android Studio 的 Inspection Results 窗口中查看 lint 檢查結(jié)果。
現(xiàn)在,就一起實(shí)操體驗(yàn) Lint ~
(1)lint實(shí)操環(huán)境(可選項(xiàng),用自己的環(huán)境和代碼也一樣)
- SamplePop環(huán)境如下:
?Android Studio 4.0
?Gradle version 6.1.1
?Android API version 30
(2)從命令行運(yùn)行 lint
//windows
gradlew lint
//Linux 或 Mac
./gradlew lint
//如果您只想對(duì)某個(gè)特定的構(gòu)建變體運(yùn)行 lint 任務(wù),則在大寫變體名稱并在其前面加上 lint 前綴
gradlew lintDebug
//會(huì)有如下輸出:
> 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
?進(jìn)入文件夾,找到lint-results.html,雙擊這個(gè)文件
常見問題可以分為如下幾個(gè)大類:
(1)Accessibility 輔助選項(xiàng),比如ImageView的contentDescription往往建議在屬性中定義等。
(2)Compliance 合規(guī)性,違反了Google Play的要求,比如使用了過期的庫版本,性能、安全性、API等級(jí)等沒有遵循新系統(tǒng)的要求等。
(3)Correctness 不夠完美的編碼,比如硬編碼、使用過時(shí)API等。
(4)Internationalization 國際化,直接使用漢字,沒有使用資源引用等
(5)Interoperability 互操作性,比如和Kotln的交互等。
(6)Performanc 對(duì)性能有影響的編碼,比如:靜態(tài)引用,循環(huán)引用等
(7)Security 不安全的編碼,比如在 WebView 中允許使用 JavaScriptInterface等
(8)Usability 可用的,有更好的替換的 比如排版、圖標(biāo)格式建議.png格式等
(3)使用獨(dú)立工具運(yùn)行 lint(不實(shí)用,忽略即可)
//工具路徑:android_sdk/tools/
//要對(duì)項(xiàng)目目錄中的文件列表運(yùn)行 lint
lint [flags] <project directory>
//例如:
lint --check MissingPrefix myproject
//查看幫助
lint --help
(4)Lint配置
您可以配置不同級(jí)別的 Lint 檢查:
- 全局(整個(gè)項(xiàng)目)
- 項(xiàng)目模塊
- 生產(chǎn)模塊
- 測(cè)試模塊
- 打開的文件
- 類層次結(jié)構(gòu)
- 版本控制系統(tǒng) (VCS) 范圍
(4.1)Android Studio 中配置 Lint(內(nèi)置的 Lint)
- 在AS代碼編輯器中的彈出文本查看。lint 發(fā)現(xiàn)問題后,會(huì)用黃色突出顯示有問題的代碼,而對(duì)于更嚴(yán)重的問題,則會(huì)在代碼下面添加紅色下劃線。
- 依次點(diǎn)擊 Analyze > Inspect Code 后,在 lint Inspection Results 窗口中查看。
(4.2)配置 lint.xml 文件
//lint.xml文件建議放置在 Android 項(xiàng)目的根目錄下,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 檢查,請(qǐng)向該代碼添加 @SuppressLint 注釋
@SuppressLint("NewApi")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
//1.2 對(duì) FeedProvider 類中的 ParserError 問題關(guān)閉 lint 檢查
@SuppressLint("ParserError")
class FeedProvider : ContentProvider() {
//1.3 禁止 lint 檢查文件中的所有問題
@SuppressLint("all")
//2 配置 XML 的 lint 檢查
//2.1 tools:ignore 屬性對(duì) 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 禁止檢查多個(gè)問題,請(qǐng)使用以逗號(hào)分隔的字符串列出要禁止檢查的問題
tools:ignore="NewApi,StringFormatInvalid"
//2.3 要禁止 lint 檢查 XML 元素中的所有問題,請(qǐng)使用 all 關(guān)鍵字
tools:ignore="all"
(4.4)通過 Gradle 配置 lint 選項(xiàng)
//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)建警告基準(zhǔn)
?您可以為項(xiàng)目的當(dāng)前警告集創(chuàng)建快照,然后將該快照用作將來運(yùn)行檢查的基準(zhǔn),以便只報(bào)告新問題。 有了基準(zhǔn)快照,您便可開始使用 lint 讓構(gòu)建失敗,而不必先返回并解決所有現(xiàn)有問題。
?個(gè)人感覺尤其是對(duì)多人的項(xiàng)目協(xié)同時(shí),此基準(zhǔn)特別有用。
//要?jiǎng)?chuàng)建基準(zhǔn)快照,請(qǐng)修改項(xiàng)目的 build.gradle 文件,如下所示:
android {
lintOptions {
baseline file("lint-baseline.xml")
}
}
//自定義基準(zhǔn):如果要將某些問題類型(而不是全部)添加到基準(zhǔn)
android {
lintOptions {
check 'NewApi', 'HandlerLeak'
baseline file("lint-baseline.xml")
}
}
?首次添加此代碼行時(shí),系統(tǒng)會(huì)創(chuàng)建 lint-baseline.xml 文件以建立基準(zhǔn)。此后,lint 工具僅讀取該文件以確定基準(zhǔn)。如果要?jiǎng)?chuàng)建新基準(zhǔn),請(qǐng)手動(dòng)刪除該文件并再次運(yùn)行 lint 以重新創(chuàng)建它。
?實(shí)行基準(zhǔn)時(shí),您會(huì)收到一條信息性警告,告知您一個(gè)或多個(gè)問題已被過濾掉,因?yàn)樗鼈円言诨鶞?zhǔn)中列出。之所以發(fā)出這條警告,是為了幫您記住您已配置基準(zhǔn),因?yàn)槔硐肭闆r下,您希望在某一時(shí)刻解決所有問題。
(6)手動(dòng)運(yùn)行檢查
?您可以通過依次選擇 Analyze > Inspect Code,手動(dòng)運(yùn)行配置的 lint 及其他 IDE 檢查。檢查結(jié)果將顯示在 Inspection Results 窗口中。
?追求細(xì)節(jié)請(qǐng)查閱文章末尾的谷歌官網(wǎng)(最全最詳細(xì))。
趕緊對(duì)自己的項(xiàng)目代碼實(shí)操開始吧~ 會(huì)有驚喜~
小編的擴(kuò)展鏈接
參考鏈接
- 谷歌官網(wǎng) -> 這是應(yīng)該讀的第一篇文章
- 谷歌官網(wǎng) -> LintOptions配置項(xiàng)參考
- 優(yōu)秀文章 -> 對(duì)詳細(xì)字段分析最徹底的文章
姿容清麗厭奢華,淡淡平平不自夸
?
舉手之勞,贊有余香!???比心??
?