在咱们平时项目开发中,常常会写一些不严谨的代码或者一些比较低级的错误代码,可是这些错误每每很难被发现,这样就致使了咱们的项目中会隐藏了不少影响性能甚至是致使闪退的错误代码,因而许多响应的检测工具就出现了.在这里我就介绍一下我比较经常使用的几个检测工具吧javascript
顾名思义,FindBugs是一个寻找bug的工具,更具体的说FindBugs是一个静态检测java代码的工具,能够找到代码中的一些潜在bug,好比说NullPointerException,或者是一些流或者数据库没有关闭的问题.html
检测范围 :java
当建立流的时候发生了异常 那么inputStream就可能为空 接下来直接调用inputStream就可能产生空指针异常
android
这里使用了io流,可是没有作释放
git
task findbugs(type: FindBugs, dependsOn: "assembleDebug") {
ignoreFailures = false
effort = "max"
reportLevel = "high"
excludeFilter = new File("$configDir/findbugs/findbugs-filter.xml")//这里是自定义的规则
classes = files("${project.rootDir}/app/build/intermediates/classes")
source 'src'
include '**/*.java'
exclude '**/gen/**'
reports {
xml.enabled = false
html.enabled = true
xml {
destination "$reportsDir/findbugs/findbugs.xml" //这里是报告产生的路径
}
html {
destination "$reportsDir/findbugs/findbugs.html" //这里是报告产生的路径
}
}
classpath = files()
}复制代码
task pmd(type: Pmd) {
ignoreFailures = false
ruleSetFiles = files("$configDir/pmd/pmd-ruleset.xml") //这里是自定义的规则
ruleSets = []
source 'src'
include '**/*.java'
exclude '**/gen/**'
reports {
xml.enabled = false
html.enabled = true
xml {
destination "$reportsDir/pmd/pmd.xml" //这里是报告产生的路径
}
html {
destination "$reportsDir/pmd/pmd.html" //这里是报告产生的路径
}
}
}复制代码
TreatCheckstyle errors as warnings 若是勾上的话,检测到错误时只是会以警告的形式提示github
下面是勾上时的提示方式(以警告的形式)正则表达式
下面是不勾时的提示方式(以报错的形式)
数据库
下面是选择官方默认的检测规则(也能够本身定义,具体这里就不作详解了)
编程
task checkstyle(type: Checkstyle) {
configFile file("$configDir/checkstyle/checkstyle.xml") //这里是自定义的规则
configProperties.checkstyleSuppressionsPath = file("$configDir/checkstyle/suppressions.xml").absolutePath //这里是自定义的规则
source 'src'
include '**/*.java'
exclude '**/gen/**'
classpath = files()
}复制代码
注意Demo里面的task都抽取到了config目录下的quality.gradle中,因此须要在项目的build.gradle添加android-studio
apply from: '../config/quality.gradle'
Demo是在他人的基础上修改的,因为已经没法找到源头,因此在这里声明一下