参考资料:
Static Code Analysis using FindBugs (Android Studio)
How to improve quality and syntax of your Android code
1. 在Android Studio中使用
-
安装插件FindBugs-IDEA
-
在Preferences->Other Settings->FindBugs-IDEA中Filter标签页的Exclude filter files中添加配置文件:
<?xml version="1.0" encoding="UTF-8"?> <FindBugsFilter> <Match> <!-- ignore all issues in resource generation --> <Class name="~.*\\.R\\$.*"/> </Match> <Match> <Class name="~.*\\.Manifest\\$.*"/> </Match> </FindBugsFilter>
-
运行:Analyze -> FindBugs -> Analyze Project Files
2. 在Gradle中使用
在工程的build.gradle
中添加FindBugs的task:
apply plugin: 'findbugs'
task findbugs(type: FindBugs) {
ignoreFailures = false
effort = "default"
reportLevel = "medium"
excludeFilter = new File("${project.rootDir}/findbugs/findbugs-filter.xml")
classes = files("${project.rootDir}/app/build/intermediates/classes")
source = fileTree('src/main/java/')
classpath = files()
reports {
xml.enabled = true
html.enabled = true
xml {
destination "$project.buildDir/findbugs/findbugs-output.xml"
}
html {
destination "$project.buildDir/findbugs/findbugs-output.html"
}
}
}
需要注意reports中的xml.enabled和html.enabled只能enable一个,否则运行时会报如下错误:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:findbugs'.
> FindBugs tasks can only have one report enabled, however more than one report was enabled. You need to disable all but one of them.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
网友评论