代码分析工具FindBugs在Android开发中的应用

参考资料:
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-IDEAFilter标签页的Exclude filter files中添加配置文件:

    
    
      
        
        
      
      
        
      
    
    
  • 运行: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.enabledhtml.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.

你可能感兴趣的:(代码分析工具FindBugs在Android开发中的应用)