Google PlusTwitter

Kotlin for Android (II): Create a new project

on Mar 23, 2015 in Blog, Development | 4 comments

Share On GoogleShare On FacebookShare On Twitter

After getting a light idea of what Kotlin is and what it can do for us, it´s time to configure Android Studio to help us develop Android apps using Kotlin. It requires some steps that only need to be done first time, but some other Gradle configurations will need to be done on every new project.

For this set of articles, I´ll be creating a reduced version of Bandhook, an app I created some time ago, which will basically connect to a music rest API and return some info about a set of bands. Go to Bandhook Kotlin on Github and take a look at the code.

Create a new project and download Kotlin plugin

Just create a basic Android project with an activity using Android Studio, the same way you would do for a regular project.

Once done, first thing you´ll need is to download Kotling plugin. Go to Android Studio preferences and search plugins. Once there, use search again to find Kotlin plugin. Install and restart the IDE.

kotlin-plugin

Add Kotlin plugin dependency to your application build.gradle

The root build.gradle needs a new dependency that will be required to use the Kotlin plugin in our main module:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.3'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.11.91'
    }
}

Configure module build.grade

First, apply Kotlin plugin:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

Then, add the Kotlin library to your dependencies:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'org.jetbrains.kotlin:kotlin-stdlib:0.11.91'
}

And finally, you need to add the Kotlin folder we´ll be creating on next step to your sources folders:

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.0"

    ...

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

Alternatively, you can skip this step, and after doing next ones, use this Android Studio action:

configure-kotlin-project

I prefer doing it manually to keep my Gradle files organized, but this second option could be easier.

Create Kotlin folder

It will be easier if you change the project visualization from ‘Android’ to ‘Project’. Go to ‘app->src->main’ and create a folder called ‘kotlin':

kotlin-folder

Convert java activity to a kotlin file

Kotlin plugin can convert from java to kotlin classes. We can convert our current activity to a Kotlin class very easily from ‘Code’ menu, by choosing ‘Convert Java File to Kotlin File':

convert-java-to-kotlin

IDE will suggest to move new file to the Kotlin folder. Click on ‘Move File’ (or move it manually if you don´t see the option).

You will get a very similar code translated to Kotlin. I suggest taking a look until you understand the differences:

public class MainActivity : ActionBarActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }


    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        val id = item.getItemId()

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true
        }

        return super.onOptionsItemSelected(item)
    }
}

Main differences

Just taking a look at previous code, we can see some direct differences. There are many more we´ll be discovering in next posts:

Conclusion

Though we can think using a new language will be very difficult, Kotlin is being created by the JetBrains team to be the most easy and interoperable language to cover the needs Java lacks. As Android Studio is also based on a JetBrains product, it will be very easy to integrate to this IDE and start working with it.

Next article will cover some tips and tricks to make our life easier when developing Android apps with Kotlin.

4 Comments

  1. Damian

    March 23, 2015

    Post a Reply

    Since M11 you can keep Kotlin files in java folder. However, I prefer keeping them separated in kotlin folder.

    • Antonio Leiva

      March 23, 2015

      Post a Reply

      Thanks! Didn’t know, but I also think it’s better to keep them separately.

    • Swati

      March 26, 2015

      Post a Reply

      Marvellous tutorial to start development in Android using Kotlin language. Keep posting new features related to Kotlin.Great !!!

  2. Reza

    April 4, 2015

    Post a Reply

    Thanks for the article.
    I’m trying to learn about the clean architecture and kotlin by reading your codes.
    And creating an app using this architecture, one of the component is authorisation using OAuth2.
    Where should i put this component? Inside repository or other module?
    The component also handle the access token refresh if possible.

Leave a Reply