Android KTX   Part of Android Jetpack.

Android KTX is a set of Kotlin extensions that is part of the Android Jetpack family. It optimizes Jetpack and Android platform APIs for Kotlin use. The purpose of Android KTX is to make Android development with Kotlin more concise, pleasant, and idiomatic by leveraging Kotlin language features such as extension functions/properties, lambdas, named parameters, and parameter default values. Android KTX does not add any new features to the existing Android APIs.

To learn more about Android KTX, see our DevBytes video.

Getting Started

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

repositories {
    google()
}

Android KTX is organized into modules. Each module contains one or more packages.

When you use a module, include a dependency for each Android KTX artifact in your app's build.gradle file. Remember to append the version to the artifact. For example, if you use the core-ktx module, the fully-formed dependency will look something like this:

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

Modules

Android KTX is composed of the following Maven artifacts. For API reference documentation, click on the specific package name and see the Extension functions summary. Note that version numbers can change. To be sure you are using the latest available version, check the Google Maven Repository.

Module (artifact) Version Package
androidx.core:core-ktx 1.0.0 See all the core packages below.
androidx.fragment:fragment-ktx 1.0.0 androidx.fragment.app
androidx.palette:palette-ktx 1.0.0 androidx.palette.graphics
androidx.sqlite:sqlite-ktx 2.0.0 androidx.sqlite.db
androidx.collection:collection-ktx 1.0.0 androidx.collection
androidx.lifecycle:lifecycle-viewmodel-ktx 2.0.0 androidx.lifecycle
androidx.lifecycle:lifecycle-reactivestreams-ktx 2.0.0 androidx.lifecycle
android.arch.navigation:navigation-common-ktx 1.0.0-alpha06 androidx.navigation
android.arch.navigation:navigation-fragment-ktx 1.0.0-alpha06 androidx.navigation.fragment
android.arch.navigation:navigation-runtime-ktx 1.0.0-alpha06 androidx.navigation
android.arch.navigation:navigation-testing-ktx 1.0.0-alpha06 androidx.navigation.testing
android.arch.navigation:navigation-ui-ktx 1.0.0-alpha06 androidx.navigation.ui
android.arch.work:work-runtime-ktx 1.0.0-alpha10 androidx.work.ktx

The core module includes these packages:

Examples

Android KTX is used in the Sunflower demo app.

The examples below demonstrate a few of the Android KTX extension functions. They are grouped by module (artifact) name. See the complete package reference documentation for the full list of extension functions.

androidx.core:core-ktx

Kotlin

sharedPreferences.edit()
    .putBoolean("key", value)
    .apply()

Kotlin + Android KTX

sharedPreferences.edit {
    putBoolean("key", value)
}

Kotlin

view.viewTreeObserver.addOnPreDrawListener(
    object : ViewTreeObserver.OnPreDrawListener {
        override fun onPreDraw(): Boolean {
            viewTreeObserver.removeOnPreDrawListener(this)
            actionToBeTriggered()
            return true
        }
    }
)

Kotlin + Android KTX

view.doOnPreDraw {
     actionToBeTriggered()
}

androidx.sqlite:sqlite-ktx

Kotlin

db.beginTransaction()
try {
    // insert data
    db.setTransactionSuccessful()
} finally {
    db.endTransaction()
}

Kotlin + Android KTX

db.transaction {
    // insert data
}

androidx.fragment:fragment-ktx

Kotlin

supportFragmentManager
    .beginTransaction()
    .replace(R.id.my_fragment_container, myFragment, FRAGMENT_TAG)
    .commitAllowingStateLoss()

Kotlin + Android KTX

supportFragmentManager.transaction(allowStateLoss = true) {
            replace(R.id.my_fragment_container, myFragment, FRAGMENT_TAG)
        }

Feedback

We will continue to add new Kotlin extensions throughout Jetpack and we welcome your feedback. If you would like to report an issue or suggest a feature, please file a new issue at the Android KTX issue tracker.