Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Update docs for ktlint integration #124

Merged
merged 4 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/supported-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ Tool | Java | Android<br/>(Java) | Kotlin | Android<br/>(Kotlin)
[`FindBugs`](http://findbugs.sourceforge.net/) | :white_check_mark: | :white_check_mark: | — | —
[`Detekt`](https://github.com/arturbosch/detekt) | — | — | :white_check_mark: | :white_check_mark:
[`Android Lint`](https://developer.android.com/studio/write/lint.html) | — | :white_check_mark:️ | — | :white_check_mark:️
[`KtLint`\*](https://github.com/shyiko/ktlint) | — | — | ✖️ | ✖️

_\* Not supported [yet](https://github.com/novoda/gradle-static-analysis-plugin/issues?q=is%3Aopen+is%3Aissue+label%3A%22new+tool%22)_
[`KtLint`](https://github.com/shyiko/ktlint) | — | — | :white_check_mark:️ | :white_check_mark:️

For additional informations and tips on how to obtain advanced behaviours with the plugin and its tools, please refer to the
[advanced usage](advanced-usage.md) page.
Expand All @@ -25,7 +23,7 @@ For additional informations and tips on how to obtain advanced behaviours with t
* [PMD](tools/pmd.md)
* [Findbugs](tools/findbugs.md)
* [Android Lint](tools/android_lint.md)
* KtLint — _COMING SOON_
* [KtLint](tools/ktlint.md)
* [Example configurations](#example-configurations)

---
Expand All @@ -42,7 +40,9 @@ staticAnalysis {
checkstyle {}
pmd {}
findbugs {}
lintOptions {}
detekt {}
ktlint {}
}
```

Expand Down
58 changes: 58 additions & 0 deletions docs/tools/ktlint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# ktlint
[Ktlint](https://github.com/shyiko/ktlint) is a linter for Kotlin with a built-in formatter. It does not support Java. Adding
this tool only makes sense when you have Kotlin sources in your project. In fact, it will fail to run if you have 0 Kotlin
files.

## Table of contents
* [IMPORTANT: setup Ktlint](#important-setup-ktlint)
* [Configure Ktlint](#configure-ktlint)
---

## IMPORTANT: setup Ktlint

Unlike the other tools, the plugin **won't automatically add Ktlint** to your project. If you forget to do it, the plugin will
fail the build with an error.

In order to integrate Ktlint easily we choose to use the [Ktlint Gradle plugin](https://github.com/JLLeitschuh/ktlint-gradle/).
This plugin has a very good understanding of Android source sets and build flavors. You can refer to the
[official documentation](https://github.com/JLLeitschuh/ktlint-gradle/#how-to-use) for further details.

Note that you should _not_ add the `ktlint` closure to your `build.gradle`s, unlike what the official documentation says. The
`ktlint` closure in the `staticAnalysis` configuration gets applied to all Kotlin modules automatically.

In most common cases, adding Ktlint to a project boils down to these simple steps:

1. Add this statement to your root `build.gradle` project (change the version according to your needs):
```gradle
plugins {
id 'org.jlleitschuh.gradle.ktlint' version '5.0.0'
// ...
}
```
2. Add this statement to each Kotlin project's `build.gradle`s:
```gradle
plugins {
id 'org.jlleitschuh.gradle.ktlint'
// ...
}
```

## Configure Ktlint

Unlike other tools, Ktlint does not offer much configuration. By default, it applies
[Kotlin style guide](https://kotlinlang.org/docs/reference/coding-conventions.html) or
[Android Kotlin style guide](https://android.github.io/kotlin-guides/style.html).

To use Android style guide:

```gradle
ktlint {
android true
}
```

For other configuration options and adding custom rules, refer to the
[official guide](https://github.com/JLLeitschuh/ktlint-gradle/#configuration).

**Note:** Failures and threshold detection is handled by Static Analysis plugin. That is why `ignoreFailures = true` is set by
the plugin. Please do not manually override `ignoreFailures` property.