Tuesday, October 23, 2018

Introducing Android Ktx: Fifty-Fifty Sweeter Kotlin Evolution For Android

Posted yesteryear Jake Wharton (@JakeWharton), Florina Muntenescu (@FMuntenescu) & James Lau (@jmslau)

Today, nosotros are announcing the preview of Android KTX - a laid of extensions designed to brand writing Kotlin code for Android to a greater extent than concise, idiomatic, as well as pleasant. Android KTX provides a overnice API layer on top of both Android framework as well as Support Library to brand writing your Kotlin code to a greater extent than natural.

The portion of Android KTX that covers the Android framework is directly available inward our GitHub repo. We invite y'all to endeavor it out to give us your feedback as well as contributions. The other parts of Android KTX that encompass the Android Support Library volition last available inward upcoming Support Library releases.

Let's accept a aspect at roughly examples of how Android KTX tin deal y'all write to a greater extent than natural as well as concise Kotlin code.

Code Samples Using Android KTX

String to Uri

Let's start amongst this uncomplicated example. Normally, you'd telephone phone Uri.parse(uriString). Android KTX adds an extension portion to the String degree that allows y'all to convert strings to URIs to a greater extent than naturally.

Kotlin
Kotlin amongst Android KTX
val uri = Uri.parse(myUriString)
val uri = myUriString.toUri()

Edit SharedPreferences

Editing SharedPreferences is a really mutual purpose case. The code using Android KTX is slightly shorter as well as to a greater extent than natural to read as well as write.

Kotlin
Kotlin amongst Android KTX
sharedPreferences.edit()            .putBoolean(key, value)            .apply() 
sharedPreferences.edit {      putBoolean(key, value)  }    

Translating path difference

In the code below, nosotros interpret the departure betwixt ii paths yesteryear 100px.

Kotlin
Kotlin amongst 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 instance triggers an activeness amongst a View's onPreDraw callback. Without Android KTX, in that location is quite a flake of code y'all need to write.

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

There are many to a greater extent than places where Android KTX tin simplify your code. You tin read the full API reference documentation on GitHub.

Getting Started

To start using Android KTX inward your Android Kotlin projects, add together the next to your app module's build.gradle file:

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

Then, later on y'all sync your project, the extensions seem automatically inward the IDE's auto-complete list. Selecting an extension automatically adds the necessary import contention to your file.

Beware that the APIs are probable to alter during the preview period. If y'all determine to purpose it inward your projects, y'all should aspect breaking changes earlier nosotros accomplish the stable version.

androidx: Hello World!

You may discovery that Android KTX uses bundle names that laid out amongst androidx. This is a novel bundle refer prefix that nosotros volition last using inward time to come versions of Android Support Library. We promise the partition betwixt android.* as well as androidx.* makes it to a greater extent than obvious which APIs are bundled amongst the platform, as well as which are static libraries for app developers that run into dissimilar versions of Android.

What's Next?

Today's preview launch is exclusively the beginning. Over the side yesteryear side few months, nosotros volition iterate on the API every bit nosotros contain your feedback as well as contributions. When the API has stabilized as well as nosotros tin commit to API compatibility, nosotros programme to unloose Android KTX every bit portion of the Android Support Library.

We aspect frontward to edifice Android KTX together amongst you. Happy Kotlin-ing!

Related Post

Introducing Android Ktx: Fifty-Fifty Sweeter Kotlin Evolution For Android
4/ 5
Oleh