Kotlin / Kotlin Multiplatform Development

Setting Up a Multiplatform Project

In this tutorial, you will learn how to set up a project that supports multiple platforms using Kotlin Multiplatform.

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Introduces Kotlin Multiplatform for building cross-platform applications.

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!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

File Size Checker

Check the size of uploaded files.

Use tool

PDF Compressor

Reduce the size of PDF files without losing quality.

Use tool

Countdown Timer Generator

Create customizable countdown timers for websites.

Use tool

Markdown to HTML Converter

Convert Markdown to clean HTML.

Use tool

Time Zone Converter

Convert time between different time zones.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help