说明:网络上的资料要么过时,要么不全;因此自己通过实践,重新整理了一份使用攻略
作者:一哥的马甲
创建时间: 2020年09月06日
开发环境:Mac + Android Studio
简介
Detekt 是 kotlin 的静态代码检测工具,有丰富的配置供开发者选择。本文只介绍基本配置和使用,剩下的内容请开发者自行探索
项目地址点这里
配置
打开项目的根配置
注意:Gradle 版本要大于 5.4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | buildscript { repositories { jcenter() } } // 1. 添加 detekt 插件 plugins { id("io.gitlab.arturbosch.detekt").version("1.13.1") } // 2. 添加 detekt 配置文件 detekt { toolVersion = "1.13.1" config = files("config/detekt/detekt.yml") // failFast = true // fail build on any finding buildUponDefaultConfig = true // preconfigure defaults reports { reports { xml { enabled = true destination = file("path/to/destination.xml") } html { enabled = true destination = file("path/to/destination.html") } txt { enabled = true destination = file("path/to/destination.txt") } } } } |
同步项目,运行
运行命令
在需要使用检测的 module 中的
1 2 | // 配置 detekt 插件 apply plugin: 'io.gitlab.arturbosch.detekt' |
也可以在根目录的
1 2 3 4 5 6 7 8 | allprojects { repositories { ... ... } // 这样配置即可全局生效 apply plugin: 'io.gitlab.arturbosch.detekt' } |
使用
直接在命令行运行

result1
此时也会在项目的

result2
添加 Git Hook
可以在每次提交之前通过 hook 自动运行检测脚本;在项目的
项目的
.git/hooks 文件夹下有一些 samples ,新建脚本必须与这些 samples 文件名一致(可以省略后缀)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | echo "? 开始执行 kotlin 代码检查" ./gradlew detekt result=$? if [ "$result" = 0 ] ; then echo "? 代码检查完毕,没有问题" exit 0 else echo "? 代码检查完毕,发现问题,请修复后重新提交" echo "? 请到项目的 /build/report 目录下查看可视化文档" exit 1 fi |
保存,并将文件设置为可执行文件
如果不想运行 hook 可以使用
git commit --no-verify
共享 Git Hook
使用 Git (version >= 2.9)映射功能
在项目根目录下新建文件夹
执行命令