(#) Use KTX extension function
!!! WARNING: Use KTX extension function
This is a warning.
Id
: `UseKtx`
Summary
: Use KTX extension function
Severity
: Warning
Category
: Productivity
Platform
: Android
Vendor
: Android Open Source Project
Feedback
: https://issuetracker.google.com/issues/new?component=192708
Since
: 8.9.0 (March 2025)
Affects
: Kotlin and Java files
Editing
: This check runs on the fly in the IDE editor
Implementation
: [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/UseKtxDetector.kt)
Tests
: [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/UseKtxDetectorTest.kt)
The Android KTX libraries decorates the Android platform SDK as well as
various libraries with more convenient extension functions available
from Kotlin, allowing you to use default parameters, named parameters,
and more.
!!! Tip
This lint check has an associated quickfix available in the IDE.
(##) Options
You can configure this lint checks using the following options:
(###) remove-defaults
Whether to skip arguments that match the defaults provided by the extension.
Extensions often provide default values for some of the parameters. For example:
```kotlin
fun Path.readLines(charset: Charset = Charsets.UTF_8): List { return Files.readAllLines(this, charset) }
```
This lint check will by default automatically omit parameters that match the default, so if your code was calling
```kotlin
Files.readAllLines(file, Charset.UTF_8)
```
lint would replace this with
```kotlin
file.readLines()
```
rather than
```kotlin
file.readLines(Charset.UTF_8
```
You can turn this behavior off using this option.
Default is true.
Example `lint.xml`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~xml linenumbers
<lint>
<issue id="UseKtx">
<option name="remove-defaults" value="true" />
</issue>
</lint>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(###) require-present
Whether to only offer extensions already available.
This option lets you only have lint suggest extension replacements if those extensions are already available on the class path (in other words, you're already depending on the library containing the extension method.)
Default is true.
Example `lint.xml`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~xml linenumbers
<lint>
<issue id="UseKtx">
<option name="require-present" value="true" />
</issue>
</lint>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(##) Example
Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
src/test/pkg/test.kt:5:Warning: Use the KTX extension function
String.htmlEncode instead? [UseKtx]
val html = TextUtils.htmlEncode("Is x > y ?")
----------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here is the source file referenced above:
`src/test/pkg/test.kt`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
package test.pkg
import android.text.TextUtils
fun test() {
val html = TextUtils.htmlEncode("Is x > y ?")
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can also visit the
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/UseKtxDetectorTest.kt)
for the unit tests for this check to see additional scenarios.
(##) Suppressing
You can suppress false positives using one of the following mechanisms:
* Using a suppression annotation like this on the enclosing
element:
```kt
// Kotlin
@Suppress("UseKtx")
fun method() {
problematicStatement()
}
```
or
```java
// Java
@SuppressWarnings("UseKtx")
void method() {
problematicStatement();
}
```
* Using a suppression comment like this on the line above:
```kt
//noinspection UseKtx
problematicStatement()
```
* Using a special `lint.xml` file in the source tree which turns off
the check in that folder and any sub folder. A simple file might look
like this:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<issue id="UseKtx" severity="ignore" />
</lint>
```
Instead of `ignore` you can also change the severity here, for
example from `error` to `warning`. You can find additional
documentation on how to filter issues by path, regular expression and
so on
[here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).
* In Gradle projects, using the DSL syntax to configure lint. For
example, you can use something like
```gradle
lintOptions {
disable 'UseKtx'
}
```
In Android projects this should be nested inside an `android { }`
block.
* For manual invocations of `lint`, using the `--ignore` flag:
```
$ lint --ignore UseKtx ...`
```
* Last, but not least, using baselines, as discussed
[here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).