Android 静态代码分析工具

原创
2019/05/13 13:56
阅读数 132

简评: 作者在文中提到的三个静态代码分析工具不是互相替代的关系,各有各的侧重点,如果有需要完全可以同时使用。

静态代码分析是指无需运行被测代码,仅通过分析或检查源程序的语法、结构、过程、接口等来检查程序的正确性,找出代码隐藏的错误和缺陷,如参数不匹配,有歧义的嵌套语句,错误的递归,非法计算,可能出现的空指针引用等等。

对于 Android 来说用得最多的三个静态代码分析工具当属:

  • Lint
  • PMD
  • Findbugs

Lint

Lint 是 Google 提供给 Android 开发者的静态代码分析工具,能帮助开发者优化代码和找到潜在的 bug。

配置:

在项目中创建 script-lint.gradle 文件:

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

其中两个重要的属性:

  • lintConfig : lint 规则文件的路径。
  • htmlOutput : html 报告生成的路径。

之后在 build.gradle 文件中引用:

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

...

测试:

运行 ./gradlew lint 命令,就可以在上面设置的 htmlOutput 路径下找到 lint.html 文件,打开后就会看到类似下面的提示: 提示

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

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

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部