Setting up Kotlin in Android Studio


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

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

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.

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.


Tags : ,
Author
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 Android
Password visibility toggle in Android EditText

Next


detect new contact addition
Android : Detect new Contact addition