Kotlin multiplatform project example
This repository contains a barebones Kotlin multiplatform Gradle project with 3 modules:
common (compiled to both JVM
bytecode and JS),
client (compiled to JS) and
server (complied to JVM). Assumes a simple architecture, where the
server module is a backend JVM application,
client is the usual frontend web application, and the
allows to share data structures, validation logic, and so on. It also comes with a working tests setup on both target
Feel free to use it for personal and commercial projects.
Note that I don't feel comfortable with frontend tools and the
client module might have some issues. If you notice
any problems or invalid configurations, please create a GitHub issue.
What to do after cloning
- Change group and version in root
- Update Gradle wrapper version in
- Update dependencies' versions in
- Rename packages (preferably with IntelliJ
Refactor > Rename), replace
com.github.czyzbythroughout the project.
- Include your project dependencies in subprojects
- Write actual code. Or not.
Useful Gradle tasks
server:test: runs server tests and common tests compiled with server
actualimplementations via JUnit runner.
client:test: runs client tests and common tests compiled with client
actualimplementations via Karma/Mocha.
check: runs tests (or tests with all checks) in every subproject.
mainfunction of the server project.
client:run: runs Webpack in watch mode at
localhost:8080which automatically reloads when you recompile KotlinJS sources or add copy new static resouces. (Press F12 and read the console to see if example code works.)
client:watch -t: recompiles client sources and copies resources on every
src/mainfile modification. Make sure to use
-tflag to run in continuous mode. Along with
client:run, these two tasks provide hot reload after every file change.
assemble: builds a standalone runnable jar at
server/build/libsand bundles JS sources to
client:bundle: invokes Webpack (with deployment configuration) to pack and minifies JS sources and exports them to
client/build/dist. Invoked by
client:copyStaticResources: copies files from
client/build/dist. Copied files will be served by the dev server (
client:run). Automatically invoked by
During development, run
./gradlew client:run and
./gradlew -t client:watch to deploy the client application locally
localhost:8080 with hot reload on each file change. Use your server framework of choice to run the server
application. If anything unexpected happens, run
./gradlew clean and repeat.
Ctrl+C will not kill Webpack (
client:run). This is a known issue.
You can avoid it by turning off Gradle deamons or installing Yarn locally and using
yarn run start instead of the
To deploy, run
server/build/libs will contain a standalone runnable jar, and
will contain your resources, Webpack-generated HTML file and bundled JS files.
Client project contains examples of asynchronous tests.