How to Use Kotlin in Your Android Projects


One great news of moving to Kotlin is, do not need to start from a new project. You could add it to your existing Java Android project. The language is interoperable with Java. So you could use steps below either for a new or existing project.

Setting up Your Environment

By default, Android Studio has no idea what to do with Kotlin, so the first step is to install the Kotlin plugin and configure Kotlin in your project.

Installing the Kotlin Plugin

Launch Android Studio and install the kotlin plugin.
  1. In Android Studio's quick start menu, select Configure >> Plugins.
  2. click on Install JetBrains plugin…
  3. Search for and select Kotlin from the list and click Install.
  4. When the installation completes, restart Android Studio to activate the plugins.

Configuring Your Project to Use Kotlin

Now IDE can understand and run Kotlin code, but need to configure kotlin for every project.
Create a new project in Android Studio from File >> New Project by filling the required details.
Select Tools from the Android Studio toolbar, followed by Kotlin and Configure Kotlin in Project.
Screen Shot 2017-05-10 at 8.47.47 AM-min.png
On the Configure Kotlin in Project popup, select the plugin version you want to use and click OK.
Screen Shot 2017-05-10 at 8.49.43 AM-min.png
The Configure Kotlin in Project option makes a number of changes to your project’s build.gradle files, so let’s take a closer look at how these files have changed. Open your project-level build.gradle file. it should look something like this:

Now, let’s take a look at your module-level build.gradle file:

Click on Sync Now to build the project.

Convert Any Java File to Kotlin

One of the feature of kotlin plugin is that we can convert a java classes to kotlin classes.
Let's convert MainActivity file into a Kotlin source file by invoking the Kotlin plugin’s Convert Java File to Kotlin File action.
Select your MainActivity file, and then select Code from Android Studio’s menu bar, followed by Convert Java File to Kotlin File.
Screen Shot 2017-05-10 at 8.50.41 AM-min.png
Your newly-converted MainActivity should look something like this:

Notice that the file's extension has changed from .java to .kt.

Using Kotlin Android Extensions

The Kotlin Android Extensions plugin allow you to use the widgets in the layout XML directly in activity without calling the findViewById method.
Add org.jetbrains.kotlin:kotlin-android-extensions as a build script dependency in app module's build.gradle.

Create a Kotlin class, KotlinActivity, that extends Activity and override its onCreate method.
To create a Kotlin file, right your app/src/main/ directory and select New >> Kotlin Activity.
Screen Shot 2017-05-10 at 8.51.49 AM-min.png

In the layout XML file, define a TextView with an id of messageTextview.

Now, instead of calling findViewById to get a reference to the TextView, you can import it using the following code snippet:
If you had more widgets in your layout, you can import all of them using the following code snippet:
Now access TextView using its id messageTextview.
In this tutorial, you have learned how to use Kotlin in your Android projects after installing the Kotlin plugin and the Kotlin Android Extensions plugin for Android Studio. As Kotlin and Java classes are largely interoperable, if you are still learning Kotlin, it is best to introduce it in your Android projects gradually.


Popular posts from this blog

Reactive Programming with RxBinding

Exploring Android O: Fonts in XML

Kotlin: Do more with less code

Android Debug Database: A Library for Debugging Android Databases and Shared Preferences

Introduction to Spring Animation

Introduction to Android Bottom Navigation View

Exploring Android O: Autosizing TextViews

Getting started with Android Fingerprint Authentication