Kotlin, What’s New to Android Development

Kotlin, another programming language for mobile applications developments, is not recent news since it first appeared in 2011. But then the stable release was available in 2017, making it usable for developers, young and old. It’s currently supported by the Android Studio IDE v3.0, the IDe famous for developing Android applications using Java.

Installing the Kotlin plugin on Android through Prefences and then Plugins section

You need to download the Kotlin plugin to the IDE to enable the support. Once it’s downloaded and the setup is complete, you can create a new project with Kotlin support. You might feel familiar with the code to be the same with Java. The flow of creating Android applications remains unchanged, but it is the way these codes were written definitely feels different. One of the big changes when using Kotlin was the absence of semicolons (which can be confusing sometimes). It’s a force of habit of mine to add semicolons at the end of code lines. Also with declaring variables almost feel like using JavaScript, where the actual type of the variable comes after the “=”.

Kotlin Benefits

With new changes comes with new and improved ways of developing Android applications. I will list just a few things that surprised me when I was trying to build my first app using Kotlin:

No more findViewByIds

Say goodbye to the usual view bindings of Java, which is a tedious approach. Calling the findViewById for each views on the layout can take up a bunch of code lines. I usually go with ButterKnife, a library which binds the view to a reference with at least 2 lines of code. In Kotlin, you can call the view id right away. The only requirement is to import the xml, and the view Id can be reference immediately. Simple yet effective.

NullPointerException != null anymore

Many crashes tend to happen because of the NullPointerException, this tells that the variable used on your application has no value, not even zero or an empty space, nothing. This can be quite a hassle when you found this application during testing, even on the production stage of the app.

But in Kotlin, a safe call can be initialized in the variable, making sure that the exception is never thrown, meaning no more unexpected crashes. This is achieved by adding a “?” at the variable initialization, i.e. var firstName : String?. In cases where a null value is assigned to the variable, the code will continue to run, although the empty value should handle anyway. A simple technique to prevent a major crash from happening.

Extension functions

With Kotlin you can extend the class’ functions, literally, just put another function that you feel is needed by that class. Although this is not a new idea, since it was already present on C#, this way the code lines you created is much shorter compared to creating a new function that utilizes that class on Android. This also shortens the line of codes in your project.

Smart Casts

Casting the wrong variable or object type to a value will throw an exception. You can check if the value is an instance of that by calling the “instanceof”, but if you want to use that value you have to cast the type you want.

In Kotlin, a checker “is” determines if the variable is of a specific type/object, and is found on an if statement. If the condition returns true, then that value can be accessed right away. No more redundant casting is needed.

Java and Kotlin working together

Since Java and Kotlin uses the same IDE to develop Android applications, Kotlin can support java codes and classes, and vice versa. Java codes can also be converted to Kotlin, making it easier for Java veterans to learn and understand the new language.

Kotlin Cons

Limited Documentation and Library support

As of now, the documentation and libraries found on the internet are not as much as big compared to Java development. Although the rapid growth of development for Kotlin increases, still the scale of support for Kotlin is not big compared to Java. That is expected for a new programming language, it will take time to develop new ways and libraries usable for other developers. Applications with complex functionalities are virtually impossible to develop using Kotlin, for now.

Conclusion

Basically, Kotlin is another tool for developing Android applications, which feels a lot like Swift for iOS that plays well with Java. It has a satisfying feeling for developers to see that the lines of codes used are very short compared to Java. It has some good things as well as bad things.

For experienced developers, shifting to a new programming language may be considered as roadblocks, but just a little one since it has some similarities to Java. For developers who want to take a dip in this new trend like me, documentations are found on the web that will help create your first Kotlin app.

Tope Mateo

Tope is a Junior Mobile App Developer at Project Assistant who specialized in finding and fixing bugs on both frontend and backend for mobile apps. Sometimes he would go deeper on finding these bugs on the server side just to make sure that the mobile application is running smoothly. He is shy and introverted, and with 2 cups of coffee he manages to hold up on the daily grind ( 3 if you include overtime hours). His hobbies include reading, eating, Gunpla building, playing/watching video games and watching movies.