android静态代码扫描,android 静态代码扫描

开始做这样一个东西是为了帮助开发减少代码方面的问题,提高代码质量,减小以后上线的风险。前面看了 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

*

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)

### 后续

目前还处在试验阶段,开发对这东西也还不怎么习惯,毕竟以前有些人以前都不会怎么关注代码的一些质量问题,并且相关的问题信息都是英文的...不过基本还是正向的,毕竟以后代码质量优化这块肯定要做的,而且确实代码一些潜在问题可能现在看不出,但这都是隐患。另外有些规则确实也没什么卵用,所以现在需要开发在磨合过程中,逐渐完善扫描规则,让扫描不是一个摆设。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值