The Kotlin language has come a long way since it was officially approved by Google. Not only will Android Studio 3.0 automatically support Kotlin by default, but there is one more surprise.

Google released a preview version of the Android KTX last week.

What is the Android KTX? In simple terms, it is a Support Library similar to the Support Library, which provides some very user-friendly APIs to help users write Kotlin code more concisingly and programmatically.

Here are a few examples to see how our Kotlin code has changed since we started using the Android KTX framework, and whether it’s cleaner and more natural.

String to Uri

Converting a String to a Uri using Kotlin looks like this:

val uri = Uri.parse(myUriString)
Copy the code

The Android KTX framework extends String to add more methods, so this line of code reads like this:

val uri = myUriString.toUri()
Copy the code

That way, you don’t have to introduce a separate Uri class, does it feel like a flow?

SharedPreferences

Kotlin:

sharedPreferences.edit()
           .putBoolean(key, value)
           .apply()
Copy the code

Kotlin with Android KTX

sharedPreferences.edit { 
    putBoolean(key, value) 
}
Copy the code

Canvas path operation

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)
}
Copy the code

Kotlin with Android KTX

val pathDifference = myPath1 - myPath2

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

The View onPreDraw action

Kotlin:

view.viewTreeObserver.addOnPreDrawListener(
       object : ViewTreeObserver.OnPreDrawListener {
           override fun onPreDraw(a): Boolean {
               viewTreeObserver.removeOnPreDrawListener(this)
               actionToBeTriggered()
               return true}})Copy the code

Kotlin with Android KT:

view.doOnPreDraw { actionToBeTriggered() }
Copy the code

The list goes on. As you can see, using the Android KTX greatly simplifies our Kotlin code.

As long as you introduce the Android KTX dependency configuration in your app/build.gradle file:

Repository {Google ()} dependencies {// Implementation for Framework API 'Androidx. Core :core- KTX :0' . }Copy the code

At development time, the IDE will automatically complete the relevant code according to your code intelligence.

It’s also worth noting that the Android KTX uses a brand new package name prefix: AndroidX. Google hopes that when it provides the Android Support Library package in the future, developers will be able to clearly distinguish between the use of the * Android. ** and * Androidx. ** apis. It’s a thoughtful design detail, and Google thought it through.

The Android KTX source code is currently in GitHub repository and is still being improved:

Github.com/android/and…

About me: Yifeng, blog address: Yifeng. Studio /, Sina Weibo: IT Yifeng

Scan the QR code on wechat, welcome to follow my personal public account: Android Notexia

Not only share my original technical articles, but also the programmer’s workplace reverie