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

欢迎关注微信号「极光开发者」,不按期赠书活动!

相关文章
相关标签/搜索