How to share kotlin code in IntelliJ IDEA between desktop, android and server?

#1

I’m developing a new client and server. The client needs to support android and desktop (Windows, Mac, Linux). The server only needs to support Linux. Both will be written in Kotlin.

I hope to be able build 3 things:

  • Android client APK

  • Desktop client JAR (Or native binaries for Windows, Mac and Linux)

  • Server JAR (Or native binary for Linux)

I want to share code between all 3 of these.

How do I go about setting this up in IntelliJ IDEA 2019.1?

I have tried the options under ‘New Project’ -> ‘Kotlin’, but they don’t appear to cover all my needs.

#2

You have 2 options. One is you create a build with multiple modules. Not sure how this would be done with intellij, but I suggest you take a look at gradle, which is a build tool and allows for more flexibility and enables features like CI.
There are quite a few sample projects here:


You basically create one core module where you put all the code you want to share. Then you can create a module for android, desktop and your server and make them depend on the core module. That way they can use the its code.

The other option is to create a kotlin multiplatform project. It allows you to build native binaries. However both native builds and multiplatform projects are currently an experimental feature and they only work with gradle. So you will have to set up a gradle build anyways. Not sure whether there is a default build.gradle file in intellj.

The docs describing it can be found here: https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html. And I guess there are a few examples out there as well if you search for them, but I personally don’t know any.