Android KTX is a set of Kotlin extensions that are included with Android
Jetpack. KTX extensions provide concise, idiomatic Kotlin to Jetpack
and Android platform APIs. To do so, these extensions leverage several Kotlin
language features, including the following:

Extension functions

Extension properties

Lambdas

Named parameters

Parameter default values

As an example, when working with
SharedPreferences, you must
create an editor
before you can make modifications to the preferences data. You must also apply
or commit those changes when you are finished editing, as shown in the following
example:

Kotlin lambdas are a perfect fit for this use case. They allow you to take a
more concise approach by passing a block of code to execute after the editor is
created, letting the code execute, and then letting the SharedPreferences API
apply the changes atomically.

Here's an example of one of the Android KTX Core functions,
SharedPreferences.edit,
which adds an edit function to SharedPreferences. This function takes an
optional boolean flag as its first argument that indicates whether to commit
or apply the changes. It also receives an action to perform on the
SharedPreferences editor in the form of a lambda.

The caller can choose whether to commit or apply the changes. The action
lambda is itself an anonymous extension function on SharedPreferences.Editor
which returns Unit, as indicated by its signature. This is why inside the
block, you are able to perform the work directly on the
SharedPreferences.Editor.

Finally, the SharedPreferences.edit() signature contains the inline keyword.
This keyword tells the Kotlin compiler that it should copy and paste (or
inline) the compiled bytecode for the function each time the function is used.
This avoids the overhead of instantiating a new class for every action each
time this function is called.

This pattern of passing code using lambdas, applying sensible defaults that can
be overridden, and adding these behaviors to existing APIs using inline
extension functions is typical of the enhancements provided by the Android KTX
library.

Use Android KTX in your project

To start using Android KTX, add the following dependency to your project's
build.gradle file:

repositories {
google()
}

Android KTX is organized into modules, where each module contains one or more
packages.

You must include a dependency for each module artifact in your app's
build.gradle file. Remember to append the version number to the artifact. For
example, if you use the core-ktx module, the fully-formed dependency looks
similar to the following:

dependencies {
implementation 'androidx.core:core-ktx:1.0.1'
}

Modules

Android KTX contains a single core module that provides Kotlin extensions for
common framework APIs and several domain-specific extensions.

With the exception of the core module, all KTX module artifacts replace the
underlying Java dependency in your build.gradle file. For example, you can
replace a androidx.fragment:fragment dependency with
androidx.fragment:fragment-ktx. This syntax helps to better manage
versioning and does not add additional dependency declaration requirements.

Core KTX

The Core KTX module provides extensions for common libraries that are part of
the Android framework. These libraries do not have Java-based dependencies that
you need to add to build.gradle.

Here's a list of the packages that are contained in the Core KTX module:

You can also bind to a ViewModel in one line by using the viewModels and
activityViewModels property delegates:

// Get a reference to the ViewModel scoped to this Fragment
val viewModel by viewModels<MyViewModel>()
// Get a reference to the ViewModel scoped to its Activity
val viewModel by activityViewModels<MyViewModel>()

Palette KTX

Dependency:androidx.palette:palette-ktx:$version. Be sure to define
$version or replace it with a literal value.

ViewModel KTX

The ViewModel KTX library provides a viewModelScope() function that makes it
easier to launch coroutines from your ViewModel. The
CoroutineScope
is bound to Dispatchers.Main and is automatically cancelled
when the ViewModel is cleared. You can use viewModelScope() instead of
creating a new scope for each ViewModel.

As an example, the following viewModelScope() function launches a coroutine
that makes a network request in a background thread. The library handles all of
the setup and corresponding scope clearing:

As an example, assume a database with a small list of users. In your app, you
load the database into memory and then display user data in your UI. To achieve
this, you might use RxJava.
The Room Jetpack component can retrieve
the user list as a Flowable. In this scenario, you must also manage the Rx
publisher subscription across the life of your fragment or activity.

With LiveDataReactiveStreams, however, you can benefit from RxJava and its
rich set of operators and work-scheduling capabilities while also working with
the simplicity of LiveData, as shown in the following example: