Kotlin for Android: new project

For this tutorial, I’m going to walk you through a sample project that gets you set up for development in Kotlin for Android, using Android Studio and the Kotlin plugin.

Create a new project

As you would normally do with a Java-based Android application, create a new project in Android Studio by choosing ‘Start a new Android Studio project’:

Start a new Android Studio application
Start a new Android Studio project

or if you already have a project open, go to ‘File > New Project’:

New Project
File > New Project

Give your project a name.  I gave mine the name ‘FirstKotlin’:

First Kotlin
I named mine ‘FirstKotlin’

Choose the defaults on the next screen.  Mine looked like this:

TargetSettings
Target Android Devices

On the ‘Add an Activity to Mobile’ dialog, choose ‘Basic Activity’:

BasicActivity
BasicActivity

On the ‘Customize the Activity’ screen, just leave the defaults:

CustomizeActivity
CustomizeActivity

Click finish to create the project.

Run the application

After your project is created, go ahead and run it to make sure that you have a working application.

running app
running app

Download Kotlin plugin

Pull up Android Studio preferences (Android Studio > Preferences…).  Go to Plugins in the left navigation, and search for ‘Kotlin’.  Install this plugin.

kotlin plugin
kotlin plugin

Modify application build.gradle to add Kotlin plugin as a dependency

Add classpath org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.6 as a dependency to your application’s root build.gradle file.  My full application build.gradle is below:

 

Modify module build.gradle to apply Kotlin plugin

Add these two lines to your module’s build.gradle.  My module’s build.gradle is found at app/build.gradle:

Full build.gradle is found below:

Convert default MainActivity.java to Kotlin

Now here’s the fun part.  Let’s convert the MainActivity.java file to Kotlin.  Open the MainActivity.java file (mine’s at app/src/main/java/com/devdustin/firstkotlin/MainActivity.java).  Then, go to Code > Convert Java File to Kotlin File:

convert java to kotlin
convert java to kotlin

Your converted class should look something like this:

 

Run the application

Now, run your first Kotlin app!

running app
running app

If you get the same result as from before converting your Java file to Kotlin, good job!

Kotlin vs Java analysis

What differences do you notice between Kotlin and Java?

Here are a few to pay particular attention to:

  • Override as a keyword, not an annotation: in Java, the @Override annotation is used to indicate the developer’s intention in overriding a method, and if not properly overridden, will cause a compile time check. But it’s not required (though you’ll likely get a compiler error, depending on the compiler you’re using).  In Kotlin, you are required to use it.
  • Primary and secondary constructors: shown in the class above only as a primary constructor in ‘AppCompatActivity()‘, Kotlin classes have a unique way of handling object construction.  I’ll dig into this more in future posts.
  • Null Safety: one of the goals of the Kotlin type system is to avoid NullPointerException from occurring.  The ‘?’ after Bundle indicates that savedInstanceState could potentially be null.  You can also expect future posts on this topic, as it’s key to understanding Kotlin.
  • ‘fun’ indicates function: Kotlin uses the ‘fun’ keyword to indicate what would be a method in Java.
  • ‘extends’ and ‘implements’ are represented using a colon: very much like C# and other modern languages.
  • Semicolons are optional: though you can add them if you’d like to.
  • Function parameters are written ‘backwards’ compared to Java: they are defined using Pascal notation: name: type, and are separated using commas. A type is required for each parameter.

Conclusion

In this sample Android project you get a taste of what it’s like to develop with Kotlin. The Kotlin plugin for Android Studio includes a great tool for converting Java files to Kotlin, which helps developers ease into developing with Kotlin.

I went over a few language features that are expressed in this simple example.  Future posts will touch more in depth on these and many other Kotlin topics, with an emphasis on Android development.