Introduction to Kotlin Multiplatform

Tutorial 1 of 5

Introduction to Kotlin Multiplatform

1. Introduction

This tutorial aims to introduce you to Kotlin Multiplatform, an innovative feature that allows developers to share code across different platforms such as Android, iOS, Web, and Desktop. By the end of this tutorial, you will have a basic understanding of Kotlin Multiplatform and how to use it in your projects.

You will learn:
- The basics of Kotlin Multiplatform
- How to set up a multiplatform project
- How to share code across platforms

Prerequisites:
- Basic understanding of Kotlin language
- Familiarity with software development and code editors

2. Step-by-Step Guide

Kotlin Multiplatform allows you to write common code that can be shared across different platforms like iOS, Android, and the Web. It also lets you write platform-specific code when needed.

Setting up a Multiplatform Project
1. Start by setting up a new project in IntelliJ IDEA.
2. Choose Kotlin under Project SDK, then select Kotlin/Multiplatform under Project Template.

Sharing Code Across Platforms
1. In Kotlin Multiplatform, shared code is written in the commonMain source set.
2. The platform-specific implementations are written in the corresponding source sets, such as androidMain and iosMain.

Example:

// Common Code in commonMain
expect class Sample() {
  fun checkMe(): Int
}

// Android Specific Code in androidMain
actual class Sample {
  actual fun checkMe() = 42
}

// iOS Specific Code in iosMain
actual class Sample {
  actual fun checkMe() = 24
}

In the example above, expect keyword is used to declare a placeholder for platform-specific implementation.

3. Code Examples

Example 1: Shared Code

// In commonMain
expect fun platformName(): String

fun createMessage() = "Kotlin runs on ${platformName()}"

In this example, platformName() is a placeholder for platform-specific code.

Example 2: Platform-Specific Code

// In androidMain
actual fun platformName(): String {
  return "Android"
}

// In iosMain
actual fun platformName(): String {
  return "iOS"
}

In these examples, platformName() is implemented specifically for Android and iOS.

4. Summary

In this tutorial, you learned the basics of Kotlin Multiplatform, how to set up a multiplatform project, and how to share code across different platforms. To continue your learning, you can explore more about Kotlin Multiplatform's advanced features and best practices.

5. Practice Exercises

Exercise 1: Create a function in commonMain that expects a platform-specific implementation to return the current date.

Exercise 2: Implement the function you created in androidMain and iosMain.

Solutions:

// Common Code in commonMain
expect fun currentDate(): String

// Android Specific Code in androidMain
actual fun currentDate(): String {
  val sdf = SimpleDateFormat("yyyy-MM-dd")
  return sdf.format(Date())
}

// iOS Specific Code in iosMain
actual fun currentDate(): String {
  return NSDate().description
}

In these solutions, currentDate() is implemented specifically for Android and iOS.

Keep practicing and exploring more about Kotlin Multiplatform. Happy coding!