Android Studio 檢查改進代碼
前一小節(jié)我們介紹了如何查找優(yōu)質(zhì)的 Android 代碼示例,本小結我們學習如何通過 lint 來檢查我們的代碼,如何發(fā)現(xiàn)代碼中存在的問題,保證應用能高效穩(wěn)定的運行。
1. 什么是 lint
Android Studio 提供了一個名為 lint 的代碼掃描工具,可幫助我們發(fā)現(xiàn)并更正代碼結構質(zhì)量的問題,而無需實際執(zhí)行應用,也不必編寫測試用例。系統(tǒng)會報告該工具檢測到的每個問題并提供問題的描述消息和嚴重級別,以便我們可以快速確定需要優(yōu)先進行的關鍵改進。此外,我們還可以降低問題的嚴重級別以忽略與項目無關的問題,或者提高嚴重級別以突出特定問題。
lint 工具可以檢查 Android 項目源文件是否有潛在的錯誤,以及在正確性、安全性、性能、易用性、無障礙性和國際化方面是否需要優(yōu)化改進。使用 Android Studio 時,無論何時構建應用,都會運行配置的 lint 和 IDE 檢查。
下圖顯示了 lint 工具如何掃描處理文件:
-
應用源文件:源文件包含組成 Android 項目的文件,包括 Java、Kotlin 和 XML 文件、圖標以及 ProGuard 配置文件;
-
lint.xml 文件:一個配置文件,可用于指定要排除的任何 lint 檢查以及自定義問題嚴重級別;
-
lint 工具:一個靜態(tài)代碼掃描工具,我們可以從命令行或在 Android Studio 中對 Android 項目運行該工具;
-
lint 檢查結果:我們可以在控制臺或 Android Studio 的 Inspection Results 窗口中查看 lint 檢查結果。
2. lint 的配置
默認情況下,當我們運行 lint 掃描時,lint 工具會檢查它支持的所有問題。 我們也可以限制 lint 要檢查的問題,并為這些問題分配嚴重級別。例如,我們可以禁止對與項目無關的特定問題進行 lint 檢查,也可以將 lint 配置為以較低的嚴重級別報告非關鍵問題。
2.1 通過 lint 文件配置
我們可以在 lint.xml 文件中指定 lint 檢查偏好設置。如果我們是手動創(chuàng)建此文件,請將其放置在 Android 項目的根目錄下。
lint.xml 文件由封閉的 <lint>
父標記組成,此標記包含一個或多個 < issue > 子元素。lint 會為每個 < issue > 定義唯一的 id 屬性值。
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<!-- list of issues to configure -->
</lint>
Tips:我們可以通過在 issue 標記中設置嚴重級別屬性來更改某個問題的嚴重級別或?qū)υ搯栴}停用 lint 檢查。如需查看 lint 支持的問題及其對應的問題 ID 的完整列表,請運行 lint --list 命令。
以下是 lint.xml 文件示例:
<?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>
2.2 通過 Gradle 配置
通過 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
}
}
...
2.3 在源文件中配置 Lint
配置 Java 的 lint 檢查
要專門對 Android 項目中的某個類或方法停用 lint 檢查,請向該代碼添加 @SuppressLint 注釋。
以下示例展示了如何對 onCreate 方法中的 NewApi 問題關閉 lint 檢查。lint 工具會繼續(xù)檢查該類的其他方法中的 NewApi 問題。
@SuppressLint("NewApi")
@Override
public 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 布局文件的 < LinearLayout > 元素中的 UnusedResources 問題關閉 lint 檢查。如果某個父元素聲明了 ignore 屬性,則該元素的子元素會繼承此屬性。在本例中,也會對 <TextView>
子元素停用 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>
要禁止檢查多個問題,請使用以逗號分隔的字符串列出要禁止檢查的問題。例如:
tools:ignore="NewApi,StringFormatInvalid"
要禁止 lint 檢查 XML 元素中的所有問題,請使用 all 關鍵字,如下所示:
tools:ignore="all"
3. 如何運行 Lint
3.1 從命令行運行 lint
我們可以在項目的根目錄下輸入以下某個命令,使用 Gradle 封裝容器 對項目調(diào)用 lint 任務:
- 在 Windows 上:
gradlew lint
- 在 Linux 或 Mac 上:
./gradlew lint
我們會看到類似于以下內(nèi)容的輸出:
lint 工具完成其檢查后,會提供 XML 和 HTML 版 Lint 報告的路徑。然后,我們可以轉(zhuǎn)到 HTML 報告并在瀏覽器中將其打開,如下圖所示:
3.2 從 Android Studio 運行 lint
在 Android Studio 中手動運行 lint 檢查,請按如下步驟:
-
在 Android 視圖中,打開我們的項目,然后選擇要分析的項目、文件夾或文件;
-
從菜單欄中,依次選擇 Analyze > Inspect Code;
-
在 Specify Inspection Scope 對話框中,查看設置:
-
在 Inspection profile 下,保留默認配置文件 (Project Default);
-
點擊 OK 以運行檢查。
通過運行 Inspect Code 所生成的 lint 及其他 IDE 檢查結果如下圖:
-
在左側(cè)窗格樹狀視圖中,通過展開并選擇錯誤類別、類型和問題來查看檢查結果;
-
在右側(cè)窗格顯示選定錯誤類別、類型或問題的檢查報告,并提供錯誤的名稱和位置。在適用情況下,檢查報告會顯示問題概要等其他信息,以幫助我們更正問題;
-
在左側(cè)窗格樹狀視圖中,右鍵點擊某個類別、類型或問題,以顯示上下文菜單。根據(jù)上下文,我們可以執(zhí)行以下全部或部分操作:跳到源代碼、排除和包含選定項、抑制問題、修改設置、管理檢查警報和重新運行檢查。
4. 小結
本節(jié)課程我們主要學習了 Android Studio 如何通過 lint 來檢查我們的代碼。本節(jié)課程的重點如下:
- 了解如何配置 lint;
- 了解如何使用 lint 檢查代碼和更正問題。