配置 Android 項(xiàng)目——靜態(tài)代碼分析工具
說明:由于翻譯水平有限,可能會(huì)存在一些不恰當(dāng)?shù)牡胤?,歡迎指出,我會(huì)馬上改正。
- 原文地址:Static Code Analyses Tools
- 原文作者:Dmytro Danylyk
- 譯者 / 校對(duì)者:bincn
本文是配置 Android 項(xiàng)目系列的一部分:
我們?cè)谶@篇文章中討論的一切都可以在 template 項(xiàng)目中找到
靜態(tài)代碼分析工具
靜態(tài)代碼分析工具,不要執(zhí)行就可以分析代碼。通常用于查找 bug 或者確保符合代碼準(zhǔn)則。有助于保持代碼的健壯和維護(hù)代碼質(zhì)量。
Android 上最流行的代碼分析工具是:
- Lint
- PMD
- Findbugs
我通常將靜態(tài)代碼分析腳本和相關(guān)文件保存在單獨(dú)的文件夾中。(separate folder)
Lint
Lint 工具檢查你的 Android 項(xiàng)目源文件是否存在潛在錯(cuò)誤,并針對(duì)正確性,安全性,性能,可用性,可訪問性和國(guó)際化進(jìn)行優(yōu)化改進(jìn)。
Config
創(chuàng)建 script-lint.gradle 添加 Lint 到你的 Android 項(xiàng)目。
ndroid {
lintOptions {
lintConfig file("$project.rootDir/tools/rules-lint.xml")
htmlOutput file("$project.buildDir/outputs/lint/lint.html")
warningsAsErrors true
xmlReport false
}
}
Lint 重要的選項(xiàng):
- lintConfig - lint 規(guī)則集文件的路徑,您可以在其中壓縮問題。
- htmlOutput - 生成html報(bào)告的路徑。
在你的 build.gradle 中導(dǎo)入 script-lint.gradle.
apply plugin: 'com.android.application'
apply from: "$project.rootDir/tools/script-lint.gradle"
...
Test
重建你的項(xiàng)目并且運(yùn)行命令 ./gradlew lint. 如果它發(fā)現(xiàn)一些問題,你會(huì)看到類似的輸出。
./gradlew lint
Execution failed for task ':app:lint.
Lint found errors in the project; aborting build.
Wrote HTML report to: template/app/build/outputs/lint/lint.html
當(dāng)你打開 lint.html 報(bào)告文件,你將看到如何解決問題的說明和建議列表。

如果你想忽略此問題,請(qǐng)將以下規(guī)則添加到 rules-lint.xml 文件中。
<?xml version="1.0" encoding="utf-8"?>
<lint>
<issue id="GoogleAppIndexingWarning" severity="ignore" />
</lint>
注意:還有其他方法你可以 抑制 lint 警告。 有關(guān) lint 的更多信息,請(qǐng)?jiān)L問 官方網(wǎng)站.
Findbugs
靜態(tài)代碼分析工具,用于分析 Java 字節(jié)碼并檢測(cè)各種各樣的問題。
Config
創(chuàng)建 script-findbugs.gradle 文件把 findbugs 加入到你的 Android 項(xiàng)目。
apply plugin: 'findbugs'
task findbugs(type: FindBugs) {
excludeFilter = file("$project.rootDir/tools/rules-findbugs.xml")
classes = fileTree("$project.buildDir/intermediates/classes/dev/debug/com/dd")
source = fileTree("$project.rootDir/src/main/java/com/dd/")
classpath = files()
reports {
xml.enabled = false
html.enabled = true
html.destination = "$project.buildDir/outputs/findbugs/findbugs.html"
}
}
findbugs 重要的選項(xiàng):
- excludeFilter - findbugs 規(guī)則集文件的路徑,您可以在其中壓縮問題。
- classes - 生成的類的路徑(如果你有多個(gè) flavor,path 由 flavor 名稱組成,在當(dāng)前情況下為“dev”)。
- source - 源代碼的路徑。
- html.destination - 生成 html 報(bào)告的路徑。
在你的 build.gradle 中導(dǎo)入 script-findbugs.gradle.
apply plugin: 'com.android.application'
apply from: "$project.rootDir/tools/script-findbugs.gradle"
...
Test
為了測(cè)試的目的,我們將創(chuàng)建以下方法。
// MainActivity.java
...
private void someMethod(int variable) {
switch (variable) {
case 1:
System.out.println("1");
case 2:
System.out.println("2");
}
}
...
重建你的項(xiàng)目,然后使用命令 ./gradlew findbugs 運(yùn)行 findbugs. 如果它發(fā)現(xiàn)一些問題,你會(huì)看到類似的輸出。
>./gradlew findbugs
Execution failed for task ':app:findbugs'.
FindBugs rule violations were found.
See the report at: template/app/build/outputs/findbugs/findbugs.html
當(dāng)你打開 findbugs.html 報(bào)告文件,你將看到如何解決問題的說明和建議列表。

如果你想忽略此問題,請(qǐng)將以下規(guī)則添加到 rules-findbugs.xml 文件中。
<FindBugsFilter>
<Bug pattern="SF_SWITCH_NO_DEFAULT" />
<Bug pattern="SF_SWITCH_FALLTHROUGH" />
</FindBugsFilter>
注意:還有一些其他方法可以 抑制 findbugs 警告。 有關(guān) findbugs 的更多信息,請(qǐng)?jiān)L問官方網(wǎng)站。
PMD
PMD是一個(gè)源代碼分析器。 它發(fā)現(xiàn)常見的編程缺陷,如未使用的變量,空 catch 塊,不必要的對(duì)象創(chuàng)建等等。
Config
創(chuàng)建 script-pmd.gradle 文件把 pmd 添加到你的 Android 項(xiàng)目中。
apply plugin: 'pmd'
task pmd(type: Pmd) {
ruleSetFiles = files("$project.rootDir/tools/rules-pmd.xml")
source = fileTree('src/main/java/')
reports {
xml.enabled = false
html.enabled = true
html.destination = "$project.buildDir/outputs/pmd/pmd.html"
}
}
pmd 重要的選項(xiàng):
- ruleSetFiles - pmd 規(guī)則集文件的路徑,你可以在其中抑制問題并定義要跟蹤哪些問題。
- source - 源代碼路徑
- html.destination?—?生成 html 報(bào)告的路徑
在你的 build.gradle 中導(dǎo)入 script-pmd.gradle
apply plugin: 'com.android.application'
apply from: "$project.rootDir/tools/script-pmd.gradle"
...
Test
為了測(cè)試目的,我們將創(chuàng)建以下方法。
// MainActivity.java
...
private void someMethod(int a, int b, int c, int d) {
if (a > b) {
if (b > c) {
if (c > d) {
if (d > a) {
// some logic
}
}
}
}
}
...
重建你的項(xiàng)目,然后使用命令 ./gradlew pmd. 如果發(fā)現(xiàn)某些問題,你將看到類似的輸出。
>./gradlew pmd
Execution failed for task ':app:pmd.
7 PMD rule violations were found.
See the report at: template/app/build/outputs/pmd/pmd.html
當(dāng)你打開 pmd.xml 報(bào)告文件時(shí),你將看到如何解決問題的說明和建議列表。

如果你想忽略這個(gè)問題,請(qǐng)將以下規(guī)則添加到 rules-pmd.xml 文件中。
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<rule ref="rulesets/java/basic.xml" />
<rule ref="rulesets/java/braces.xml" />
<rule ref="rulesets/java/strings.xml" />
<rule ref="rulesets/java/design.xml" >
<exclude name="AvoidDeeplyNestedIfStmts"/>
</rule>
<rule ref="rulesets/java/unusedcode.xml" />
</ruleset>
注意:還有其他方法可以 抑制 pmd 警告。 有關(guān) pmd 的更多信息可以在 官方網(wǎng)站 上查看。
譯文