0%

FindBugs™ - Find Bugs in Java Programs

A program which uses static analysis to look for bugs in Java code. It is free software, distributed under the terms of the Lesser GNU Public License. The name FindBugs™ and the FindBugs logo are trademarked by The University of Maryland. FindBugs has been downloaded more than a million times.

findbugs是一个分析bytecode并找出其中可疑部分的一个工具。它给项目字节码做一个全面扫描,通过一些通用规则去判断可能潜在的一些问题,比如性能,多线程安全等等。

FindBugs基本上只需要一个程序来做分析的字节码,所以这是非常容易使用。它能检测到常见的错误,如错误的布尔运算符。
FindBugs也能够检测到由于误解语言特点的错误,如Java参数调整(这不是真的有可能因为它的参数是传值)。

FindBugs是一个Java静态分析工具,用来检查类或者jar文件,用来发现可能的问题。检测完成之后会生成一份详细的报告,借助这份报告可以找到潜在的bug,比如NullPointException,特定的资源没有关闭,查询数据库没有调用Cursor.close()等,如果才用人工的方式很难才能发现类似的bug,或者这些bug永远不会发现,直到运行时才发现,还有可能是一直没有出现,别人调用的时候没有做检查就调用了…..

Java的静态分析工具当然可以无难度的在Android上面运行,通过这种FindBugs的检查可以让App的运行更加的稳定。

集成

FindBugs在Gradle中是当做一个插件存在的,可以在Android Studio中直接使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
apply plugin: 'findbugs'

// 定义生成文件目录
def findbugsReportPath = "${project.rootDir}/reports/"

task findbugs(type: FindBugs) {
ignoreFailures = true
excludeFilter = new File("findbugs.xml") // 自定义配置文件
classpath = files()
classes = fileTree('build/intermediates/classes/')
effort = 'max'

source = fileTree('src')
include '**/*.java'
exclude '**/gen/**'

// 定义输出报告
reports {
xml {
enabled = false
destination "$findbugsReportPath/FindBugs.xml"
xml.withMessages true
}
html {
enabled = true
destination "$findbugsReportPath/FindBugs.html"
}
}
}

afterEvaluate {
tasks.withType(Task).each { task ->
task.doLast {
if (task.name.startsWith("assemble")) {
tasks.findByName("findbugs").execute()
}
}
}
}

ignoreFailures:有警告错误的时候也是允许构建。
reportLevel:报告的级别,Low,Medium,High一般来说我们首先关注的是高级别的报告,再关注低一级别的报告。
classes和source分别是对应的.classe文件夹地址,和源代码文件地址。
repoets指定报告类型,有两种方式xml和html,只允许一种输出格式。
在右侧的Gradle的对于的Module可以在Tasks中找到对应的findBugs任务,点击即可运行。

Best Practice

How to improve quality and syntax of your Android code

About the hierarchy of my demo project :

Since you can split your gradle script in many files, I currently have 3 gradles files :

  1. The one in the root folder, which is more or less just about configuration for the project (which maven repos to use, which Gradle version to use….).
    1. The one in the subfolder app, which is a very classic gradle file to build an Android application.
    2. The one in the subfolder config, on which we will focus on, since I use this one to retain and configure all my quality tools for my project.

Pitfall

Android Studio 错误

com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

  • 第1步
    添加依赖于你的build.gradle支持MultiDex库
1
2
3
4
5
dependencies {
...
compile 'com.android.support:multidex:1.0.1'
...
}
  • 第2步
    在buildType或productFlavor中开启multiDexEnabled。
1
2
3
4
5
defaultConfig {
...
multiDexEnabled true
...
}

Reference

[1]Android中使用FindBugs
[2]FindBugs
[3]Android静态代码分析
[4]如何利用工具提高你的 Android 代码质量
[5]vb-android-app-quality
[6]Android Studio 错误 com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
[7]How to improve quality and syntax of your Android code
[8]FindBugs

欢迎关注我的其它发布渠道