The new extentions are here

Android KTX makes Kotlin coding an absolute delight

Eirini-Eleni Papadopoulou
Kotlin
© Shutterstock / By Bakhtiar Zein  

On February 5th, Android Developers announced the preview of Android KTX, a set of extensions designed to make your Kotlin coding for Android as smooth as a summer’s sail.

Kotlin is one of the most popular kids on the playground! It did not take long for the community to become fascinated with this language and Google’s announcement in May 2017 on making Kotlin an officially supported language for developing apps, made it hit the top of the tops!

It doesn’t really matter that Kotlin lost to C in the run for the number one programming language for 2017, according to TIOBE Index. Kotlin conquers new ground in the programming world every day, becoming a real winner in our hearts (and our coding platforms!).

Kotlin, first developed in 2012, is still a baby in the programming ecosystem, however, it already has attributes and features that surpass many of its older cousins. Kotlin is officially described as concise, drastically reducing the amount of boilerplate code; safe, because it avoids entire classes of errors such as null pointer exceptions; interoperable for leveraging existing libraries for JVM, Android, and the browser; and tool-friendly thanks to its capability to choose any Java IDE or build from the command line.

If you want to learn more about how to get started with Kotlin, visit the official website. And for those of you who are already in love with it, here are some tips & tricks presented by JetBrains’ Dmitry Jemerov for JAXenter readers.

But let’s have a closer look at Android KTX.

SEE ALSO: Kotlin Team AMA highlights: Kotlin vs. Java, “under construction” features and more

Delighted coding with you, sir!

Android KTX provides a platform for smoother Kotlin coding. According to the official release announcement, the extensions are promised to make coding with Kotlin more “concise, idiomatic, and pleasant”. The extensions provide an API layer on top of both Android framework and Support Library.

To give you a small example, in the code below you see the translation of the difference between two paths by 100px.

Kotlin

 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)
}
 

Kotlin with Android KTX

 val pathDifference = myPath1 - myPath2

canvas.withTranslation(y = 100F) {
   drawPath(pathDifference, myPaint)
} 

Time to get started!

If you are eager to start using Android KTX in your Android Kotlin projects, the only thing you need to do is 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'
    ...
} 

Simple as that, 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.

However, you should be aware that the APIs are likely to change during the preview period. As the developers stated, you should expect breaking changes before a stable version of Android KTX is reached.

Can’t wait to try it out? We wish you a great time coding and don’t forget to share your experiences with the JAXenter community!

Author
Eirini-Eleni Papadopoulou

Eirini-Eleni Papadopoulou

All Posts by Eirini-Eleni Papadopoulou

Eirini-Eleni Papadopoulou is an assistant editor for JAXenter.com

Comments
comments powered by Disqus