Kotlin 1.2 Multiplatform Projects

Kotlin 1.2 introduced an experimental feature to create multiplatform projects. With multiplatform projects, it’s now possible to write common code in Kotlin, and share the code across different platforms, JVM and JS at the moment.

Let’s use a simple example to demonstrate how multiplatform projects work. The easiest way is to create a new Kotlin multiplatform project using IDEA 2017.3. I created a project called calculator. IDEA created a Gradle project with two sub-projects: calculator-jvm and calculator-js. The root project is the common module that contains code that is not specific to any platform, as well as declarations without implementation of platform-dependent APIs. calculator-jvm and calculator-js are platform modules that contain implementations of platform-dependent declarations in the common module for a specific platform, as well as other platform-dependent code.

Let’s start from the Calculator object in the common module. Calculator has two functions add and subtract as shared common API to be used on both platforms. Both functions use log to output a log message. log is a declaration to be implemented on both platforms. expect means it expects a function log to exist.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package calculator

expect fun log(msg: String)

object Calculator {
fun add(x: Int, y: Int): Int {
log("add -> $x $y")
return x + y
}

fun subtract(x: Int, y: Int): Int {
log("subtract -> $x $y")
return x - y
}
}

In a Kotlin file in the module calculator-jvm, it implements the expected function log using System.out.println(msg). actual means it’s the actual implementation. This file uses Calculator from the common module.

1
2
3
4
5
6
7
8
package calculator

actual fun log(msg: String) = System.out.println(msg)

fun main(args: Array<String>) {
Calculator.add(1, 2)
Calculator.subtract(1, 2)
}

The Kotlin file in the module calculator-js is similar, except that the function log is implemented using console.log.

1
2
3
4
5
6
7
8
package calculator

actual fun log(msg: String) = console.log(msg)

fun main(args: Array<String>) {
Calculator.add(1, 2)
Calculator.subtract(1, 2)
}

This new feature in Kotlin is trying to resolve the issue of sharing code between different platforms. If your application is targeting multiple platforms, then this new feature in Kotlin may be a good choice. Currently only JVM and JS platforms are supported, Native platform will be supported later.

For a typical Java-based web application, the server usually exposes REST APIs for browsers and mobile apps to invoke. Here are some examples that may be created as common modules.

  • Validations: Validations for REST requests can be shared between different platforms. The server side always needs to perform validations. In the client side code, we can reuse the same module to perform validations to give the user immediate feedback.

  • Business logic: Although it’s rare to put complex business logic in the client side code, some simple business logic can be put into a common module and shared by different platform modules.

  • Cross-cutting concerns, like logging and security, can be easily used in common modules and have platform specific implementations, similar to the logging example shown in the Calculator.

Comments