Android开发规范:代码规范(CheckStyle)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ddnosh/article/details/84197778

开发APP的过程中,每个团队都会约定自己的代码规范。但是往往在实践过程中,要么由于开发周期紧张,大家没有按照规范进行,而是按照自己的编程习惯来开发;要么是新入职的员工,或者从别的项目组借调过来的员工,不熟悉我们的开发规范。所以有必要用一些强制手段来规范我们设定的代码规范。
因此我们引入checkstyle来约束代码规范。
checkstyle有两种使用方式,一种是安装plugin插件,还有一种是通过gradle配置。

checkstyle: plugin

1. 安装插件
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这个因此很简单,搜索插件,安装,然后选择一个checkstyle文件。
2. checkstyle文件
这里介绍一下常用的checkstyle文件。
官网:
https://github.com/checkstyle/checkstyle

各种checkstyle文件参考:
Google:https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml
顺便说一下Google的java代码规范,可以默认用这个:
https://google.github.io/styleguide/javaguide.html
中文版本:http://www.hawstein.com/posts/google-java-style.html
需要说明的是google的checkstyle的tab缩进是两个空格,可以按需改成4个空格来使用。
Sun:https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/sun_checks.xml
HuaWei:https://gist.github.com/ownwell/c32878440216f1866842

3. 实战:
打开一个java文件,右键点击:check current file,结果如下:

修改后:
在这里插入图片描述
4. 自定义checkstyle
可以参考google的checkstyle文件,自行修改成为适合自己团队使用的代码规范文件。

checkstyle: gradle

我们配置gradle脚本可以让我们有两种方式去使用checkstyle:

  1. 可以直接运行命令执行checkstyle
  2. 可以在build app的时候,触发checkstyle任务,用来检测代码规范,如果不符合checkstyle规范,停止运行,并且提供html文件查看错误详情,直到完全符合我们定义的checkstyle规范才能继续运行

我觉得第二种方式比较使用,使用强制性的方式保证团队成员提交的代码都是符合checkstyle规范的。

操作步骤:
新建一个checkstyle.gradle文件,内容如下:

allprojects {
    project ->
        // 代码规范检查
        apply plugin: 'checkstyle'
        checkstyle {
            configFile rootProject.file('config/quality/checkstyle/checkstyle.xml')
            toolVersion '8.2'
            ignoreFailures false
            showViolations true
        }
        task('checkstyle', type: Checkstyle) {
            source 'src/main/java'
            include '**/*.java'
            exclude '**/gen/**'
            classpath = files()
        }
        tasks.whenTaskAdded { task ->
            boolean runCheckStyleOnLocalDev = "${enable_checkstyle}".toBoolean()
            boolean runCheckStyleTask = task.name == 'prepareReleaseDependencies' || (runCheckStyleOnLocalDev && task.name == 'preBuild')
            if (runCheckStyleTask) { //prepareReleaseDependencies, preBuild
                println("checkstyle run task.name :" + task.name)
                task.dependsOn 'checkstyle'
            }
        }
}

其中config/quality/checkstyle/checkstyle.xml是我们项目中checkstyle文件的路径。
然后在Project的build.gradle文件中加上

apply from: './checkstyle.gradle'

运行:

  1. 直接在terminal里面输入:“gradlew checkstyle”,即可运行checkstyle任务,结果如下:
    在这里插入图片描述
  2. Ctrl+ B,运行build,查看结果如下:
    在这里插入图片描述
    红框内提供了一个检查报告结果的路径,我们可以打开这个html文件格式的report报表,查看详细的检查结果,然后对比修改成符合我们定义的checkstyle规范的标准的代码。
    注意:
    如果错误太多, 输出的消息不一定会打印出检查结果路径:
    在这里插入图片描述
    如果没输出,我们就到各个工程的build\reports\checkstyle路径下查看即可。
    在这里插入图片描述

在这里插入图片描述

checkstyle: 155条规范

这个是checkstyle官网提供的检查标准,截至目前一共155条。
https://checkstyle.org/checks.html
我们在编写checkstyle的时候可以按照此规范来编写,点击进入有详细的属性介绍,如FileLength:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/ddnosh/article/details/84197778