Header Ads Widget

Responsive Advertisement

Kotlin Multiplatform (KMP) makes it easy to share code across Android, iOS, and other platforms. 🛠️ One powerful feature that enables platform-specific code is expect/actual. Let's dive into how these work and why they're essential for Kotlin developers!

Kotlin Multiplatform expect and actual

🌟 What are expect and actual Keywords?

In KMP, here are often situations where you need to call platform-specific APIs that can’t be shared across different platforms. For example, Android has specific APIs for accessing hardware that don’t exist on iOS, and iOS has APIs that Android doesn’t support. To tackle this, Kotlin provides the expect and actual keywords help manage platform-specific code:

  • expect: Used in the shared commonMain module to define what you "expect" each platform to provide.
  • actual: Used in platform-specific modules (like androidMain or iosMain) to provide the actual implementation of expect declarations.

💡 Why Use expect and actual?

Using expect and actual keeps shared code independent of platform-specific APIs, making code more reusable and maintainable.

⚙️ Setting Up Your Project

To use expect and actual, start by creating a Kotlin Multiplatform project and setting up the common and platform-specific modules.

📝 Example: Get Platform Name with expect and actual

Step 1: Define expect in Common Code


// commonMain/src/commonMain/kotlin/com/example/platform/Platform.kt
package com.example.platform

expect fun getPlatformName(): String
    

In this code:

1. expect marks the getPlatformName function as something that needs a platform-specific implementation.

2. The function is declared without a body, as the actual implementation will be provided by each platform.

Step 2: Implement actual for Each Platform

Now, let’s move to the platform-specific modules and provide the actual implementations.

Android Implementation


// androidMain/src/androidMain/kotlin/com/example/platform/Platform.kt
package com.example.platform

actual fun getPlatformName(): String {
    return "Android"
}
    

iOS Implementation


// iosMain/src/iosMain/kotlin/com/example/platform/Platform.kt
package com.example.platform

import platform.UIKit.UIDevice

actual fun getPlatformName(): String {
    return UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion
}
    

Step 3. Using the Platform-Specific Function

In your common code, call getPlatformName() without worrying about platform differences:


// commonMain/src/commonMain/kotlin/com/example/Greeting.kt
package com.example

import com.example.platform.getPlatformName

fun createGreeting(): String {
    return "Hello from ${getPlatformName()}!"
}
    

When you call createGreeting() in the common code, it will return “Hello from Android!” on an Android device and “Hello from iOS <version> !” on an iOS device.

🎯 Common Use Cases

Here are some common use cases for expect and actual:

  • Platform APIs (like camera or location)
  • File Management
  • Networking with platform-specific libraries
  • Data Storage on different file systems

🧪 Testing expect and actual

Test your expect and actual code with common tests in commonTest and platform-specific tests in androidTest or iosTest.

🚀 Conclusion

The expect and actual keywords are powerful tools in Kotlin Multiplatform. They let you create shared code without platform-specific details. Start using them in your KMP projects to keep code clean and efficient! 🎉

Post a Comment

Link copied to clipboard!