News
Android KTX Provides Kotlin Extensions for Easier Mobile Development
- By David Ramel
- February 6, 2018
"Kotlin on Android is here to stay, and we have big plans for it."
Those are the words of Florina Muntenescu, an Android developer advocate at Google, in announcing the preview of new extensions designed to ease the Kotlin coding experience for creating Android mobile apps.
Ever since Google anointed Kotlin as a first-class programming language for Android -- along with Java -- last May, Kotlin has been gaining in popularity and usage, including among core developers working on Android Studio.
Yesterday, the team announced the extensions, named Android KTX, for "even sweeter Kotlin development for Android."
That sweetness comes by providing more concise code that feels natural to those working in the Android ecosystem.
The extensions are provided via APIs atop the base Android framework (available now) and to the Support Library (coming soon).
The core-ktx packages on GitHub now show items ranging from androidx.animation to androidx.view.
The "androidx.*" is a new designation that indicates static libraries that work across different versions of Android, as opposed to APIs bundled with the platform that use the standard android.* nomenclature.
The extensions provide handy autocomplete coding shortcuts for many different operations. While sometimes they just save a few seconds or so by letting coders shorten some small constructs, they can also save more time by turning the old way of triggering an action with a View's onPreDraw callback from this:
view.viewTreeObserver.addOnPreDrawListener(
object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
viewTreeObserver.removeOnPreDrawListener(this)
actionToBeTriggered()
return true
}
})
into this:
view.doOnPreDraw { actionToBeTriggered() }
To use Android KTX, developers need only tweak their build.gradle file to add androidx.core:core-ktx:0.1 as an implementation dependency, after which the extensions automatically appear in the IDE's auto-complete list.
The Android team is looking for developers who want to do just that to help them produce the production product. When finalized, Android KTX will be included in the Android Support Library.
"We're still in preview," Muntenescu said. "The API is not stable yet, so your feedback is valuable and can shape the library. This is just the beginning of Android KTX. We're working on extensions for support library and architecture components. Kotlin on Android is here to stay, and we have big plans for it."
Yesterday's announcement prompted this comment on a Hacker News post: "Still working in Java on Android. Have a feeling that I will be left behind soon."
About the Author
David Ramel is an editor and writer at Converge 360.