KevinDoremy/SearchDeadCode

GitHub: KevinDoremy/SearchDeadCode

一款快速 Rust CLI 工具,用于检测并安全移除 Android 项目(Kotlin 和 Java)中的死代码和未使用资源。

Stars: 11 | Forks: 4

SearchDeadCode Logo # 搜索 DeadCode **在 Android 项目中查找并消除死代码** [English](README.md) · [简体中文](docs/README.zh-CN.md) · [日本語](docs/README.ja.md) · [한국어](docs/README.ko.md) [![CI](https://static.pigsec.cn/wp-content/uploads/repos/cas/ad/ad5834178f7599af9fdda11629d49cae07f2997beec49821b2920eff5bfd50e7.svg)](https://github.com/KevinDoremy/SearchDeadCode/actions/workflows/ci.yml) [![Crates.io](https://img.shields.io/crates/v/searchdeadcode.svg?style=flat-square)](https://crates.io/crates/searchdeadcode) [![Cargo downloads](https://img.shields.io/crates/d/searchdeadcode.svg?style=flat-square&label=cargo%20downloads)](https://crates.io/crates/searchdeadcode) [![GitHub downloads](https://img.shields.io/github/downloads/KevinDoremy/SearchDeadCode/total?style=flat-square&label=binary%20downloads)](https://github.com/KevinDoremy/SearchDeadCode/releases) [![Clones](https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2FKevinDoremy%2FSearchDeadCode%2Fstats%2Fstats%2Fclones-badge.json&style=flat-square)](https://github.com/KevinDoremy/SearchDeadCode/tree/stats/stats) [![Total clones](https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2FKevinDoremy%2FSearchDeadCode%2Fstats%2Fstats%2Fcumulative-badge.json&style=flat-square)](https://github.com/KevinDoremy/SearchDeadCode/tree/stats/stats) [![MSRV](https://img.shields.io/badge/MSRV-1.80-blue.svg?style=flat-square)](https://blog.rust-lang.org/2024/07/25/Rust-1.80.0.html) [![Homebrew](https://img.shields.io/badge/Homebrew-available-FBB040?logo=homebrew&logoColor=white&style=flat-square)](https://github.com/KevinDoremy/homebrew-tap) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square)](https://opensource.org/licenses/MIT) 一个快速的 Rust CLI,用于检测并安全移除 Android 项目(Kotlin 和 Java)中的死代码。灵感来自于 Swift 的 [Periphery](https://github.com/peripheryapp/periphery)。 ``` brew install KevinDoremy/tap/searchdeadcode # macOS / Linux cargo install searchdeadcode # via Cargo ``` SearchDeadCode Demo
## 为什么选择 SearchDeadCode - **快速。** 在 1 秒内解析 1 000 个文件;在 5 秒内解析 10 000 个文件。 - **专为 Android 设计。** Activities、Fragments、Compose、AndroidManifest、布局 XML、DI 注解均自动保留为入口点。 - **混合分析。** 结合静态分析与 JaCoCo / Kover / LCOV 覆盖率以及 R8 的 `usage.txt`,提供确凿的检测结果。 - **安全删除。** 支持交互式、批处理和 dry-run(演练)模式,并生成恢复脚本。 - 与 [kotlin-jump](https://github.com/elumine-dev/kotlin-jump) 完美搭配,提供编辑器侧的导航功能。 ## 与同类工具对比 | 功能 | SearchDeadCode | Android Lint | R8 / ProGuard | Detekt | IntelliJ | |---|:---:|:---:|:---:|:---:|:---:| | 速度 | <1秒/1千个文件 | 慢 | 构建时 | 中等 | 中等 | | Kotlin 优先 | ✅ | 部分 | ✅ | ✅ | ✅ | | Java 支持 | ✅ | ✅ | ✅ | ❌ | ✅ | | 安全删除 | ✅ 交互式 | ❌ | ❌ | ❌ | 仅限 IDE | | 支持 CI / CD | ✅ SARIF, JSON | ✅ XML | ❌ | ✅ SARIF | ❌ | | 覆盖率整合 | ✅ JaCoCo, Kover, LCOV | ❌ | ❌ | ❌ | ❌ | | 循环依赖检测 | ✅ 僵尸代码 | ❌ | ❌ | ❌ | ❌ | | 资源检测 | ✅ | ✅ | ❌ | ❌ | ✅ | | 独立运行(无需构建) | ✅ | ❌ | ❌ | ❌ | ❌ | | 许可证 | MIT | Apache | 专有 | Apache | 专有 | **何时使用哪种工具**:SearchDeadCode 适用于快速的 CI 反馈和项目审查。Android Lint 适用于更广泛的 Android 专项检查。R8 适用于生产构建级别的精确度。Detekt 适用于风格和复杂度检查。IntelliJ 适用于 IDE 内部的交互式重构。 ## 快速开始 ``` # 分析你的 Android 项目 searchdeadcode ./my-android-app # 预览将被删除的内容(不作更改) searchdeadcode ./my-android-app --delete --dry-run # 仅限 High-confidence 发现 searchdeadcode ./my-android-app --min-confidence high ``` 每个命令及其真实输出:[`docs/cli-tour.md`](docs/cli-tour.md)。 ### 示例输出 ``` $ searchdeadcode ./my-app --min-confidence high SearchDeadCode v0.4.0 Found 247 files to analyze Reachability: 1 847 reachable, 2 103 total Found 12 dead code issues: Confidence Legend: ● Confirmed (runtime) ◉ High ○ Medium ◌ Low app/src/main/java/com/example/data/OldApiClient.kt ◉ 15:1 warning [DC001] class 'LegacyApiClient' is never used app/src/main/java/com/example/utils/StringUtils.kt ◉ 42:5 warning [DC001] function 'formatLegacyDate' is never used ◉ 67:5 warning [DC001] function 'parseOldFormat' is never used Summary: 12 issues in 4 files (3 classes, 5 functions, 4 properties) Estimated removable lines: ~340 ``` ## 检测能力 | 类别 | 检测内容 | |---|---| | 核心 | 未使用的类、接口、方法、函数、属性、字段、导入 | | 高级 | 未使用的参数、枚举项、类型别名 | | 智能 | 仅赋值的属性、死分支、多余的 public 修饰符 | | Android | Activities、Fragments、XML 布局、AndroidManifest 条目(自动保留) | | 资源 | 未使用的字符串、颜色、尺寸、样式、属性 | 各检测器的完整参考和代码示例:[`docs/detectors.md`](docs/detectors.md)。 ## 安装 ### Homebrew (macOS / Linux) ``` brew tap KevinDoremy/tap brew install searchdeadcode ``` ### Cargo ``` cargo install searchdeadcode ``` ### 预编译二进制文件 从 [GitHub Releases](https://github.com/KevinDoremy/SearchDeadCode/releases) 下载。支持 Linux x86_64/aarch64、macOS Intel/Apple Silicon、Windows x86_64。 ### 从源码构建 ``` git clone https://github.com/KevinDoremy/SearchDeadCode cd SearchDeadCode cargo install --path . ``` ## 用法指南 ``` # 基础分析 searchdeadcode ./app # 用于程序化使用的 JSON 输出 searchdeadcode ./app --format json --output report.json # 用于 GitHub Code Scanning 的 SARIF searchdeadcode ./app --format sarif --output report.sarif # 结合 coverage + R8 usage 的混合分析 searchdeadcode ./app \ --coverage build/reports/jacoco/test/jacocoTestReport.xml \ --proguard-usage app/build/outputs/mapping/release/usage.txt \ --detect-cycles \ --min-confidence high # 通过 dry-run 进行安全删除 searchdeadcode ./app --delete --dry-run ``` 高级功能:混合覆盖率分析、R8 / ProGuard 集成、僵尸代码检测、watch 模式、baseline 支持、未使用的资源、未使用的参数。完整 CLI 参考请参阅 [`docs/cli-reference.md`](docs/cli-reference.md),覆盖率 + R8 工作流请参阅 [`docs/hybrid-analysis.md`](docs/hybrid-analysis.md)。 ## CI 集成 (GitHub Actions) ``` # .github/workflows/dead-code.yml name: Dead Code Detection on: [push, pull_request] jobs: dead-code: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Detect Dead Code uses: KevinDoremy/SearchDeadCode@v0 with: path: '.' format: 'sarif' output: 'deadcode.sarif' min-confidence: 'high' fail-on-findings: 'true' - name: Upload SARIF uses: github/codeql-action/upload-sarif@v2 with: sarif_file: deadcode.sarif ``` GitLab CI、Bitbucket 和 pre-commit 钩子示例请参阅 [`docs/ci-integration.md`](docs/ci-integration.md)。 ## 配置 SearchDeadCode 会查找 `.deadcode.yml`、`.deadcode.toml`,或通过 `--config` 传入的路径。最小示例: ``` # .deadcode.yml targets: - "app/src/main/kotlin" - "app/src/main/java" exclude: - "**/generated/**" - "**/build/**" - "**/test/**" retain_patterns: - "*Adapter" - "*ViewHolder" - "*Binding" android: parse_manifest: true parse_layouts: true auto_retain_components: true ``` 完整 schema(YAML + TOML)及 Android 专属选项:[`docs/configuration.md`](docs/configuration.md)。 ## 何时不建议使用 SearchDeadCode 坦诚自身的局限性有助于您选择合适的工具。在以下情况中请跳过 SearchDeadCode: - **您需要 100% 的准确率。** 静态分析无法捕获反射或仅在运行时存在的引用。建议对照 R8 的 `usage.txt` 进行验证,或通过 `--proguard-usage` 传入。 - **大量使用反射。** 通过 `Class.forName()` 访问的代码看起来像是未被使用的。变通方法:将反射目标添加到 `retain_patterns` 中。 - **纯 Java 项目。** SearchDeadCode 以 Kotlin 优先。Java 同样适用,但 [UCDetector](https://ucdetector.org/) 或 IntelliJ 的代码检查可能更合适。 - **您需要 IDE 集成。** 本工具是一个 CLI。请使用 IntelliJ / Android Studio 的“Unused declaration”检查,或者在旁边以 `--watch` 模式运行 SearchDeadCode。 - **动态目标平台 (KMP JS)。** 我们分析的是 JVM 字节码模式。JavaScript 和其他动态目标平台不在处理范围内。 但如果您需要:极速、CI 集成、支持撤销的安全删除、混合覆盖率分析,或免构建的项目审查,您很可能会需要 SearchDeadCode。 ## 文档 - [`docs/detectors.md`](docs/detectors.md) — 包含代码示例的 9 种检测类型 - [`docs/cli-reference.md`](docs/cli-reference.md) — 完整的 CLI 参考和命令示例 - [`docs/configuration.md`](docs/configuration.md) — YAML 和 TOML schemas - [`docs/hybrid-analysis.md`](docs/hybrid-analysis.md) — 覆盖率、R8 / ProGuard、僵尸代码 - [`docs/ci-integration.md`](docs/ci-integration.md) — GitHub Actions、GitLab、pre-commit 钩子 - [`docs/troubleshooting.md`](docs/troubleshooting.md) — Gatekeeper、常见问题、已知限制 - [`docs/architecture.md`](docs/architecture.md) — pipeline、技术栈、项目结构、性能目标 - [`docs/research.md`](docs/research.md) — 死代码检测范式(Periphery、Meta SCARF、R8、tree shaking) - [`docs/roadmap.md`](docs/roadmap.md) — 计划用于未来检测器的 40 个高级模式 - [`CHANGELOG.md`](CHANGELOG.md) — 完整的版本历史 ## 配套工具 - [kotlin-jump](https://github.com/elumine-dev/kotlin-jump) — VS Code 的 Kotlin/Java 导航工具,无需 JVM(9,639 次安装)。 - [detekt-lsp](https://github.com/elumine-dev/detekt-lsp) — 为任何 LSP 编辑器提供实时的 Detekt 诊断(预发布阶段)。 - SearchDeadCode — 本项目。 由 [Kevin Doremy](https://kevindoremy.com) 与 [elumine-dev](https://github.com/elumine-dev) 共同维护。 ## 许可证 [MIT](LICENSE) © Kevin Doremy Laferrière
标签:Android开发, JS文件枚举, Kotlin, LNA, Rust, 云安全监控, 可视化界面, 死代码检测, 网络流量审计, 通知系统, 静态分析