detekt/detekt

GitHub: detekt/detekt

detekt 是一款专为 Kotlin 语言设计的静态代码分析工具,用于检测代码异味、复杂度问题和风格违规,并支持 CI/CD 集成与多格式报告输出。

Stars: 6884 | Forks: 826

# detekt [![加入 #detekt KotlinLang 聊天室](https://img.shields.io/badge/%23detekt-on_slack-red.svg?logo=slack)](https://kotlinlang.slack.com/archives/C88E12QH4) [![访问网站 detekt.dev/](https://img.shields.io/badge/visit-website-red.svg?logo=firefox)](https://detekt.dev/) [![Maven Central](https://img.shields.io/maven-central/v/io.gitlab.arturbosch.detekt/detekt-cli?label=MavenCentral&logo=apache-maven)](https://search.maven.org/artifact/io.gitlab.arturbosch.detekt/detekt-cli) [![Gradle Plugin](https://img.shields.io/maven-central/v/io.gitlab.arturbosch.detekt/detekt-gradle-plugin?label=Gradle&logo=gradle)](https://plugins.gradle.org/plugin/io.gitlab.arturbosch.detekt) [![由 Develocity 加速](https://img.shields.io/badge/Revved%20up%20by-Develocity-06A0CE?logo=Gradle&labelColor=02303A)](https://ge.detekt.dev/scans) [![License](https://img.shields.io/github/license/detekt/detekt.svg)](LICENSE) ![Pre Merge Checks](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/967f8983be205750.svg) [![Codecov](https://codecov.io/gh/detekt/detekt/branch/main/graph/badge.svg)](https://codecov.io/gh/detekt/detekt) [![Awesome Kotlin 徽章](https://kotlin.link/awesome-kotlin.svg)](https://github.com/KotlinBy/awesome-kotlin) [![FOSSA Status](https://app.fossa.com/api/projects/custom%2B25591%2Fgithub.com%2Fdetekt%2Fdetekt.svg?type=small)](https://app.fossa.com/projects/custom%2B25591%2Fgithub.com%2Fdetekt%2Fdetekt?ref=badge_small) 认识一下 _detekt_,这是一个针对 [_Kotlin_ 编程语言](https://kotlinlang.org/)的静态代码分析工具。 访问[项目网站](https://detekt.dev/)获取安装指南、规则描述、配置选项等信息。 ![detekt in action](website/static/img/tutorial/detekt_in_action.png "detekt in action") ### 功能特性 - 为您的 [Kotlin](https://kotlinlang.org/) 项目进行代码异味分析。 - 高度可配置的规则集。 - 生成基线以抑制遗留项目中存在的问题,同时确保不引入新问题。 - 使用 `@Suppress` 注解在源文件中抑制问题。 - 支持多种报告格式:HTML、Markdown、[SARIF](https://sarifweb.azurewebsites.net/)、XML (Checkstyle) 以及自定义报告。 - 使用自定义规则集和报告[扩展 detekt](https://detekt.dev/docs/introduction/extensions)。 - 基于代码行数、圈复杂度和代码异味数量的复杂度报告。 - 通过我们的 [Gradle 插件](#with-gradle)实现官方 Gradle 集成。 - 拥有丰富的[第三方插件](https://github.com/topics/detekt-plugin)社区,为 detekt 添加更多规则和功能。 #### 快速链接 - [变更日志和迁移指南](https://detekt.dev/changelog.html) - [可用的 CLI 选项](https://detekt.dev/cli.html) - [规则集和规则描述](https://detekt.dev/complexity.html) - [编写自定义规则和扩展 detekt](https://detekt.dev/docs/introduction/extensions/) - [在代码中抑制问题](https://detekt.dev/suppressing-rules.html) - [通过基线文件抑制问题](https://detekt.dev/baseline.html) - [配置 detekt](https://detekt.dev/configurations.html) - Gradle 集成示例: - [多项目 (Kotlin DSL)](https://github.com/detekt/detekt/blob/main/build.gradle.kts) - [单项目 (Groovy DSL)](https://github.com/arturbosch/kutils/blob/master/build.gradle) - [单项目 (非官方 Maven 插件)](https://github.com/detekt/sonar-kotlin/blob/main/pom.xml) ### 快速开始 ... #### 使用命令行界面 ``` curl -sSLO https://github.com/detekt/detekt/releases/download/v[version]/detekt-cli-[version]-all.jar java -jar detekt-cli-[version]-all.jar --help ``` 你可以在[这里找到安装 detekt 的其他方法](https://detekt.dev/cli.html) #### 使用 Gradle ``` plugins { id("io.gitlab.arturbosch.detekt") version "[version]" } repositories { mavenCentral() } detekt { buildUponDefaultConfig = true // preconfigure defaults allRules = false // activate all available (even unstable) rules. config.setFrom("$projectDir/config/detekt.yml") // point to your custom config defining rules to run, overwriting default behavior baseline = file("$projectDir/config/baseline.xml") // a way of suppressing issues before introducing detekt } tasks.withType().configureEach { reports { html.required.set(true) // observe findings in your browser with structure and code snippets checkstyle.required.set(true) // checkstyle(xml) like format mainly for integrations like Jenkins sarif.required.set(true) // standardized SARIF format (https://sarifweb.azurewebsites.net/) to support integrations with GitHub Code Scanning markdown.required.set(true) // simple Markdown format } } // Groovy DSL tasks.withType(Detekt).configureEach { jvmTarget = "1.8" } tasks.withType(DetektCreateBaselineTask).configureEach { jvmTarget = "1.8" } // or // Kotlin DSL tasks.withType().configureEach { jvmTarget = "1.8" } tasks.withType().configureEach { jvmTarget = "1.8" } ``` 发布版本请参见 [Maven Central](https://search.maven.org/artifact/io.gitlab.arturbosch.detekt/detekt-cli),快照版本请参见 [Sonatype](https://central.sonatype.com/repository/maven-snapshots/)。 如果您想使用 SNAPSHOT 版本,可以在[此文档页面](https://detekt.dev/snapshots.html)找到更多信息。 #### 环境要求 ##### 执行 detekt Gradle 6.8.3+ 是最低要求。但是,推荐的版本以及其他工具的推荐版本如下: | Detekt 版本 | Gradle | Kotlin | AGP | Java Target Level | JDK Max Version | |-----------------|----------|----------|----------|-------------------|-----------------| | `2.0.0-alpha.2` | `9.3.0` | `2.3.0` | `9.0.0` | `1.8` | `25` | | `2.0.0-alpha.1` | `9.1.0` | `2.2.20` | `8.13.0` | `1.8` | `25` | | `2.0.0-alpha.0` | `8.13.0` | `2.2.10` | `8.13.0` | `1.8` | `21` | | `1.23.8` | `8.12.1` | `2.0.21` | `8.8.1` | `1.8` | `21` | [此处列出了以前 detekt 版本的推荐版本列表](https://detekt.dev/compatibility.html)。 ##### 构建 detekt 构建 detekt 需要 Java 17 或更高版本。 ### 添加更多规则集 detekt 本身提供了 [ktlint](https://github.com/pinterest/ktlint) 的包装器作为 `ktlint` 规则集, 可以轻松添加到 Gradle 配置中: ``` dependencies { detektPlugins("io.gitlab.arturbosch.detekt:detekt-rules-ktlint-wrapper:[version]") } ``` 同样,detekt 也提供了额外的规则集: ``` dependencies { detektPlugins("io.gitlab.arturbosch.detekt:detekt-rules-libraries:[version]") detektPlugins("io.gitlab.arturbosch.detekt:detekt-rules-ruleauthors:[version]") } ``` 更多信息请访问 [Detekt Marketplace](https://detekt.dev/marketplace)。 同样地,也可以向 detekt 添加自定义[扩展](https://detekt.dev/docs/introduction/extensions/)。 ### 提及 [![androidweekly](https://img.shields.io/badge/androidweekly.net-259-orange.svg?style=flat-square)](http://androidweekly.net/issues/issue-259) [![androidweekly](https://img.shields.io/badge/androidweekly.cn-154-orange.svg?style=flat-square)](http://androidweekly.cn/android-dev-wekly-issue-154/) 提及自... - [迈向 Detekt 2.0 及未来!](https://www.youtube.com/watch?v=6EQd_SDR6n0) - [droidcon London 2021 - Detekt - 状态报告](https://www.droidcon.com/2021/11/17/detekt-state-of-the-union-2/) - [KotlinConf 2018 - 安全(r)的 Kotlin 代码 - Kotlin 静态分析工具,作者 Marvin Ramin](https://www.youtube.com/watch?v=yjhQiP0329M) - [droidcon NYC 2018 - Kotlin 静态代码分析](https://www.youtube.com/watch?v=LT6m5_LO2DQ) - Kotlin 代码质量工具 - 作者 @vanniktech [幻灯片](https://docs.google.com/presentation/d/1sUoQCRHTR01JfaS67Qkd7K1rdRLOhO6QGCelZZwxOKs/edit) [演示](https://www.youtube.com/watch?v=FKDNE6PPTTE) - [在工作流中集成 detekt](https://www.raywenderlich.com/24470020-integrating-detekt-in-the-workflow) - [检查 Kotlin 代码质量](https://blog.frankel.ch/check-quality-kotlin-code/) - [Kotlin 静态分析工具](http://smyachenkov.com/posts/kotlin-static-analysis-tools/) - [你还能闻到它吗?:Java 和 Kotlin 语言的比较研究](https://doi.org/10.1145/3267183.3267186),作者 [Flauzino 等人](https://github.com/matheusflauzino/smells-experiment-Kotlin-and-Java) - [使用 Detekt 预防软件反模式](https://galler.dev/preventing-software-antipatterns-with-detekt/) 集成: - [IntelliJ 集成](https://github.com/detekt/detekt-intellij-plugin) - [SonarQube 集成](https://docs.sonarsource.com/sonarqube/latest/analyzing-source-code/importing-external-issues/external-analyzer-reports/) - [TCA(腾讯代码分析) 集成](https://github.com/Tencent/CodeAnalysis/blob/main/client/tool/detekt.py) - [Codacy](https://www.codacy.com) - [配置 Error Prone, Checkstyle, PMD, CPD, Lint, Detekt & Ktlint 的 Gradle 插件](https://github.com/vanniktech/gradle-code-quality-tools-plugin) - [Violations Lib](https://github.com/tomasbjerre/violations-lib) 是一个用于解析静态代码分析等报告文件的 Java 库。 - [sputnik](https://github.com/TouK/sputnik) 是一个免费的静态代码审查工具,并提供对 detekt 的支持 - [Detekt Maven 插件](https://github.com/Ozsie/detekt-maven-plugin),封装了 Detekt CLI - [Detekt Bazel 插件](https://github.com/buildfoundation/bazel_rules_detekt),封装了 Detekt CLI - [帮助促进 GitHub PR 检查和违规自动评论的 Gradle 插件](https://github.com/btkelly/gnag) - [Codefactor](http://codefactor.io/) - [GitHub Action: Detekt All](https://github.com/marketplace/actions/detekt-all) - [GitHub Action: Setup detekt](https://github.com/marketplace/actions/setup-detekt) 来自第三方的自定义规则和报告可以在我们的 [**Detekt Marketplace**](https://detekt.dev/marketplace) 上找到。 #### 致谢 - [JetBrains](https://github.com/jetbrains/) - 创造了 IntelliJ + Kotlin - [PMD](https://github.com/pmd/pmd) & [Checkstyle](https://github.com/checkstyle/checkstyle) & [ktlint](https://github.com/pinterest/ktlint) - 阈值和风格规则的灵感来源
标签:Android开发, Detekt, Gradle插件, JS文件枚举, Kotlin, Lint工具, Maven, 云计算, 代码味道, 代码规范, 后台面板检测, 安全专业人员, 安全扫描, 文档结构分析, 时序注入, 漏洞验证, 编程工具, 自动化审查, 规则引擎, 远程代码执行, 错误基检测, 静态代码分析