This post is about setting up Android App project with Kotlin and Anko support.

Over the last couple of years Development Tools and Frameworks for Android significantly changed and improved:

  • Android Emulator got major performance improvement
  • Android Studio has become an official IDE for Android
  • Gradle is now default build tool
  • Android SDK was redesigned and improved bringing Material Design to Android users

I could continue on and on, describing all those nice changes that made Android Apps development easier, faster, more enjoyable and resulting apps nice and more stable. However this list would miss probably the most basic component – Java. We still need to use it for Android Apps development.

What’s wrong with Java, you might ask?

The short answer is – Java is not a MODERN programming language. It’s got a serious backwards compatibility burden. To make things worse, Android seems to be stuck with JDK 7 forever. With JDK 7 that doesn’t even have lambda functions. Fancy that! Android apps are event-driven by nature, and we don’t even get lambdas.

Missing lambdas is not the only problem Java has, there are many more:

  • null pointer unsafety
  • cumbersome generics
  • poor functional programming support
  • lack of extendability (operator overloading, meta-programming)
  • endless try-catch blocks
  • overall language verbosity

the list is endless, really. Many of the language’s flaws can be compensated by using specific library (for example to deal with null safety we can use Guava Optional), however it leads to other problems. As a result, we can’t write anything using Core Java anymore. To develop in Java it’s not enough to just know Java. We need to know and use bunch of frameworks / libraries, constantly upgrading them, migrating from old to new versions, be familiar with their features / bugs and so on. To summarise all of the above: Java at the moment is the most problematic component Android developers have to deal with.

Fortunately, thanks to Intellij (company behind IDEA – best IDE for Java development), we now have Kotlin – a relatively new modern language for JVM that is free of most of Java’s problems and has no performance overhead. Together with Anko – library for Android that lets defining Android layouts with Kotlin DSL, they make mobile app development truly enjoyable.

I will demonstrate how to create simple Android application with Intellij IDEA (you can use free Community version) or Android Studio (which is based on Intellij IDEA too).

Important! You don’t have to migrate the entire (existing) project to Kotlin. You can use both Java and Kotlin within single project, you can call Kotlin classes from Java and vice versa.


Setting up Android project with Kotlin and Anko support

First, we need to install Kotlin plugin for IDEA (I am using Intellij IDEA 2016.1.3 on MacOS). Go to “Preferences”, then search for “Plugins”, on Plugins page press “Browse repositories…” button and finally search for “Kotlin”. After plugin is downloaded you will be prompted to restart IDE.

Kotlin - enabling plugin

Kotlin – enabling plugin

When IDE comes back up, create new project: “File” > “New” > “Project” and in following wizard select “Android”

Kotlin - creating Android project

Kotlin – creating Android project

You may change default parameters or leave default, then click “Next”. On “Add an activity to Mobile” select “Empty Activity”

Kotlin - creating activity

Kotlin – creating activity

Move on in project creation wizard until the end. After project is created, open gradle build configuration file app/build.gradle and add these lines to enable Kotlin.

It sets up gradle plugin plus Kotlin and Anko dependencies.

To create your first Kotlin source file, right-click on package com.example.kotlin.myapplication in folder app/src/main/java and select New > Kotlin File / Class from drop-down menu and create file MainActivityKotlin. Then type in following code to implement main activity with layout:

As you see, with Kotlin and Anko we can define layout together with event handlers and all required logic in a single file. It is especially convenient for creating highly customisable and reusable self-contained components. Unlike ordinal XML-defined layouts, layouts defined with Anko are type- and null-safe. They are, also, faster to load (XML-layouts need to be parsed and processed on mobile device during runtime).

To register our new activity, we need to update app/src/main/AndroidManifest.xml with the new activity name

Optionally, we can do some cleanup by removing files that no longer required:

  • app/src/main/res/layout/activity_main.xml
  • app/src/main/java/com/example/kotlin/myapplication/

After compiling we can run our app on Android device or emulator

Kotlin - running Android app

Kotlin – running Android app

Getting project code from GitHub

You can get full source code of this example from GitHub.

Then you will need to create file with parameter sdk.dir than points to your local Android SDK location

and finally build it with gralde

If you want to import project into your Intellij IDEA or Android Studio, select File > New > Project from Existing Sources, then navigate to the project folder.

In Import Project dialog select Import project from external model and Gradle

Kotlin - import project from Gradle

Kotlin – import project from Gradle

Then just follow the wizard to complete project creation.

I intentionally kept the project functionality to bare minimum and focused on what’s required to setup the project and required development tools.

If you want to know more about Kotlin / Anko, please leave a comment below and I will write more in-depth post.

What else Anko has to offer to simplify Android development

Full Android layout definition support with Kotlin DSL. It supports all standard Android layouts and views with event handlers plus you can easily define wrappers for any non-standard ones.

Intent building functions

Configuration qualifiers



Asynchronous tasks

and many more…

What else Kotlin has to offer to simplify Android development

Data classes

Default values for method arguments

Convenience methods for all Collections simplify functional-style programming

Instance check with smart type cast

Read-only lists and maps creation

Easy map access

Lazy property

Extension function – you can “attach” a function to any class without inheritance (even to a final class like String, Date etc)

“If not null” shorthand

“try / catch” expression

and much more:

  • improved generics
  • singleton classes
  • delegation support
  • operator overloading
  • null safety

If you find this topic interesting, please leave a comment below and I will write more posts on it.

Share This

Share This

Share this post with your friends!