Setting up Kotlin in Android Studio

Kotlin is a typed programming language for JVM. This new programming language is primarily sponsored by Jetbrains (who developed IntelliJ) and now announced as the first-class language for developing android apps by the internet giant Google. It is totally interoperable with Java and now comes with Android Studio 3.0. Kotlin’s interoperability with Java makes a good choice for developers. It’s quite similar like writing code in Swift for iOS development instead of using objective C. In this article we will discuss how to integrate Kotlin in Android Studio that we already have (as it comes default with Android Studio 3.0) and then test a small piece of code to check everything working as expected.

As the language is being termed as interoperable with Java, it’s an additional support for android development beside Java and C++. That means you can write code for android in traditional way you write, and no need to worry about if you don’t know Kotlin. The difference is that kotlin comes with some exciting features that is not currently supported by Java. Code snippets are smaller while writing code in Kotlin than that of Java. Kotlin is also being targeted to work with as native language in iOS development and it can also be compiled to Javascript source code for web developing.

PLUGIN FOR KOTLIN IN ANDROID STUDIO

The Kotlin plugin is bundled with Android Studio from version 3.0 and later. So, if you are using older version(we are using 2.2.2 here) and you want Kotlin to work within it you need to install Kotlin Plugin. So the steps are :

In Android Studio Go to File in menubar.

Then click settings. Settings window will appear.

Click on Plugins from the panel that appears in left.

In plugin tab click Install JetBrains plugin

In ‘Browse JetBrains Plugins’ window type ‘Kotlin‘ in the search box.

When you find Kotlin(language), install it. Plugin will start downloading. When completed restart your Android Studio to effect the changes.

Kotlin is now installed in your Android Studio. Now we need to configure it. So, to configure, click Tools from menubar, select Kotlin and from options appeared in right side select Configure Kotlin in Project.
A configurator chosser will appear. Select Android with Gradle oprion. Soon after that you will be prompted to select module and Kotlin compiler and runtime version. Select single module. By default app will appear. Leave it as it is. Select most recent Kotlin version and click OK.

After adding all those things you will be asked to sync your project as changes were made in both project and app level gradle files(build.gradle). Sync your project. Necessary Kolin files like compilers etc. will start downloading. That’s all. Now you have set up Kotlin in your project.

Please have a look on your both gradle(project and app level) files. You will see like

build.gradle(project-level)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

buildscript{

ext.kotlin_version='1.1.2-4'

repositories{

jcenter()

}

dependencies{

classpath'com.android.tools.build:gradle:2.2.2'

// ......

classpath'com.google.gms:google-services:3.0.0'

classpath"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

}

}

build.gradle(app-level)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

apply plugin:'com.android.application'

apply plugin:'kotlin-android'

android{

compileSdkVersion25

buildToolsVersion"25.0.1"

defaultConfig{

//...

}

buildTypes{

//...

}

}

dependencies{

compile fileTree(dir:'libs',include:['*.jar'])

//...

compile"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

}

repositories{

mavenCentral()

}

Now add the following lines to your app level build.gradle file below apply plugin: ‘kotlin-android’ at the top

build.gradle(app-level)

1

2

3

4

5

apply plugin:'kotlin-android-extensions'

This is required for identifying different views and other things as findViewById() concept not present in Kotlin. You will see this later in below. Now sync again your project.

A SMALL TEST

All set! Now to test Kotlin working we will write a smaller piece of code in Kotlin and run that. Before that we have created a new Activity, KotlinTestActivity.java where we will write our code. The layout code for this activity is as following

Then go to KotlinTestActivity.java. Now we need to convert this Java file in Kotlin file. You can do it by find action and then searching for Convert Java File to Kotlin File option. Or you can select Code from the menubar and then ‘Convert Java File to Kotlin File’ at the bottom(or direct shortcut Ctrl+Alt+Shift+K). After the conversion done you will see extension of this java file has been changed to .kt extension

Now add the following code block in your KotlinTestActivity.kt activity.

KotlinTestActivity.kt

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

packagecom.digitstory.testapplication

importandroid.support.v7.app.AppCompatActivity

importandroid.os.Bundle

importkotlinx.android.synthetic.main.activity_kotlin_test.myTextView

classKotlinTestActivity:AppCompatActivity(){

override fun onCreate(savedInstanceState:Bundle?){

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_kotlin_test)

myTextView.setText("Hello Kotlin")

}

}

As you can see a lots of changes there than that of traditional android programming using Java.
Here textView is imported and we are not using any kind of findViewById() here.
Run the app and you will say a TextView displaying “Hello Kotlin”.

Now we can say we have successfully integrated Kotlin in our Android Studio. Though the code looks a little weird(according to me), but you will love using Kotlin’s many attractive features that’s not available in Java. We will write more on Kotlin’s tutorials soon.

If you find this article helpful, spread it then! Share it to inspire us. Subscribe us to get more stuff like this.

Abhishek is a developer cum blogger working more than 4 years. He loves programming especially open stack technologies. He has decent knowledge in Android development, Wordpress, MongoDB, Node.js and so on. Beside this Abhishek finds himself busy in painting, front-end designing.

Follow Abhishek

Previous

Password visibility toggle in Android EditText

Next

Featured Post

ExoPlayer is an Android Application level Media Player by Google. It is used for playing media(both audio and video) from local or remote. In this article we will discuss how to play or stream remo ... Read More