Setting Up a Multiplatform Project

Tutorial 2 of 5

Setting Up a Multiplatform Project with Kotlin Multiplatform

1. Introduction

In this tutorial, we will learn how to set up a project that supports multiple platforms using Kotlin Multiplatform. You will learn how to set up a basic project structure, write shared code, and implement platform-specific functionalities.

Prerequisites

  • Basic understanding of Kotlin programming language.
  • Knowledge of using IntelliJ IDEA or Android Studio.

2. Step-by-Step Guide

Setting up the Project

  1. Open IntelliJ IDEA and create a new Kotlin Multiplatform Project.

  2. Choose the platforms you want to support. For this tutorial, we will select JVM and JS.

  3. Name your project and click 'Finish'.

Understanding the Project Structure

The generated project includes several modules:

  • shared: This module contains code that is shared between platforms.

  • jvmMain: This module is for JVM platform-specific code.

  • jsMain: This module is for JS platform-specific code.

Writing Shared Code

Shared code goes in the shared module. Here, you can define classes, functions, and interfaces that will be used across all platforms.

Writing Platform-Specific Code

Platform-specific code goes in the respective jvmMain and jsMain modules. Here, you can implement platform-specific functionalities.

3. Code Examples

Shared Code

In the shared module, create a new file Shared.kt and write the following code:

expect class Platform() {
    val platform: String
}

This expect keyword tells Kotlin that the actual implementation will be provided in each platform module.

Platform-Specific Code

In the jvmMain module, create a new file Platform.kt:

actual class Platform actual constructor() {
    actual val platform: String = "JVM"
}

In the jsMain module, create another Platform.kt file:

actual class Platform actual constructor() {
    actual val platform: String = "JS"
}

The actual keyword tells Kotlin that this is the implementation of an expected declaration.

Using the Shared Code

In the main function of each platform module, you can use the Platform class:

fun main() {
    println("Hello from ${Platform().platform}")
}

This will print "Hello from JVM" when run in the JVM module and "Hello from JS" when run in the JS module.

4. Summary

In this tutorial, you learned how to set up a Kotlin Multiplatform project, write shared code, and implement platform-specific functionalities.

Next, you could explore how to use the Kotlinx.serialization library to serialize and deserialize data in a multiplatform project.

5. Practice Exercises

  1. Create a new Kotlin Multiplatform project that supports JVM, JS, and Native. Write a shared function that returns the current date and time, and implement it differently for each platform.

  2. Extend the previous exercise by serializing the date and time into JSON format.

Solutions

  1. You can use the Date class in JVM, the Date object in JS, and the posix function in Native to get the current date and time.

  2. You can use the Json class from kotlinx.serialization to serialize the date and time.

Remember, practice makes better. Keep exploring and happy coding!