Kotlin for Android Developers in 2019 | Chapter 2 : Quick Dive Into Kotlin!

Hope you are aware of the awesomeness of Kotlin, If not, check out my previous article Kotlin for Android Developers in 2019 | Chapter 1 : Kotlin is f-Awesome!

1. Setting-up Android Studio for Kotlin

You need to install the Kotlin Plugin if you are still running the previous versions of Android Studio, else it comes pre-installed with newer version of Android Studio.

Java Code

Create your new Android project as usual in Android Studio and we see our old innocent looking Java code which it really is at the beginning of the project.

Auto Generated Kotlin Code

Since Kotlin was made by the sweet folks who developed Android Studio itself, so there is a plugin to directly convert your Java code into Kotlin code, so if at times you dont know how to write something in Kotlin, write it in Java and use the plugin to get the Kotlin equivalent. A word of warning, the auto generated Kotlin code might not always work as you expect it to be so be careful while giving control to IDE to write your code. So let’s use that plugin to see what we have here.

And configure Kotlin-extension plugin in Build.gradle file, Also add the Kotlin dependencies in same file.

Add the third plugin, ‘kotlin-android-extensions’

2. Basic Syntax

> Hello World!

XML Layout for activity_main
Kotlin code for our Activity

Remember how we have to create variables to represent each of our views in Java. There is Butterknife to help with the Boilerplate code, but some boiler plate code is still there.

In Kotlin, you can directly access the view by just typing it’s ID, here in the example it’s messageTextView and we set it’s text so easily without any boiler plate code at all! That’s awesome right?

There should be an auto-import, If not then your project is not correctly configured, see step 1.

import kotlinx.android.synthetic.main.activity_main.*

> Classes

Creating a Kotlin Class is simple, just do

class NewClass {
// The { } are not required if you class doesn't have any body
}

In Android, most of the time our classes require only single constructor, In Kotlin the constructors parameters are defined in ( ) immediately after the classname like:

class NewClass (text : String, context : Context) {

}

And the constructor’s body??? We have init { } block for that.

class NewClass (text : String, context : Context) {
init {
// Assign the parameters
}
}

In Kotlin, Classes are final by default— cannot be extended, so our NewClass above is a final class. To make class extensible we have to declare them as ‘open’ or ‘abstract’. Although we can still use ‘extension functions’ :

fun NewClass.newFunction() {
println("yoyo")
} // This is valid

> Functions

Functions by default return ‘Unit’ which is equivalent to void in Java. Although Unit here is an object in Kotlin, and you can specify any type as a return value.

Remember function overloading?

Depending upon the various numbers of parameters we have to write various functions, here in Kotlin, you pass in a default value along with the parameter in the function, in case no value is passed the function will take the default value for that parameter. This means with just one function body we can do everything without the need to overload and over-complicate things.

I think that’s pretty much Kotlin sytax you need to know before you get started with your Kotlin App.

Stay tuned for next story in the series.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhananjay Trivedi

Dhananjay Trivedi

Developer who loves to build beautiful apps and write great Medium stories to share what I have learned.