kotlin-for-android-icoderslab

Getting started with Android and Kotlin Programming Language

Google announced Kotlin as officially supported programming language for android development in developer conference Google I/O 2017. This is for the first time in Android OS history google added any programming language.

Android Studio 3.0 provides full support to Kotlin, you can create new projects with Kotlin files, add Kotlin files to your existing project, and also convert Java language code to Kotlin.

How to create new project with Kotlin

In Android Studio 3.0, click

File > New > New Project

Or if you see the Welcome to Android Studio window, click Start a new Android Studio project.
On the first screen,

check Include Kotlin support

That’s the only difference.
Click Next and continue through the wizard until you have created your new project.
kotlin-new-project-android-studio-icoderslab

How to install Kotlin Plugin

As Kotlin comes with android studio 3.0 so if you are using earlier version of android studio then you have to install kotlin plugin.
Go to

File > Settings > Plugins > Install JetBrains plugin

and then search and install Kotlin. If you are at the “Welcome to Android Studio” screen, choose

Configure > Plugins > Install JetBrains plugin

You have to restart the IDE after complete installation.

install-kotlin-plugin-icoderslab

download-install-kotlin-plugin-icoderslab

after successful installation of plugin you have to restart IDE, now you can use Kotlin in your projects.

How to convert Java code into Kotlin

There are several ways to convert Java source file (.java) into Kotlin source file (.kt), you can simply goto

code > Convert Java File to Kotlin File

convert-java-into-kotlin-android-icoderslab

Sample Activity Source Code of Kotlin

We have converted our simple blank activity from java to kotlin and here is what we get;


package kotlin.icoderslab.com.mykotlinapplication

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *