3

How to start with KTX for android?

Jay Dwivedi
  • 396
  • 1
  • 14

3 Answers3

6

Android KTX - a set of extensions designed to make writing Kotlin code for Android more concise, idiomatic, and pleasant. Android KTX provides a nice API layer on top of both Android framework and Support Library to make writing your Kotlin code more natural.

For example ,

To parse String to URI in Kotlin (Traditional Way )

We do

val uri = Uri.parse(myUriString)

and by using KTX, we can simplify above code to

val uri = myUriString.toUri()

You can refer this blog for more details

https://android-developers.googleblog.com/2018/02/introducing-android-ktx-even-sweeter.html?m=1

Because of less boiler plate code , i think its going to gain popularity.

Suraj Nair
  • 1,251
  • 10
  • 11
4

What is KTX (From developer blog)

Android KTX - a set of extensions designed to make writing Kotlin code for Android more concise, idiomatic, and pleasant. Android KTX provides a nice API layer on top of both Android framework and Support Library to make writing your Kotlin code more natural.

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. (Packages details)

Watch Introducing Android KTX: Even Sweeter Kotlin Development for Android.


Why gaining popularity

As developers, it's almost always desirable to be able to write shorter and more readable code without sacrificing performance, stability, or control. 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.

For example

enter image description here


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.

Pankaj Kumar
  • 78,055
  • 25
  • 161
  • 180
2
        repositories {
            google()
        }

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

example:        
         Kotlin :
            sharedPreferences.edit()
                       .putBoolean(key, value)
                       .apply()

            Kotlin with Android KTX :
            sharedPreferences.edit { 
                putBoolean(key, value) 
            }
Tarun konda
  • 1,075
  • 9
  • 18