Android代碼檢查規(guī)則Lint的自定義與應(yīng)用詳解
前言:
在日常的代碼開發(fā)中,此處相信每個開發(fā)人員對代碼質(zhì)量都是高要求,有自己的一套代碼規(guī)范,但是我們不是單獨作戰(zhàn),往往大家都是團隊作戰(zhàn),人是最大的變量,各人各異,如何保證團隊的代碼質(zhì)量和代碼規(guī)范呢?靠開發(fā)者自覺嗎?也許有的團隊有嚴格的CR機制,在MR階段會進行CR,CR不通過的MR是不允許合入的,但是這樣會使Reviewer花費較多的時間去校驗,那么這時候我們就需要在編碼過程中提供一種代碼檢測機制。
例如:期望表現(xiàn)的效果就是在編碼時可以展示異常檢測的方法,高亮或者標紅,當鼠標懸停在高亮的代碼上時,會提供問題的描述和解決方法。需要這種效果,就需要自定義lint了。

什么是Lint
在Android Studio中提供的代碼掃描工具Lint,可在無需實際執(zhí)行該應(yīng)用,也不必編寫測試用例的情況下幫助開發(fā)者發(fā)現(xiàn)代碼質(zhì)量問題和提出一些改進建議。
Lint工具可檢查您的 Android 項目源文件是否包含潛在錯誤,以及在正確性、安全性、性能、易用性、便利性和國際化方面是否需要優(yōu)化改進。在使用 Android Studio 時,配置的 Lint 和 IDE 檢查會在您每次構(gòu)建應(yīng)用時運行。不過,您可以手動運行檢查或從命令行運行 Lint。
android studio內(nèi)置了較多的lint規(guī)則,但內(nèi)置的lint規(guī)則無法滿足直觀的適合我們時,就需要我們自定義lint了。

自定義Lint流程:
1. 新創(chuàng)建module,Module類型選擇Java or Kotlin Library, 暫時命名lint_tools
2. 在build.gradle中引入lint的依賴
dependencies {
compileOnly 'com.android.tools.lint:lint-api:27.2.2'
compileOnly 'com.android.tools.lint:lint-checks:27.2.2'
}在moudle中依賴了lint-api和lint-checks,其中l(wèi)int-api就是lint相關(guān)的api,lint-checks就是android studio里自定義的一些lint規(guī)則,我們自定義lint可以參考lint-checks里面的寫法。
3. 本地創(chuàng)建個資源id命名檢查規(guī)則,用來規(guī)范項目中的id統(tǒng)一命名
創(chuàng)建ViewIdDetector,直接繼承LayoutDetector(也可以繼承ResourceXmlDetector 或者 繼承Detector實現(xiàn)的接口是XmlScanner,方式多樣)
import com.android.SdkConstants
import com.android.tools.lint.detector.api.*
import com.android.tools.lint.detector.api.Category.Companion.CORRECTNESS
import com.android.tools.lint.detector.api.Scope.Companion.RESOURCE_FILE_SCOPE
import org.w3c.dom.Element
?
class ViewIdDetector : LayoutDetector() {
override fun getApplicableElements(): Collection<String>? {
return listOf(
SdkConstants.TEXT_VIEW,
SdkConstants.IMAGE_VIEW,
SdkConstants.BUTTON
)
}
?
override fun visitElement(context: XmlContext, element: Element) {
if (!element.hasAttributeNS(SdkConstants.ANDROID_URI, SdkConstants.ATTR_ID)) {
return
}
val attr = element.getAttributeNodeNS(SdkConstants.ANDROID_URI, SdkConstants.ATTR_ID)
val value = attr.value
if (value.startsWith(SdkConstants.NEW_ID_PREFIX)) {
val idValue = value.substring(SdkConstants.NEW_ID_PREFIX.length)
var matchRule = true
var expMsg = ""
when (element.tagName) {
SdkConstants.TEXT_VIEW -> {
expMsg = "tv"
matchRule = idValue.startsWith(expMsg)
}
SdkConstants.IMAGE_VIEW -> {
expMsg = "iv"
matchRule = idValue.startsWith(expMsg)
}
SdkConstants.BUTTON -> {
expMsg = "btn"
matchRule = idValue.startsWith(expMsg)
}
}
if (!matchRule) {
context.report(
ISSUE,
attr,
context.getLocation(attr),
"ViewIdName建議使用view的縮寫_xxx; ${element.tagName} 建議使用 `${expMsg}_xxx`"
)
}
}
}
?
companion object {
val ISSUE: Issue = Issue.create(
"ViewIdCheck",
"ViewId命名不規(guī)范",
"ViewIdName建議使用 view的縮寫加上_xxx,例如tv_xxx, iv_xxx",
CORRECTNESS,
5, Severity.ERROR,
Implementation(
ViewIdDetector::class.java,
RESOURCE_FILE_SCOPE
)
)
}
}自定義Detector可以實現(xiàn)一個或多個Scanner接口,選擇實現(xiàn)哪種接口取決于你想要的掃描范圍。
Lint API 中內(nèi)置了很多 Scanner:
| Scanner 類型 | Desc |
|---|---|
| UastScanner | 掃描 Java、Kotlin 源文件 |
| XmlScanner | 掃描 XML 文件 |
| ResourceFolderScanner | 掃描資源文件夾 |
| ClassScanner | 掃描 Class 文件 |
| BinaryResourceScanner | 掃描二進制資源文件 |
| GradleScanner | 掃描Gradle腳本 |
4. 實現(xiàn)IssueRegistry并添加對應(yīng)的自定義Issue:
class IMockIssueRegistry: IssueRegistry() {
override val issues: List<Issue>
get() = listOf(
ViewIdDetector.ISSUE
)
?
}5. 在module(lint_tools)中對應(yīng)的build.gradle中配置如下信息:
jar {
manifest {
attributes("Lint-registry-v2": "com.imock.lint.IMockIssueRegistry")
}
}
6. 在需要進行l(wèi)int檢查的module中或者app目錄現(xiàn)的build.gradle中引用對應(yīng)的lint_tools即可使用。
dependencies {
lintChecks project(path: ':lint-tools')
}至此你可以試著自己自定義Lint了,相關(guān)語法api都可參考lint-checks中提供的Detector實現(xiàn),來實現(xiàn)自己的Lint檢查規(guī)則。
拓展一下:
1. 針對Issue.create參數(shù)了解一下:
companion object {
/**
* Creates a new issue. The description strings can use some simple markup;
* see the [TextFormat.RAW] documentation
* for details.
*
* @param id the fixed id of the issue
* @param briefDescription short summary (typically 5-6 words or less), typically
* describing the **problem** rather than the **fix**
* (e.g. "Missing minSdkVersion")
* @param explanation a full explanation of the issue, with suggestions for
* how to fix it
* @param category the associated category, if any
* @param priority the priority, a number from 1 to 10 with 10 being most
* important/severe
* @param severity the default severity of the issue
* @param implementation the default implementation for this issue
* @return a new [Issue]
*/
@JvmStatic
fun create(
id: String,
briefDescription: String,
explanation: String,
category: Category,
priority: Int,
severity: Severity,
implementation: Implementation
): Issue {
val platforms = computePlatforms(null, implementation)
return Issue(
id, briefDescription, explanation, category, priority,
severity, platforms, null, implementation
)
}
}- 參數(shù)id 唯一的id,簡要表面當前提示的問題。
- 參數(shù)briefDescription 簡單描述當前問題
- 參數(shù)explanation 詳細解釋當前問題和修復(fù)建議
- 參數(shù)category 問題類別
- 參數(shù)priority 優(yōu)先級,從1到10,10最重要
- 參數(shù)Severity 嚴重程度:FATAL(奔潰), ERROR(錯誤), WARNING(警告),INFORMATIONAL(信息性),IGNORE(可忽略)
- 參數(shù)Implementation Issue和哪個Detector綁定,以及聲明檢查的范圍。Scope有如下選擇范圍:RESOURCE_FILE(資源文件),BINARY_RESOURCE_FILE(二進制資源文件),RESOURCE_FOLDER(資源文件夾),ALL_RESOURCE_FILES(所有資源文件),JAVA_FILE(Java文件), ALL_JAVA_FILES(所有Java文件),CLASS_FILE(class文件), ALL_CLASS_FILES(所有class文件),MANIFEST(配置清單文件), PROGUARD_FILE(混淆文件),JAVA_LIBRARIES(Java庫), GRADLE_FILE(Gradle文件),PROPERTY_FILE(屬性文件),TEST_SOURCES(測試資源),OTHER(其他);
到此這篇關(guān)于Android代碼檢查規(guī)則Lint的自定義與應(yīng)用詳解的文章就介紹到這了,更多相關(guān)Android代碼檢查規(guī)則Lint內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android SharedPreferences實現(xiàn)保存登錄數(shù)據(jù)功能
這篇文章主要為大家詳細介紹了Android SharedPreferences實現(xiàn)保存登錄數(shù)據(jù)功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05
Android組合式自定義控件實現(xiàn)購物車加減商品操作
這篇文章主要介紹了Android組合式自定義控件實現(xiàn)購物車加減商品操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11
Android Dialog仿ios9中UIAlertController控件
這篇文章主要為大家詳細介紹了Android Dialog仿ios9中UIAlertController控件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06
Android ActivityManagerService啟動流程詳解
這篇文章主要介紹了Android ActivityManagerService啟動流程,AMS,即ActivityManagerService,是安卓java framework的一個服務(wù),運行在system_server進程。此服務(wù)十分重要,因為它管理著安卓的四大組件,是安卓APP開發(fā)者最常接觸到的一個服務(wù)2023-02-02
Android自定義View實現(xiàn)多邊形統(tǒng)計圖示例代碼
這篇文章主要給大家介紹了關(guān)于Android自定義View如何實現(xiàn)多邊形統(tǒng)計圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
詳解RxJava2 Retrofit2 網(wǎng)絡(luò)框架簡潔輕便封裝
本篇文章主要介紹了詳解RxJava2 Retrofit2 網(wǎng)絡(luò)框架簡潔輕便封裝,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
Flutter交互并使用小工具管理其狀態(tài)widget的state詳解
這篇文章主要為大家介紹了Flutter交互并使用小工具管理其狀態(tài)widget的state詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12

