As developers, it’s almost always desirable to be able to write shorter and more readable code without sacrificing performance, stability, or control. Many Android devs have adopted Kotlin to replace Java because it delivers on those points and and many others. Even though Kotlin’s syntax and language features make it well-suited to Android, it’s still not designed specifically for Android, so there are plenty of platform-specific patterns that require a lot of boilerplate code. To that end, a new extension library called Android KTX was released in preview form today with a focus on wrapping a lot of that boilerplate code.
The purpose of these extension methods is to abstract away large chunks of commonly implemented code. Google’s blog post gives a series of examples ranging from single lines of code that were shortened by just a few characters to become more readable, to function declarations of several lines in length that could be cut down to just a single line. These extensions effectively reproduce existing code blocks in a much shorter format.
Android KTX is initially launching with just methods to support the Android framework. At a later date, it will grow to cover parts of the Android Support Library, as well. The list of packages and API documentation, and the source code for the product can be found on the Android KTX github repository.
It’s important to note that this is still a preview release and Google’s blog post notes that APIs are likely to change until the library leaves preview status. In other words, be careful using it in projects because they are likely to break as the API changes. Nevertheless, it might be interesting to experiment with and give feedback to the team if you find ways to improve the library or new extensions to suggest. Check out the official blog post for more examples and setup instructions if you want to try out Android KTX.