Kotlin: The new standard language for Android app development

30. August 2019 - from Yannick Pulver

Kotlin has become the standard programming language for the development of native Android apps. With the Five up project, we developed an Android app for the first time using only Kotlin. In this blog, we would like to answer some questions that come up from time to time on this topic.

What is Kotlin?

Kotlin is a programming language, like Java, Python, C++ and co. The Kotlin code can be converted to Java Virtual Machine (JVM) bytecode, JavaScript source code or even directly into machine code. In less technical terms, this means that the language can be used to develop programs for different platforms. Kotlin is developed by JetBrains, who are also known for their development environments such as IntelliJIDEA, among others. Compared to the programming language Java, which appeared in 1995, Kotlin is still rather new (year of publication 2011).

Since the announcement at Google I/O 2017, Kotlin is officially supported for Android development. Since this summer, Kotlin is Google's preferred language for Android development, replacing Java. No wonder, because already at Droidcon 2018, hardly any Java code was shown on the speakers' slides. This clearly shows that the support of Kotlin is very much welcomed, even celebrated, by the Android community.

How does Kotlin differ from Java? What are the advantages and disadvantages compared to Java?

Kotlin helps represent the essential code a little better than Java. The omission of semicolons, explicit type declarations and the "new" keyword are only three of many notable differences. In the following sections, some changes are presented in more detail.

Null Handling

When developing with Java, it is possible to assign "null" to each variable of each type. In principle, null stands for "nothing". From a short-sighted point of view, this seems practical in certain cases. To keep this behaviour under control, it is sometimes necessary to check whether the variables are null or non-null. This prompts to fill the code with if loops. If these queries are not made, an error of the type "NullPointerException" may occur. (Every Java developer has certainly experienced this).

In Kotlin, however, null values are controlled by the type system. This means that a variable generally cannot be null, unless it is specifically marked as "nullable" with a question mark (see code snippet). This makes the if loops mentioned unnecessary in many scenarios. And if a check is needed, Kotlin provides some practical scope functions such as "let".

Nice to know: The Elvis operator can be used to bypass an if loop when assigning.


Data classes

Often classes are created that only contain data. In architecture concepts such as MVVM or MVP, such classes are labelled as "model". In Java, such classes contain many lines of repetitive code with so-called "getters" and "setters", which serve to decapsulate the variables. Kotlin simplifies the creation of such models with data classes. With only one line of code, the much larger Java class can be replaced.

Likewise, everything unnecessary is dispensed with in the application of the class. Instead of using "getters", the variable name is accessed directly. This definitely helps improve the readability of the code.

Extension Functions

With the help of extension functions, the functionality of classes can be extended. This extension is done without changing the class itself. This is less suitable for own classes, but can be used well for non-influenceable classes such as Android classes. We have used these extension functions, for example, to beautify the setting of the "Visibility". This helps make the code fundamentally more readable and to outsource less readable, repetitive parts to such extension functions.

However, these functions should be used with caution. Too many extensions can lead to the code only being well understood by its developer.

The application of these extension functions is as follows:

These three described changes in Kotlin are only a few of many that make the language very pleasant to develop in compared to Java. The official Kotlin documentation offers many more examples of how the language can be used sensibly: kotlinlang.org.

Why did we use Kotlin for Five up?

As already briefly mentioned: As an Android developer, it is currently difficult to get around Kotlin. On the Google resources, Java code examples are only presented secondarily or not at all. For the further development of existing projects, it did not seem to make sense to write only individual classes in Kotlin. Therefore, the fresh development of Five up lent itself perfectly to Kotlin.

Half a year after the launch: Has the choice proved successful?

At the beginning, there was a lot of code that was programmed in "Java-like Kotlin". After a short time, however, we were able to make good use of Kotlin's capabilities. In the course of the project, we also noticed that well-known libraries were increasingly switching to Kotlin and making use of functionalities such as extension functions.

The Android Studio development environment helps a lot because it automatically converts Java code to Kotlin when it is copied into a Kotlin file. The game also works the other way round. One has the possibility to look at the Java code of the Kotlin file. This can help here and there to build up a better understanding of Kotlin.

For Five up, we also relied on the Android Architecture Components. This collection of libraries defines the architecture of Android apps preferred by Google. Although these libraries can also be used with Java, the handling with Kotlin is much more intuitive.

"With great power comes great responsibility."

Stan Lee

So to answer the question: Yes! Using Kotlin was the right move. Especially since Kotlin is now considered the primary language for Android. But as with everything, there also are points that speak against Kotlin. Some of them are summarised in the following article: Why Kotlin Sucks

Are there differences in "maintenance" compared to Java?

Kotlin is being actively developed, so you can enjoy new functionalities and improvements on a regular basis. In addition, one has the possibility to contribute to Kotlin as an open-source contributor.

We use a similar architecture to the one we use for Java projects. The maintenance of the projects therefore actually remains the same. The advantage is that developers quickly find their way into the project, regardless of whether Java or Kotlin is used.

Will Kotlin prevail in Android app development? Will we do new projects only with Kotlin?

Kotlin has already clearly established itself. Going back to Java does not seem to be a good idea in the long term. At the latest when Google's libraries are only developed for Kotlin in the future. We must now continue to try to integrate Kotlin as well as possible in existing and future projects.

Will we "migrate" old projects to Kotlin?

Migrating old projects does not seem to make sense. However, we set ourselves the goal of writing new features only with Kotlin. This may lead to some older projects being entirely in Kotlin over time. In the short term, however, we will not be able to avoid developing with Java. But now that we know the advantages of Kotlin, we will certainly try to use Kotlin rather than Java.

We just noticed that you surf with Internet Explorer. Unfortunately, our website does not look so nice with it.

You want to know why that is?
We have written about it.

Blog

You need help with the changeover?
Get in touch. We are happy to help

Contact

Install a new browser?
There's lots of choice.

Browser