Android 靜態代碼分析工具

簡評: 做者在文中提到的三個靜態代碼分析工具不是互相替代的關係,各有各的側重點,若是有須要徹底能夠同時使用。

靜態代碼分析是指無需運行被測代碼,僅經過分析或檢查源程序的語法、結構、過程、接口等來檢查程序的正確性,找出代碼隱藏的錯誤和缺陷,如參數不匹配,有歧義的嵌套語句,錯誤的遞歸,非法計算,可能出現的空指針引用等等。html

對於 Android 來講用得最多的三個靜態代碼分析工具當屬:java

  • Lint
  • PMD
  • Findbugs

Lint

Lint 是 Google 提供給 Android 開發者的靜態代碼分析工具,能幫助開發者優化代碼和找到潛在的 bug。

配置:android

在項目中建立 script-lint.gradle 文件:git

android {
    lintOptions {
        lintConfig file("$project.rootDir/tools/rules-lint.xml")
        htmlOutput file("$project.buildDir/outputs/lint/lint.html")
        warningsAsErrors true
        xmlReport false
    }
}

其中兩個重要的屬性:github

  • lintConfig : lint 規則文件的路徑。
  • htmlOutput : html 報告生成的路徑。

以後在 build.gradle 文件中引用:編程

apply plugin: 'com.android.application'
apply from: "$project.rootDir/tools/script-lint.gradle"

...

測試:微信

運行 ./gradlew lint 命令,就能夠在上面設置的 htmlOutput 路徑下找到 lint.html 文件,打開後就會看到相似下面的提示:
提示app

Findbugs

Findbugs 分析的是 Java 字節碼,能識別出上百種的潛在錯誤。

配置:工具

建立 script-findbugs.gradle文件:測試

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"
    }
}

屬性:

  • excludeFilter:Findbugs 規則文件的路徑。
  • classes:classes 文件的路徑。
  • source:源碼的路徑。
  • html.destination:html 報告的生成地址。

一樣須要在 build.gradle 中進行引用:

apply plugin: 'com.android.application'
apply from: "$project.rootDir/tools/script-findbugs.gradle"

...

測試:

下面的這段代碼:

// MainActivity.java

...

private void someMethod(int variable) {
   switch (variable) {
       case 1:
           System.out.println("1");
       case 2:
           System.out.println("2");
   }
}

...

運行 ./gradlew findbugs 命令,findbugs.html 中就會生成檢測結果,相似於下面這樣:
檢測結果

PMD

PMD 能發現常見的編程缺陷,好比未使用的變量、空的 catch 塊、沒必要要的對象等等。

配置:

建立 script-pmd.gradle文件:

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"
    }
}

屬性:

  • ruleSetFiles:規則文件路徑。
  • source:源碼路徑。
  • html.destination:檢測結果的 html 文件所在路徑。

一樣,再在 build.gradle 文件中引用:

apply plugin: 'com.android.application'
apply from: "$project.rootDir/tools/script-pmd.gradle"

...

測試:

// 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
               }
           }
       }
   }
}

...

執行 ./gradlew pmd 命令,在 html.destination 設置的路徑下找到 pmd.html,就可以看到以下的檢測結果:
檢測結果

上面提到的全部文件,和配置項均可以在做者的這個項目中找到:dmytrodanylyk/template


原文連接: Configuring Android Project — Static Code Analysis Tools
推薦閱讀聊聊 Android StateListAnimator

歡迎關注微信號「極光開發者」,不按期贈書活動!

相關文章
相關標籤/搜索