您的位置:首页 > 移动开发 > Android开发

白盒测试 android 静态代码扫描

2017-03-04 18:11 423 查看
白盒测试 android 静态代码扫描
https://testerhome.com/topics/4653 https://testerhome.com/topics/4653 https://testerhome.com/topics/4653
开始做这样一个东西是为了帮助开发减少代码方面的问题,提高代码质量,减小以后上线的风险。前面看了360的那个静态代码扫描感觉很强大,但目前没这实力去做成这样,希望早日开源,多多学习。所以就先用开源的也能解决下问题。


怎么来的

开始做是想直接使用sonar自带的android静态代码扫描,但后面发现不是很好用,而且sonar对于移动端的扫描能力好像也不是很强。

后面在逛github时发现一个项目,就是关于android的代码扫描的,觉得思路不错,而且扩展性也不错,就参考了这个项目,传送门


怎么做的

我们项目是用gradle进行编译,我用的方法比较low,每次jenkins拉下代码后,直接用自己的gradle文件替换项目的文件,然后将配置文件夹config直接拷进项目。
说下gradle文件吧,主要是先引用config/quality.gradle这个gradle文件,主要是添加
apply
from: '../config/quality.gradle'
,
quality.gradle
这个文件定义了包括checkstyle,findbugs,pmd,lint这些扫描工具的任务,因为现在我们项目的android代码没有定义规范,所以checkstyle就没用,文件内容如下:
``` apply plugin: 'findbugs' apply plugin: 'pmd'

/*
Copyright 2015 Vincent Brison. *
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at *
http://www.apache.org/licenses/LICENSE-2.0 *
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

// Add checkstyle, findbugs, pmd and lint to the check task.

check.dependsOn 'findbugs', 'pmd', 'lint'

task findbugs(type: FindBugs, dependsOn: assembleDebug) {

ignoreFailures = true #注意这里需要设置为true,否则有失败就会停止,后面的任务就不会跑了

effort = "max"

reportLevel = "low"

excludeFilter = new File("${project.rootDir}/config/quality/findbugs/findbugs-filter.xml")

classes = files("${project.rootDir}/app/build/intermediates/classes")

source 'src'

include '/*.java'

exclude '/gen/**'

reports {

xml.enabled = true #因为需要在jenkins中集成,所以需要开启xml

html.enabled = false #而且xml和html只能开启一个,注意关掉html哦

xml {

destination "$project.buildDir/reports/findbugs/findbugs.xml"

}

html {

destination "$project.buildDir/reports/findbugs/findbugs.html"

}

}

classpath = files()

}

task pmd(type: Pmd) {

ignoreFailures = true #注意这里需要设置为true,否则有失败就会停止,后面的任务就不会跑了

ruleSetFiles = files("${project.rootDir}/config/quality/pmd/pmd-ruleset.xml")

ruleSets = []

source 'src'

include '/*.java'

exclude '/gen/**'

reports {

xml.enabled = false #同理

html.enabled = true

xml {

destination "$project.buildDir/reports/pmd/pmd.xml"

}

html {

destination "$project.buildDir/reports/pmd/pmd.html"

}

}

}

android {

lintOptions {

abortOnError true

xmlReport false

htmlReport true

lintConfig file("${project.rootDir}/config/quality/lint/lint.xml")

htmlOutput file("$project.buildDir/reports/lint/lint-result.html")

xmlOutput file("$project.buildDir/reports/lint/lint-result.xml")

}

}
### 怎么集成的
1. 下载`gradle`编译的插件,并且在系统管理中配置gradle的路径
2. 下载`findbugs`,`pmd`以及`lint`相应的插件
3. 插件都弄完后,在jenkins中创建任务,指定拉去代码仓库,跑下shell脚本替换指定文件,并且加入config目录
4. 指定gradle的task`clean findbugs pmd lint`
5. 如有需要指定需要什么什么跑
6. 然后再配置各个插件收集结果的xml文件,注意需要设置`Run always`,根据自己的需求进行配置
7. 设置邮件模版,将结果发给指定的开发基本就完成了
![](/photo/2016/ad1da298153d8d8d1a395a71a7d30b7d.png)

### 后续
目前还处在试验阶段,开发对这东西也还不怎么习惯,毕竟以前有些人以前都不会怎么关注代码的一些质量问题,并且相关的问题信息都是英文的...不过基本还是正向的,毕竟以后代码质量优化这块肯定要做的,而且确实代码一些潜在问题可能现在看不出,但这都是隐患。另外有些规则确实也没什么卵用,所以现在需要开发在磨合过程中,逐渐完善扫描规则,让扫描不是一个摆设。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: