Keddit — Part 1: Configuring Android Studio with Kotlin

Juan Ignacio Saravia
AndroidPub
Published in
6 min readJan 28, 2016

--

Content

Part 1: Configuring Android Studio with Kotlin

In this Part we are going to configure an Android Studio project to use Kotlin and leave everything prepared for the next story to start developing the Keddit App from the beginning and learning some Kotlin concepts.

If you missed the first part of these Stories you can go to the Introduction just to get a quick overview of what we are going to be developing, which Kotlin topics are going to be covered and also which libraries are going to be used.

Create Project

We are going to start from scratch, launch Android Studio and create a new Project, I’ll name it “KedditBySteps” and select “Blank Activity”.

Note: In AS 3.x these preview images change a little bit but still helps to understand which options we are going to use.

Android Studio 3.x

In Android Studio 3.0 the plugin is already built-in there and while creating the new project you just have to make sure to check “Include Kotlin support” and the complete project will just support Kotlin from the beginning without requiring you any extra steps:

If you are using AS 3.x then you just can skip the following configuration and just jump to “Repository” section.

Android Studio 2.x

Kotlin Plugin

Ok we have a new Project, let’s install the required Plugin for Kotlin. Go to:

Android Studio Pref > Plugins > Browse Repositories > search for “Kotlin”

Install only the Plugin called “Kotlin” and restart Android Studio.

There are other Kotlin plugins but we only need this one as the Kotlin Extensions Plugin was already integrated in the first plugin so we are OK with this one.

N̶o̶t̶e̶:̶ ̶I̶f̶ ̶y̶o̶u̶ ̶a̶r̶e̶ ̶u̶s̶i̶n̶g̶ ̶A̶n̶d̶r̶o̶i̶d̶ ̶S̶t̶u̶d̶i̶o̶ ̶2̶.̶0̶ ̶P̶r̶e̶v̶i̶e̶w̶,̶ ̶e̶x̶p̶e̶c̶t̶ ̶s̶o̶m̶e̶ ̶w̶e̶i̶r̶ ̶b̶e̶h̶a̶v̶i̶o̶u̶r̶s̶ ̶i̶n̶ ̶t̶h̶e̶ ̶P̶l̶u̶g̶i̶n̶,̶ ̶a̶l̶s̶o̶ ̶i̶n̶ ̶s̶o̶m̶e̶ ̶s̶i̶t̶u̶a̶t̶i̶o̶n̶s̶ ̶i̶t̶ ̶s̶t̶o̶p̶ ̶w̶o̶r̶k̶i̶n̶g̶,̶ ̶s̶o̶ ̶f̶o̶r̶ ̶n̶o̶w̶ ̶I̶ ̶s̶u̶g̶g̶e̶s̶t̶ ̶y̶o̶u̶ ̶t̶o̶ ̶u̶s̶e̶ ̶1̶.̶5̶ ̶v̶e̶r̶s̶i̶o̶n̶ ̶a̶s̶ ̶i̶t̶’̶s̶ ̶w̶o̶r̶k̶i̶n̶g̶ ̶a̶s̶ ̶e̶x̶p̶e̶c̶t̶e̶d̶.̶

Note Updated: Now you can use Android Studio 2.0 that was released with the Kotlin Plugin.

Configure Project with Kotlin — “first attempt”

If you already read something on how to configure Kotlin, maybe you feel tempted to execute the plugin action to “Configure Kotlin in Project” which is accessible from Tools > Kotlin or by Android Studio Find Actions (pressing Shift button twice) and searching for “Configure Kotlin…” like in this image:

Left picture: Shortcut to show the Search Dialog || Right picture: Message that everything was configured

If you do this, you will notice that nothing happens. The only thing that you will see is a message that all modules with Kotlin files were configured.

And that’s why nothing happens because we don’t have any Kotlin file in our project so the Plugin didn’t configure our project. So let’s create a Kotlin file and configure this project again.

From Java to Kotlin file

In order to have a Kotlin file we are going to convert our already created MainActivity.java file into a Kotlin file. In order to do that just open the MainActivity.java file and open Find Action and start searching “Convert Java to…”

A message will warn us that maybe the conversion is not accurate and required some manual modifications but we are fine for now with this.

You will see something like this:

Right now we are not going to review this file (this will be in the next Post) so we can continue configuring this. Also for sure you notice the extension name of our new Kotlin file which is “.kt”.

Configure Project with Kotlin — “Last one :)”

Now we are ready to configure it. Let’s run again the “Configure Project with Kotlin” action:

Choose the latest Kotlin version and make sure to have the latest Plugin installed in Android Studio

A new message will appear allowing us to select which modules we would like to convert and also the Kotlin plugin version that we want to use (just pick the latest one in this case).

You will notice that the build.gradle file from your module was updated. It will have new configurations like this:

apply plugin: 'kotlin-android'
...
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
...
dependencies {
...
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
...
buildscript {
ext.kotlin_version = '1.0.0-XYZ'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

At the beginning you had to do this manually but now with the Plugin this is really easy.

Run, run, run!

Everything is ready to run this App and using our new MainActivity.kt Kotlin file :)

Repository

Here you have the code that we developed in this Part, also it was tagged with the tag name “v0.1” which identify the Part1 of this tutorial.

https://github.com/juanchosaravia/KedditBySteps/tree/v0.1

Conclusion

As you can see, it’s really easy to configure an Android Studio project with Kotlin, we only need a Kotlin file in the module in order to configure it with the Plugin.

With this new project we are ready to start developing the Keddit App but most important to start learning some Kotlin concepts.

Please, if you find something to improve or any suggestion, don’t hesitate to contact me, I’ll try to do my best to answer any question or improve this tutorial.

See you in the next story!

Next Part:

Part 2: MainActivity.kt: Syntax, Null Safety and more…

Twitter: https://twitter.com/juanchosaravia

--

--