What Does Google’s Support for Kotlin Mean for Developers?
At Google I/O 2017, Google announced that Android Studio 3.0 will officially support Kotlin and this announcement also brought a lot of attentions to Kotlin. Kotlin, whose primary sponsor is JetBrains, is a statically typed programming language for Java Virtual Machine. Programmer can use Kotlin and Java at the same time. We will focus on how to use Kotlin to develop Android apps in this series instead of discussing subjective questions like is Kotlin better than Java.
Install Kotlin’s plugin
Before Android Studio 3.0 is officially released, we still need to install plugin to use Kotlin.
Preference -> plugin -> install Kotlin
Now we write a simple app to briefly introduce Kotlin.
Now we rewrite the code with Kotlin. First, we have to configure the project.
cmd + shift + a -> search kotlin -> configure kotlin in project
P.S. You can use cmd+shift+a -> search kotlin -> Convert Java File to Kotlin File at the beginning to help you understand how to use Kotlin
As you can see, now we can use property to set text of TextView instead of calling setText(). However, there is no big difference between Java style and Kotlin style so for. Actually, Kotlin can make this code more simple. Put following code to your build.grandle of module.
apply plugin: ‘kotlin-android-extensions’
Now you can rewrite the code like below.
As you can see, we don’t need to use findViewById() anymore. We can just use the id of View directly and the code is simpler.
This article is just a starter and we will introduce more features and usages of Kotlin in the future.