Android KTX

Two weeks ago, Google announced a set of extensions designed to make writing kotlin code more concise, idiomatic and pleasant called Android KTX. The objective here seems to be help developers to write less code! You can check the repo of Android KTX here.

Getting Started

To start using Android KTX you only need to put this on your build.gradle:

repositories {
    google()
}

dependencies {
    // Android KTX for framework API
    implementation 'androidx.core:core-ktx:0.1'
    ...
}

Now, lets start comparing kotlin with and without Android KTX:

Shared Preferences

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

Shared Preferences with Android KTX

sharedPreferences.edit { 
    putBoolean(key, value) 
}

String to URI

val uri = Uri.parse(myUriString)

String to URI with Android KTX

val uri = myUriString.toUri()

Canvas

val pathDifference = Path(myPath1).apply {
    op(myPath2, Path.Op.DIFFERENCE)
}
canvas.apply {
  val checkpoint = save()
  translate(0F, 100F)
  drawPath(pathDifference, myPaint)
  restoreToCount(checkpoint)
}

Canvas with Android KTX

val pathDifference = myPath1 - myPath2
canvas.withTranslation(y = 100F) {
    drawPath(pathDifference, myPaint)
}

There's a lot of more places you can simplify your code. You can read the full documentation here. You can also contribute to Android KTX with new ideas, giving your feedback, reporting defects and requesting features.

androidx

Google is now giving androidx. names to its prefix packages as you can see at Android KTX. It is because they have the intention to use it in their future versions of Android Support Library to differentiate which APIs are bundled with the platform and which are static libraries for app developers that work across differents versions of Android.