美文网首页
代码分析工具FindBugs在Android开发中的应用

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

作者: sylcrq | 来源:发表于2016-09-14 18:08 被阅读156次

    参考资料:
    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中添加配置文件:

      <?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.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开发中的应用

          本文链接:https://www.haomeiwen.com/subject/hwzpettx.html