Google announced support for Kotlin in Android at Google I/O 2017, but should Kotlin be a preferred choice over the 20 years old programming language Java? Let’s find out.

About Kotlin and its rise

Kotlin has been made the official programming language and is part of Android Studio version 3.0 onwards. In a short span of time, the language has grabbed so much attention that folks have begun addressing whether Java can hold its mastery for Android application development or whether this decades-old language would get replaced by Kotlin in the Android world.

Kotlin can possibly change application development paradigm because It is rather more developer friendly and considerably enhancing code readability because it eliminates several of Java’s complexities.

Why you should move to Kotlin?

Concise

Kotlin eliminates null references and it doesn’t have checked exceptions – both of which allow us to get rid of boilerplate code too, resulting in cleaner and more readable code.

Now let’s take an example of one food ordering app called FoodZy. Its menu item’s POJO class in Java look like:

Kotlin app development letsnurture

And in Kotlin will look like:

Kotlin app

Using Kotlin’s Data Class concept.

findViewById is No More

In Kotlin Android extensions allows you to import the reference of the View into an Activity file, so you can then work with the View because now it’s part of that Activity. This means no longer you need to identify each View using findViewById, which can transform code such as:

kotlin development

Into the much more succinct:

kotlin development letsnurture

Android KTX

Android KTX is a set of Kotlin extensions that are part of the Android Jetpack family. The motivation behind Android KTX is to make Android development with Kotlin more brief, pleasant, and idiomatic by utilizing Kotlin language features such as extension functions/properties, lambdas, named parameters, and parameter default values. Hence, Android KTX does not add any new features to the existing Android APIs.

It looks like the aim KTX is to continue with the advantages that kotlin brings to our project i.e.  less code, more fun and a simpler to understand the project.

Presently, the library is just in preview so it is probably going to change  and it will most likely up offering additional capabilities when it is released. So let’s talk about how Android KTX will change the app development paradigm.

SQLite Cursor

There are a group of functions available for the Cursor class. For each of the function we have three different methods to get values as below:

  • The first function returns a non-null type
  • The second function returns a data type (or null) using the given column name
  • The third function returns a data type (or null) using the given index

kotlin development letsnurture

Text

In most of the applications, we used text somewhere throughout the project and thankfully, KTX provides some extension functions when it comes to these parts. For text, we basically have some functions available for the SpannableStringBuilder class.

For example, after instantiating a Builder instance we can use the build methods to append some bold text:

kotlin app development letsnurture

And there are many more awesome extensions which simplify and reduce our code. I’m excited to use these in my projects from now and am looking forward to seeing what else gets added soon.

If you wish to know more about Kotlin app development, kindly share your views with us. For enquiries on Kotlin app development, write us at enquiry@letsnurture.com

Happy Coding!

Want to work with us? We're hiring!