mverleg / kotlin_multiplatform_gradle_demo

My attempt to get Gradle to work with multiplatform Kotlin while using subprojects.
27 stars 3 forks source link
gradle kotlin

Please be aware that this was created in early 2018. Some of it may still be useful, but please realize that parts will have stopped working or become deprecated. I am no longer updating it (though PRs are welcome).

Example of using Gradle sub-projects together with Kotlin multi-platform support

This project is an example of one way to set up a multi-platform Kotlin project separated into Gradle submodules.

The code itself is super simple, with just a platform-dependent method (expect/actual).

To run the code::

gradle wrapper
./gradlew run

Or to run only one platform (jvm in this example)::

./gradlew -p alpha/alpha-jvm run

This should show (between a bunch of Gradle stuff)::

Hello from JVM
Hello from JS

To collect the Javascript files in build/js, use the collectWeb task.

What I learned

Some errors and solutions

For the future

Tests

Currently the tests just use kotlin.test, and are ran by JUnit on JVM and QUnit in Javascript. They are included in ./gradlew run.

More info

Inspiration from:

These paragraphs from the documentation may be useful:

A multiplatform project consists of three types of modules:

* A common module contains code that is not specific to any platform, as well as declarations without implementation of platform-dependent APIs. Those declarations allow common code to depend on platform-specific implementations.
* A platform module contains implementations of platform-dependent declarations in the common module for a specific platform, as well as other platform-dependent code. A platform module is always an implementation of a single common module.
* A regular module. Such modules target a specific platform and can either be dependencies of platform modules or depend on platform modules.

A common module can depend only on other common modules and libraries, including the common version of the Kotlin standard library (kotlin-stdlib-common). Common modules contain only Kotlin code, and not code in any other languages.

A platform module can depend on any modules and libraries available on the given platform (including Java libraries in case of Kotlin/JVM and JS libraries for Kotlin/JS). Platform modules targeting Kotlin/JVM can also contain code in Java and other JVM languages.

(Force-pushed on 27 jan 2018, sorry about that)