Over a million developers have joined DZone.

Kotlin for Cross Platform Mobile App Development

Use Kotlin for both Android and iOS app development

· Mobile Zone

Kotlin is leading the pack of new JVM languages in the mobile space these days. Combining Kotlin with RoboVM allows you to not only target Android, but also use Kotlin on iOS, and share code between your frontends and backend.

To help spread the word, we started a Kotlin contest a few weeks ago. Read about the details on our contest site and submit until the 14th of December!

Getting Started with Kotlin on mobile


If you are using IntelliJ Community or Ultimate editions, you can install RoboVM from the JetBrains Plugin Repository.

You can also just download and install our custom build of IntelliJ, RoboVM Studio, which bundles up everything you need to get started.

No matter how you installed RoboVM, once your IDE launches, you can click on “Create New Project”. Under the RoboVM section, select the new cross platform template for Kotlin. Then fill in the details about your app.

Your new project consists of three modules: ios, android, and core.

In the Android project you will create your UI using the Android Designer tool and Kotlin extensions for android.

For iOS, you will use Interface Builder and the Robovm APIs.

And finally, The core module is where the bulk of your code will go, like database access and business logic. This module is shared between the your Android and iOS apps.

RoboVM Studio has already created run configurations for all the simulators and devices available, so to try out your first Kotlin cross-platform application, choose one from the drop down and press Run or Debug.

From here you should check out the docs at the Kotlin and RoboVM websites, which provide more in depth references and tutorials.

A brief tour of Kotlin features

Not only is Kotlin 100% compatible with Java, Kotlin also provides sophisticated features that allow you reduce boilerplate and maximize safety.

Automatic getters, setters, …

You can create plain old Java objects with getters, setters, equals(), hashCode(), toString() and copy() in a single line.

  data class Hero(val identity: String, val power: String, val strength: Int)

Don’t fear the NullPointerException

The type system protects you from mistakenly operating on nullable types, including those from Java.

  var headquarters: String = "Fortress of Justice"
  headquarters = null // compilation error

Lambdas

Lambdas are ready to be used in your projects today, even on Android.

  max(heroes, { a, b -> a.strength < b.strength })

Properties

You finally have access to properties. These can be declared read-only or mutable, and have custom accessors.

  val isSuperHuman: Boolean 
    get() = this.strength > 15

Extensions

Kotlin also provides the ability to extend a class with new functionality without having to inherit from the class. This is done using extension declarations.

  fun MutableList.swap(index1: Int, index2: Int) {
    val tmp = this[index1] // 'this' corresponds to the list
    this[index1] = this[index2]
    this[index2] = tmp
  }

One step at a time

You don’t even need to convert your entire project right away, because you can mix Kotlin into your existing Java projects. IntelliJ IDEA even let’s you convert Java code to Kotlin code, one class at a time. Open the Java class you want to convert, then hit the Code menu and select Convert Java file to Kotlin:

convert-java-to-kotlin

This is just a birds eye view on what Kotlin has to offer. To learn more about Kotlin, check out the official Kotlin site. To get up to speed quickly, have a look at the Kotlin Koans!

Let us know what you come up with, and Happy Coding!

Topics:
kotlin ,mobile ,robovm

Published at DZone with permission of joseph.hager, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}