Findbugs - a necessary check while building Android apps to perfection!

So I find most that people do not spend time on code analysis to avoid common mistakes in programming. Well a proper structured project going for deployment needs to have these. Using findbug and checkstyle are good development practises that needs to followed while development. I fiddle around with findbug a long time back and will be talking about common configurations and filters needed to be added.

Here are some of the articles related to findbug

  1. Key mistakes that matter link

Add findbug as an plugin to both IntelliJ and Eclipse from here. Create a separate gradle build file for findbug like this in your app folder

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
apply plugin: 'findbugs'
findbugs {
ignoreFailures = true
reportsDir = file("$project.buildDir/reports/findbugs/")
reportLevel = "medium"
effort = "max"
}
task findbugs(type: FindBugs) {
description 'run findbugs'
group 'code quality' // code quality/ verification
classes = fileTree("build/intermediates/classes/mock/debug/")
source = fileTree('src/main/java')
classpath = files() // specify the classpath
effort = 'max'
excludeFilter = file("findbugs_exclude.xml")
reports {
xml.enabled = false
html.enabled = true
}
}

You can tie this up with pmd as well for code analyzer based on rules. So this particular section will go in a separate gradle build file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
apply plugin: 'pmd'
task pmd (type: Pmd, dependsOn: assembleDebug) {
description 'Run pmd'
group 'verification'
ruleSets = ["basic", "braces", "strings", "design", "unusedcode"] // need custom ruleset add a ruleset configuration file
source = fileTree('src/main/java')
reports {
xml.enabled = false
html.enabled = true
}
}

Then include it in your app build.gradle

1
2
3
4
5
6
7
8
apply from: 'findbugs.gradle'
apply from: 'pmd.gradle'
//end
check.doLast {
project.tasks.getByName("findbugs").execute()
project.tasks.getByName("pmd").execute()
}

The check.doLast will ensure that findbugs and pmd is executed after the build.