Google launches Android KTX preview, a set of Kotlin extensions for developers

Google today introduced Android KTX, a set of extensions to help developers write Kotlin code for Android that is “more concise, idiomatic, and pleasant.” The company promises to iterate on the API “over the next few months” based on developer contributions and feedback.

Modern. Expressive. Safe.

Kotlin is concise while being expressive. It contains safety features for nullability and immutability, to make your Android apps healthy and performant by default.

 
At its I/O 2017 developer conference in May, Google announced Android officially supports the Kotlin programming language, in addition to Java and C++. Android Studio 3.0 with Kotlin support followed in May.

Currently in preview, Android KTX provides an API layer on top of the Android framework and Support Library — the former is available now on GitHub. Google promises to make the other parts of Android KTX that cover the Android Support Library available in upcoming Support Library releases.
To start using Android KTX in your Android Kotlin projects, add the following to your app module’s build.gradle file:

repositories {
google()
}

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

 
After you sync your project, the extensions will appear automatically in the IDE’s autocomplete list, and choosing any of them will add the necessary import statement to your file.

Google warns that the APIs are likely to change during the preview period. In other words, developers should not use it in their main Android projects, as there will be breaking changes before the stable version arrives. When the API has stabilized and Google commits to API compatibility, Android KTX will be released as part of the Android Support Library.

 
Android KTX uses package names that begin with androidx. Google also shared today that this is the new package name prefix for future versions of Android Support Library. The division between android.* and androidx.* is meant to help differentiate which APIs are bundled with the platform and which are static libraries for app developers that work across different versions of Android.
If you are interested in providing contributions and feedback on Android KTX, check out the “How to Contribute” section on GitHub. 


The portion of Android KTX that covers the Android framework is now available in our GitHub repo. We invite you to try it out to give us your feedback and contributions. The other parts of Android KTX that cover the Android Support Library will be available in upcoming Support Library releases.
Let's take a look at some examples of how Android KTX can help you write more natural and concise Kotlin code. 

Code Samples Using Android KTX
 
String to Uri
 
Let's start with this simple example. Normally, you'd call Uri.parse(uriString). Android KTX adds an extension function to the String class that allows you to convert strings to URIs more naturally. 

Kotlin
Kotlin with Android KTX
val uri = Uri.parse(myUriString)
val uri = myUriString.toUri()
Edit SharedPreferences
Editing SharedPreferences is a very common use case. The code using Android KTX is slightly shorter and more natural to read and write.
Kotlin
Kotlin with Android KTX
sharedPreferences.edit()
           .putBoolean(key, value)
           .apply()
sharedPreferences.edit { 
    putBoolean(key, value) 
}

 
Translating path difference

In the code below, we translate the difference between two paths by 100px. 

Kotlin
Kotlin with Android KTX
val pathDifference = Path(myPath1).apply {
   op(myPath2, Path.Op.DIFFERENCE)
}

val myPaint = Paint()

canvas.apply {
   val checkpoint = save()
   translate(0F, 100F)
   drawPath(pathDifference, myPaint)
   restoreToCount(checkpoint)
}

val pathDifference = myPath1 - myPath2

canvas.withTranslation(y = 100F) {
   drawPath(pathDifference, myPaint)
}
Action on View onPreDraw
This example triggers an action with a View's onPreDraw callback. Without Android KTX, there is quite a bit of code you need to write.
Kotlin
view.viewTreeObserver.addOnPreDrawListener(
       object : ViewTreeObserver.OnPreDrawListener {
           override fun onPreDraw(): Boolean {
               viewTreeObserver.removeOnPreDrawListener(this)
               actionToBeTriggered()
               return true
           }
       })
Kotlin with Android KTX
view.doOnPreDraw { actionToBeTriggered() }
 
There are many more places where Android KTX can simplify your code. You can read the full API reference documentation on GitHub. 

Getting Started
 
To start using Android KTX in your Android Kotlin projects, add the following to your app module's build.gradle file: 

repositories {
    google()
}

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

Then, after you sync your project, the extensions appear automatically in the IDE's auto-complete list. Selecting an extension automatically adds the necessary import statement to your file.
Beware that the APIs are likely to change during the preview period. If you decide to use it in your projects, you should expect breaking changes before we reach the stable version.

androidx: Hello World!

You may notice that Android KTX uses package names that begin with androidx. This is a new package name prefix that we will be using in future versions of Android Support Library. We hope the division between android.* and androidx.* makes it more obvious which APIs are bundled with the platform, and which are static libraries for app developers that work across different versions of Android.

What's Next?

Today's preview launch is only the beginning. Over the next few months, we will iterate on the API as we incorporate your feedback and contributions. When the API has stabilized and we can commit to API compatibility, we plan to release Android KTX as part of the Android Support Library.
We look forward to building Android KTX together with you. Happy Kotlin-ing!

Via VentureBeats, googleblog