JavaAndroid知识Android技术知识

【译文】配置 Android 项目系列3——静态代码分析工具

2017-04-20  本文已影响90人  bincn

配置 Android 项目——静态代码分析工具

说明:由于翻译水平有限,可能会存在一些不恰当的地方,欢迎指出,我会马上改正。

本文是配置 Android 项目系列的一部分:

  1. Little Things That Matter
  2. Version Name & Code
  3. Static Code Analyses Tools
  4. Continuous Integration

我们在这篇文章中讨论的一切都可以在 template 项目中找到

静态代码分析工具

静态代码分析工具,不要执行就可以分析代码。通常用于查找 bug 或者确保符合代码准则。有助于保持代码的健壮和维护代码质量。

Android 上最流行的代码分析工具是:

我通常将静态代码分析脚本和相关文件保存在单独的文件夹中。(separate folder)

Lint

Lint 工具检查你的 Android 项目源文件是否存在潜在错误,并针对正确性,安全性,性能,可用性,可访问性和国际化进行优化改进。

Config

创建 script-lint.gradle 添加 Lint 到你的 Android 项目。

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

Lint 重要的选项:

在你的 build.gradle 中导入 script-lint.gradle.

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

Test

重建你的项目并且运行命令 ./gradlew lint. 如果它发现一些问题,你会看到类似的输出。

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

当你打开 lint.html 报告文件,你将看到如何解决问题的说明和建议列表。

1.png

如果你想忽略此问题,请将以下规则添加到 rules-lint.xml 文件中。

<?xml version="1.0" encoding="utf-8"?>
<lint>
    <issue id="GoogleAppIndexingWarning" severity="ignore" />
</lint>

注意:还有其他方法你可以 抑制 lint 警告。 有关 lint 的更多信息,请访问 官方网站.

Findbugs

静态代码分析工具,用于分析 Java 字节码并检测各种各样的问题。

Config

创建 script-findbugs.gradle 文件把 findbugs 加入到你的 Android 项目。

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 重要的选项:

在你的 build.gradle 中导入 script-findbugs.gradle.

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

...

Test

为了测试的目的,我们将创建以下方法。

// MainActivity.java

...

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

...

重建你的项目,然后使用命令 ./gradlew findbugs 运行 findbugs. 如果它发现一些问题,你会看到类似的输出。

>./gradlew findbugs
Execution failed for task ':app:findbugs'.
FindBugs rule violations were found. 
See the report at: template/app/build/outputs/findbugs/findbugs.html

当你打开 findbugs.html 报告文件,你将看到如何解决问题的说明和建议列表。

2.png

如果你想忽略此问题,请将以下规则添加到 rules-findbugs.xml 文件中。

<FindBugsFilter>
   <Bug pattern="SF_SWITCH_NO_DEFAULT" />
   <Bug pattern="SF_SWITCH_FALLTHROUGH" />
</FindBugsFilter>

注意:还有一些其他方法可以 抑制 findbugs 警告。 有关 findbugs 的更多信息,请访问官方网站

PMD

PMD是一个源代码分析器。 它发现常见的编程缺陷,如未使用的变量,空 catch 块,不必要的对象创建等等。

Config

创建 script-pmd.gradle 文件把 pmd 添加到你的 Android 项目中。

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 重要的选项:

在你的 build.gradle 中导入 script-pmd.gradle

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

...

Test

为了测试目的,我们将创建以下方法。

// 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. 如果发现某些问题,你将看到类似的输出。

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

当你打开 pmd.xml 报告文件时,你将看到如何解决问题的说明和建议列表。

3.png

如果你想忽略这个问题,请将以下规则添加到 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 警告。 有关 pmd 的更多信息可以在 官方网站 上查看。

译文

  1. 小事情
  2. 版本号
  3. 静态代码分析工具
  4. 持续集成
上一篇下一篇

猜你喜欢

热点阅读