|
1 | 1 | # Kotlin |
2 | 2 |
|
3 | | -Contributions welcome! See [contributing guidelines](CONTRIBUTING.md). |
| 3 | +## Quick Reference |
4 | 4 |
|
5 | | -Tools to cover (draft list): gradle, ktlint, detekt, diktat, kotlinc, kover, spotless, kotlin-test. |
| 5 | +| Tool | AI-Friendly Flags | |
| 6 | +|-------------------|---------------------------------------------------------------------| |
| 7 | +| ./gradlew (build) | `--console=plain` (add `-q` for minimal output) | |
| 8 | +| ./gradlew test | `--console=plain` | |
| 9 | +| kotlinc | No flags needed (no colors, no progress bars) | |
| 10 | +| ktlint | `--relative --log-level=none` | |
| 11 | +| ktlint (fix) | `-F --relative --log-level=none` | |
| 12 | +| detekt | No flags needed (default output is one-line-per-finding, no colors) | |
| 13 | +| spotless (Gradle) | `--console=plain` | |
| 14 | + |
| 15 | +## ./gradlew |
| 16 | + |
| 17 | +```bash |
| 18 | +# Build — clean output |
| 19 | +./gradlew build --console=plain |
| 20 | + |
| 21 | +# Errors only |
| 22 | +./gradlew build --console=plain -q |
| 23 | +``` |
| 24 | + |
| 25 | +- `--console=plain` — disables all colors, progress bars, and rich status output |
| 26 | +- `-q` / `--quiet` — suppresses most output (errors and explicit stdout from build scripts still show) |
| 27 | +- `-w` / `--warn` — shows warnings and errors only |
| 28 | +- `--no-daemon` — runs without the Gradle daemon (avoids stale daemon issues in CI) |
| 29 | +- `--warning-mode=summary` — shows deprecation warning summary instead of individual warnings |
| 30 | + |
| 31 | +With `--console=auto` (default), Gradle auto-detects TTY via `isatty()` and uses plain text in non-TTY environments. `--console=plain` makes this explicit. |
| 32 | + |
| 33 | +Gradle does not respect `NO_COLOR=1`. Use `--console=plain` instead. |
| 34 | + |
| 35 | +Persistent config via `gradle.properties`: |
| 36 | + |
| 37 | +```properties |
| 38 | +org.gradle.console=plain |
| 39 | +org.gradle.daemon=false |
| 40 | +``` |
| 41 | + |
| 42 | +### ./gradlew test |
| 43 | + |
| 44 | +```bash |
| 45 | +./gradlew test --console=plain |
| 46 | + |
| 47 | +# Single test class |
| 48 | +./gradlew test --console=plain --tests "com.example.MyTest" |
| 49 | + |
| 50 | +# Single test method |
| 51 | +./gradlew test --console=plain --tests "com.example.MyTest.myMethod" |
| 52 | +``` |
| 53 | + |
| 54 | +Test results are saved as JUnit XML in `build/test-results/test/` automatically. To control console test output, configure `testLogging` in `build.gradle.kts`: |
| 55 | + |
| 56 | +```kotlin |
| 57 | +tasks.test { |
| 58 | + testLogging { |
| 59 | + events("failed") |
| 60 | + exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +- `events("failed")` — stream individual test failure events to console (default shows no individual test events — only the final pass/fail count) |
| 66 | +- `exceptionFormat = FULL` — show full stack traces for failures |
| 67 | + |
| 68 | +## kotlinc |
| 69 | + |
| 70 | +```bash |
| 71 | +kotlinc -Werror src/main/kotlin/ |
| 72 | +``` |
| 73 | + |
| 74 | +- `-Werror` — treats warnings as errors (exits 1 on any warning) |
| 75 | +- `-nowarn` — suppresses all warnings |
| 76 | +- `-Wextra` — enables extra checkers (Kotlin 2.0+) |
| 77 | +- `-verbose` — enables verbose logging output |
| 78 | +- `-progressive` — enables progressive mode where deprecations take effect immediately |
| 79 | + |
| 80 | +kotlinc outputs errors and warnings to stderr in `file:line:col: severity: message` format, followed by the source line and a `^` caret pointer (typically 3 lines per diagnostic). No color or progress flags exist because kotlinc produces no colors or progress bars. |
| 81 | + |
| 82 | +kotlinc is rarely invoked directly — Gradle handles compilation. These flags are configured in `build.gradle.kts`: |
| 83 | + |
| 84 | +```kotlin |
| 85 | +tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> { |
| 86 | + compilerOptions { |
| 87 | + allWarningsAsErrors.set(true) |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +## ktlint |
| 93 | + |
| 94 | +```bash |
| 95 | +# Check |
| 96 | +ktlint --relative --log-level=none "src/**/*.kt" |
| 97 | + |
| 98 | +# Single file |
| 99 | +ktlint --relative --log-level=none src/main/kotlin/MyFile.kt |
| 100 | + |
| 101 | +# Fix |
| 102 | +ktlint -F --relative --log-level=none "src/**/*.kt" |
| 103 | +``` |
| 104 | + |
| 105 | +- `--relative` — prints file paths relative to the working directory instead of absolute paths |
| 106 | +- `--log-level=none` — suppresses informational and warning log messages printed to stderr |
| 107 | +- `-F` / `--format` — auto-corrects fixable violations in place |
| 108 | +- `--color` — opt-in flag to enable ANSI color codes (colors are off by default) |
| 109 | +- `--limit=<n>` — caps the number of errors shown |
| 110 | +- `--reporter=<name>` — selects output format |
| 111 | + |
| 112 | +Default output (plain reporter) is already `file:line:col: message (rule)` — one violation per line, no colors unless `--color` is explicitly passed. |
| 113 | + |
| 114 | +ktlint always prints a summary footer after violations listing error counts by rule. This footer cannot be suppressed. |
| 115 | + |
| 116 | +Other reporters: `plain?group_by_file`, `plain-summary` (counts only, no individual violations), `json`, `sarif`, `checkstyle`, `html`. |
| 117 | + |
| 118 | +Via Gradle (ktlint-gradle plugin): |
| 119 | + |
| 120 | +```bash |
| 121 | +./gradlew ktlintCheck --console=plain |
| 122 | +./gradlew ktlintFormat --console=plain |
| 123 | +``` |
| 124 | + |
| 125 | +## detekt |
| 126 | + |
| 127 | +```bash |
| 128 | +# Check |
| 129 | +detekt --input src/ |
| 130 | + |
| 131 | +# Single file |
| 132 | +detekt --input src/main/kotlin/MyFile.kt |
| 133 | +``` |
| 134 | + |
| 135 | +- `--auto-correct` / `-ac` — auto-corrects violations where supported (requires the `formatting` ruleset plugin) |
| 136 | +- `--report` / `-r` — generates report files: `-r txt:report.txt -r xml:report.xml` |
| 137 | +- `--base-path` / `-bp` — sets base directory for relative paths in report files (does not affect console output) |
| 138 | +- `--parallel` — enables parallel analysis (beneficial for codebases over ~2000 lines) |
| 139 | +- `--all-rules` — activates all available rules including unstable ones |
| 140 | +- `--max-issues=<n>` — exits 0 if found issues count does not exceed the threshold |
| 141 | +- `--baseline` / `-b` — compares against a baseline file, only reporting new findings |
| 142 | +- `--build-upon-default-config` — uses default config as base, allowing overrides |
| 143 | + |
| 144 | +Default output is already `file:line:col: message [RuleName]` — one finding per line, no colors, no progress bars. A summary line ("Analysis failed with N weighted issues.") is always appended. |
| 145 | + |
| 146 | +Report formats: `txt`, `xml`, `html`, `md`, `sarif`. Multiple reports can be generated simultaneously: `-r txt:detekt.txt -r sarif:detekt.sarif`. |
| 147 | + |
| 148 | +Via Gradle (detekt-gradle plugin): |
| 149 | + |
| 150 | +```bash |
| 151 | +./gradlew detekt --console=plain |
| 152 | +``` |
| 153 | + |
| 154 | +## spotless (Gradle plugin) |
| 155 | + |
| 156 | +```bash |
| 157 | +# Check formatting |
| 158 | +./gradlew spotlessCheck --console=plain |
| 159 | + |
| 160 | +# Fix formatting |
| 161 | +./gradlew spotlessApply --console=plain |
| 162 | +``` |
| 163 | + |
| 164 | +Noise comes from Gradle, not Spotless itself — use `--console=plain` to suppress it. |
| 165 | + |
| 166 | +## AGENTS.md / CLAUDE.md Template |
| 167 | + |
| 168 | +Copy this into your project's AGENTS.md or CLAUDE.md: |
| 169 | + |
| 170 | +```markdown |
| 171 | +## Running Tools |
| 172 | + |
| 173 | +- Build: `./gradlew build --console=plain` |
| 174 | +- Tests: `./gradlew test --console=plain` |
| 175 | +- Tests (single): `./gradlew test --console=plain --tests "com.example.MyTest.myMethod"` |
| 176 | +- Lint (ktlint): `./gradlew ktlintCheck --console=plain` |
| 177 | +- Lint fix (ktlint): `./gradlew ktlintFormat --console=plain` |
| 178 | +- Static analysis (detekt): `./gradlew detekt --console=plain` |
| 179 | +- Format check (Spotless): `./gradlew spotlessCheck --console=plain` |
| 180 | +- Format fix (Spotless): `./gradlew spotlessApply --console=plain` |
| 181 | +``` |
0 commit comments