編輯:Android 開發環境
要驗證 Android 應用是否滿足功能需求,最關鍵的一點是確保程式碼沒有結構性缺陷。品質差的程式碼會拖累應用的可靠性,也讓後續維護變得棘手。舉例來說,XML 資源檔若殘留未使用的命名空間,就會白白佔用空間並帶來不必要的開銷;而像呼叫已淘汰的 API 這類問題,在裝置 API 版本不相容時,甚至可能導致執行失敗。
Android Studio 提供了一款名為 Lint 的程式碼掃描工具,能輕鬆幫你辨識並修正結構性的品質問題,完全不必先跑起應用或撰寫測試案例。工具偵測到的每個問題都會標示嚴重性與說明,讓你能快速排定處理優先順序,也可以把專案中無關緊要的項目直接忽略。由於它提供了命令列介面,整合進測試流程也相當容易。
Lint 會掃描你的 Android 專案原始碼,找出潛在 bug 並給出優化建議,你可以從命令列或 Android Studio 中執行它。

Application 原始檔:包含組成 Android 專案的所有檔案,如 Java 與 XML 檔、圖示及 ProGuard 設定檔。
Lint.xml 檔:設定檔,用來指定要忽略的問題或調整問題的嚴重性。
Lint 工具:可從命令列或 Android Studio 執行的靜態程式碼掃描工具,用來偵測影響應用品質與效能的結構性問題。強烈建議在正式發布前修掉 Lint 報出的每一項問題。
Lint 檢測結果:你可以在 Android Studio 的 Event Log(或命令列)中查看結果。Lint 工具是 Android SDK 工具(版本 16 以上)的一部分。
當你建置應用時,Android Studio 會自動執行 Lint。你也可以在 android 設定中透過 lintOptions 屬性進行調整:
android {
lintOptions {
// 設為 true 會關閉 lint 分析進度顯示
quiet true
// 若為 true,發現錯誤時會中止 gradle 建置
abortOnError false
// 若為 true,只回報錯誤
ignoreWarnings true
}
}
掃描整個專案:
lint [flags] <project directory>
只檢測特定錯誤,例如檢查 Android 命名空間前綴:
lint --check MissingPrefix myproject
想看 Lint 說明,可執行 lint --help。
以下展示 Lint 掃描 Earthquake 專案時的輸出:
$ lint Earthquake
Scanning Earthquake: ............
Scanning Earthquake (Phase 2): .......
AndroidManifest.xml:23: Warning: <uses-sdk> tag appears after <application> tag [ManifestOrder]
AndroidManifest.xml:23: Warning: <uses-sdk> tag should specify a target API level ... [UsesMinSdkAttributes]
res/layout/preferences.xml: Warning: The resource R.layout.preferences appears to be unused [UnusedResources]
res: Warning: Missing density variation folders in res: drawable-xhdpi [IconMissingDensityFolder]
0 errors, 4 warnings
預設情況下,執行掃描時會檢測 Lint 支援的所有 issue。你可以停用某個 issue,或調整它的嚴重性。設定可套用在不同層級:
1、全域(整個專案);2、每個專案模組;3、每個測試模組等。
使用 Android Studio 時,內建的 Lint 會檢測你的程式碼,你可以透過兩種方式查看警告與錯誤:
1、在程式碼編輯器中,Lint 發現問題後會以黃色高亮標示;
2、選擇 Analyze > Inspect Code,開啟 Lint Inspection Results。
設定預設檢測規則:開啟專案後,依序選擇 File > Other Settings > Default Settings,再進入 Editor > Inspections 開啟 Default Preferences;在 Profile 中選擇 Default 或 Project Default,依需求調整後按 OK。
在 Inspection Results 視窗顯示結果:開啟專案並選取要檢測的範圍,選擇 Analyze > Inspect Code,在 Specify Inspection Scope 中確認範圍後按 OK,結果便會依分類呈現在 Inspection Results 視窗。
你可以在 lint.xml 中宣告檢測參數。若手動建立,請放在專案根目錄;若在 Android Studio 中設定,檔案會自動產生並加入專案。lint.xml 以 <lint> 為父標籤,內部可包含一個或多個 <issue> 標籤:
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<!-- 待設定的 issue 清單 -->
</lint>
透過這些標籤,你可以停用某個 issue 或調整其嚴重性。想查看 Lint 支援的所有 issue,可執行 lint --list。
以下是一個 lint.xml 範例:
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<!-- 在整個專案中停用這項檢測 -->
<issue id="IconMissingDensityFolder" severity="ignore" />
<!-- 僅在指定檔案中忽略 ObsoleteLayoutParam -->
<issue id="ObsoleteLayoutParam">
<ignore path="res/layout/activation.xml" />
<ignore path="res/layout-xlarge/activation.xml" />
</issue>
<!-- 僅在指定檔案中忽略 UselessLeaf -->
<issue id="UselessLeaf">
<ignore path="res/layout/main.xml" />
</issue>
<!-- 將硬編碼字串的嚴重性提升為 error -->
<issue id="HardcodedText" severity="error" />
</lint>
Java:若想對某個類別或方法停用 Lint,可在程式碼上加 @SuppressLint 註解。以下在 onCreate 方法中關閉 NewApi 檢測,其他方法仍照常檢測:
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
關閉 ParserError:
@SuppressLint("ParserError")
public class FeedProvider extends ContentProvider {}
關閉整個檔案的所有檢測則用 @SuppressLint("all")。
XML:可使用 tools:ignore 屬性停用檢測。為了讓 Lint 能識別相關屬性,必須在 XML 根元素加入命名空間 xmlns:tools="https://schemas.android.com/tools":
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
tools:ignore="UnusedResources" >
<TextView
android:text="@string/auto_update_prompt" />
</LinearLayout>
停用多個 issue:tools:ignore="NewApi,StringFormatInvalid";停用全部:tools:ignore="all"。
Android Studio(十四):在虛擬機中運行應用
Android虛擬機能模擬一台設備並顯示在你的開發電腦上,讓你在不使用實體硬件的情況下開發、測試Android應用。虛擬機支援Android手機、Android Wear
DDMS 詳解:Dalvik Debug Monitor Service 使用指南
做 Android 開發很難不碰到 DDMS,它在除錯與診斷環節能提供相當大的幫助。以下整理它的定位、工作原理與各面板的實際用法。 DDMS 是什麼 DDMS 全名
配置 Android SDK 環境變量的兩種方法
建立 AVD 時,如果在命令列中輸入 android list targets 卻收到「android 不是內部或外部命令」的提示,問題基本可以確定:Android SD
十款最常用的 Android 開發工具推薦
用任何語言開發都離不開工具。有些是必備品,有些則是為了拉高效率。Android 開發在設計、建立、測試與發佈各個階段都有對應的工具可倚靠,以下推薦十款相當常用的。 1.