Kotlin Multiplatform(KMP) Interview Questions and Answers

Kotlin Multiplatform(KMP) Interview Questions and Answers


Kotlin Multiplatform (KMP) is getting a lot of attention these days because it allows you to write code once and share it across multiple platforms like Android, iOS, Web, Desktop, and even backend servers.

If you are preparing for interviews related to KMP, this blog will help you with important questions and deep explanations.

1. What is Kotlin Multiplatform?

Kotlin Multiplatform is a technology that lets you share the common code (like business logic, networking, database handling) between different platforms like Android, iOS, web, etc.  You only write platform-specific code (like UI, Bluetooth, GPS) where necessary.

Example:

  • You can write networking code once and use it on both Android and iOS apps.
  • But you will still design separate UIs for Android (Compose/XML) and iOS (SwiftUI/UIKit).

2. Why do we use Kotlin Multiplatform?

We use Kotlin Multiplatform to:

  • Save time: No need to write the same code again for Android and iOS.
  • Reduce bugs: Shared code means fewer chances for mistakes.
  • Maintain easily: Fix in one place, works everywhere.

3. What parts of an app can be shared using Kotlin Multiplatform?  You can share:

  • Business Logic (e.g., how your app works)
  • API/networking code
  • Database operations (e.g., SQLDelight)
  • Models (data classes)

You cannot share:

  • UI code (Android and iOS have different UI systems)

4. How does Kotlin Multiplatform actually work under the hood?

  • KMP compiles the shared code into different binaries for each platform.
  • For Android: it compiles to JVM bytecode.
  • For iOS: it compiles to native code (via Kotlin/Native).
  • So, each platform understands the shared code natively.

5. What are the main modules or projects you find in a KMP setup?

  • Shared Module (Common code for all platforms)
  • Android App Module (Native Android app)
  • iOS App Module (Native iOS app)

6. What are expect/actual keywords in Kotlin Multiplatform?

  • expect is used to declare something in the shared code.
  • actual is used to implement it separately for each platform.

Example:  In shared code:

expect fun getPlatformName(): String        

In Android:

actual fun getPlatformName(): String = "Android"        

In iOS:

actual fun getPlatformName(): String = "iOS"        

7. What is Kotlin/Native?

Kotlin/Native is a part of KMP that compiles Kotlin code to native machine code (like for iOS, macOS, Linux).

8. What is Kotlin Multiplatform Mobile (KMM)?

KMM is a special set of tools and libraries inside KMP focused only on sharing code between Android and iOS.

9. How do you handle Database in Kotlin Multiplatform?

You can use libraries like:

  • SQLDelight (for database sharing)
  • Realm Kotlin (cross-platform database)

These libraries allow you to write database code once and use it everywhere.

10. How do you handle Networking (API calls) in Kotlin Multiplatform?

You can use libraries like:

  • Ktor (HTTP client for KMP)

You can create APIs once using Ktor and call them from Android and iOS both.

11. How do you share code between Android and iOS?

You create a shared module that contains common logic, and then:

  • Android calls it like normal Kotlin code.
  • iOS accesses it using a generated framework (.framework file).

12. Can you use Jetpack Compose in Kotlin Multiplatform?

Answer:  Yes! With Compose Multiplatform you can build UI using Compose for:

  • Android
  • Desktop
  • Web (experimental)

But for iOS, Compose UI is still under development.

13. What is the difference between Kotlin Multiplatform and Flutter/React Native?

Article content

14. What are the challenges of using Kotlin Multiplatform?

  • Setting up can be tricky for beginners.
  • Third-party library support is still growing.
  • UI cannot be shared (yet, fully).
  • Requires good knowledge of Android and iOS platforms.

15. Future of Kotlin Multiplatform?

  • JetBrains (creators of Kotlin) are investing heavily.
  • Big companies like Netflix, VMware, CashApp are using KMP.
  • It’s getting better support for iOS and Desktop every day.

16. What is Multiplatform Project Structure in Kotlin?

A typical KMP project has 3 parts:

  • commonMain → Shared code (business logic, networking, database)
  • androidMain → Android-specific code
  • iosMain → iOS-specific code

There are also test folders:

  • commonTest, androidTest, iosTest for unit testing.

17. How does Dependency Injection work in Kotlin Multiplatform?

You can use:

  • Koin (supports KMP)
  • Manual Dependency Injection (by passing dependencies manually)
  • Kodein (earlier used but less popular now)

18. What are the Gradle Plugins used in Kotlin Multiplatform?

Important plugins:

  • kotlin-multiplatform → to enable KMP
  • kotlin-native.cocoapods → to integrate with iOS CocoaPods
  • kotlinx-serialization → for JSON parsing
  • android-library → if you want Android module inside

19. What is CocoaPods and why is it needed in KMP?

  • CocoaPods is a dependency manager for iOS (like Gradle for Android).
  • KMP uses CocoaPods to link shared Kotlin code into iOS projects easily.

20. What are some popular libraries compatible with Kotlin Multiplatform?

  • Ktor → Networking (HTTP calls)
  • SQLDelight → Database
  • Koin → Dependency Injection
  • Kotlinx.serialization → JSON Parsing
  • Moko libraries → Resources, MVVM

21. How does Kotlin Serialization work in Multiplatform Projects?

  • Kotlinx.serialization library allows you to serialize (convert) and deserialize (convert back) objects to/from JSON.
  • It works across all platforms (Android, iOS, Web).

Example:

@Serializable
data class User(val name: String, val age: Int)        

22. Can we call Swift/Objective-C code from Kotlin Multiplatform?

Yes! , You can call platform-specific Swift/Obj-C code using expect/actual mechanism or native interop.

23. Can we use Coroutines in Kotlin Multiplatform?

Yes!

  • Kotlinx.coroutines library is available in KMP.
  • You can write suspend functions, use launch, async, flow across platforms.

24. How do you share Resource files (like images, strings) in Kotlin Multiplatform?

  • Use libraries like moko-resources to manage shared strings, images, and assets.

25. How do you perform Testing in Kotlin Multiplatform Projects?

  • Use commonTest for shared business logic tests.
  • Use androidTest for Android-specific code.
  • Use XCTest for iOS-specific code.

26. When should you NOT use Kotlin Multiplatform?

  • If your project is very simple (small app, no complex logic).
  • If your team has no Kotlin experience.
  • If you need full UI sharing (then Flutter/React Native may be better).

27. How do you handle platform-specific threading and concurrency in Kotlin Multiplatform?

  • KMP supports Coroutines for managing concurrency across platforms. You can use Dispatchers to specify different threads for each platform.
  • On Android, use Dispatchers.Main for UI-related tasks and Dispatchers.IO for background tasks.
  • On iOS, Kotlin/Native supports concurrency through Dispatchers.Main, Dispatchers.Default, etc., which operate similarly to Android.

Example:

fun fetchData() {
    GlobalScope.launch(Dispatchers.Main) {
        val data = withContext(Dispatchers.IO) { fetchFromNetwork() }
        updateUI(data)
    }
}        

28. What are the challenges of using Kotlin Multiplatform for iOS?

  • Interop with Objective-C/Swift: Calling Objective-C or Swift from Kotlin can be complex and require manual setup.
  • CocoaPods integration: iOS dependencies might not always be easily integrated into Kotlin Multiplatform because some iOS libraries are not yet fully supported.
  • Performance issues: The translation of Kotlin code to native iOS code via Kotlin/Native may cause performance issues in some edge cases.
  • Limited UI support: Kotlin Multiplatform does not yet support sharing UI code for iOS fully, so you still need to write separate UI for iOS apps (SwiftUI/UIKit).

29. How do you integrate third-party libraries in Kotlin Multiplatform?

  • For Android, you can add libraries directly in the androidMain source set (just like any normal Android library).
  • For iOS, you use CocoaPods to include third-party dependencies.
  • You’ll need to add the CocoaPods dependency in the build.gradle file under the iOS target.
  • You may also need to use expect/actual when dealing with APIs that differ on iOS vs Android.

30. How do you handle debugging Kotlin Multiplatform code across different platforms?

  • Android Debugging: Use Android Studio’s debugger as usual, which can be used for debugging Kotlin code.
  • iOS Debugging: Use Xcode for debugging. When running the app in Xcode, you can debug the Kotlin code within the iOS app as well.
  • Shared code debugging: You can set breakpoints and use logs in commonMain for shared logic. For platform-specific issues, debug using platform-specific tools.

31. What are the differences between suspend and expect functions in Kotlin Multiplatform?

  • suspend functions are used in Coroutines to allow suspending the execution of a function without blocking the thread.
  • expect functions are a mechanism to declare a function in the common code, and the platform-specific implementations are provided using the actual keyword. These are typically used when you need platform-specific functionality.

32. How do you manage versioning of Kotlin Multiplatform dependencies?

  • Versioning: KMP supports dependency management similar to any other Gradle project, so you can specify versions of shared dependencies.
  • CocoaPods: You manage versioning for iOS dependencies using CocoaPods, and it needs to be integrated properly in your Podfile.
  • Cross-platform compatibility: To handle versions between Android and iOS, you need to ensure that both platforms use compatible versions of libraries.

33. How do you handle platform-specific resources (like images or strings) in Kotlin Multiplatform?

  • Moko-Resources: This is a library that allows you to manage resources such as strings, images, and colors in a multiplatform way. It generates platform-specific implementations for accessing the resources.
  • For platform-specific resources, you’ll still need to use Android-specific or iOS-specific implementations.

34. Explain the role of kotlinx.serialization in Kotlin Multiplatform and how it works across platforms.

  • Kotlinx.serialization is a library used for converting Kotlin objects into JSON (or other formats like XML) and vice versa.
  • It works across all KMP platforms, allowing you to serialize and deserialize data in a consistent way across Android, iOS, and even backend code.
  • You can use it with expect/actual for handling platform-specific data types.

35. How do you handle testing for Kotlin Multiplatform projects?

  • Unit Testing: You can write unit tests for shared code in commonTest. For platform-specific code, write tests in androidTest for Android and iosTest for iOS.
  • Mocking: Use tools like MockK for mocking dependencies in tests.
  • KMP-specific testing libraries: You can use libraries like KotlinTest or Spek for writing test cases.

36. What is the role of Kotlin/Native in Kotlin Multiplatform and how does it work?

  • Kotlin/Native is used for compiling Kotlin code to run natively on platforms that do not have a JVM (e.g., iOS, macOS, Linux).
  • It uses LLVM (Low-Level Virtual Machine) to compile Kotlin code to machine code.
  • Kotlin/Native is essential for iOS in Kotlin Multiplatform because iOS doesn’t run on JVM.

37. How do you handle dependencies between shared code and platform-specific code in Kotlin Multiplatform?

  • Dependencies are declared in the shared module (commonMain), but platform-specific code can reference shared code while implementing platform-specific behavior.
  • You can use expect/actual to declare dependencies in common code and provide platform-specific implementations.

38. You’re working on a mobile app that needs to fetch user data and display it in the app. You have to support both Android and iOS platforms. The user data is fetched from a backend API. How would you implement this in Kotlin Multiplatform?

Approach:

  1. Shared Module: Write the logic to fetch data in the shared module (commonMain). This logic will handle the data fetching for both Android and iOS.
  2. Expect/Actual: For platform-specific functionality (e.g., network libraries like Retrofit for Android, and Ktor for iOS), use the expect/actual mechanism to handle the differences.
  3. Coroutines for Async: Use Coroutines to perform the network call asynchronously to avoid blocking the main thread.
  4. Error Handling: Ensure proper error handling using sealed classes to represent success and failure cases for both platforms.

Example Code:

// Common code (shared module)
expect fun fetchUserData(): String

// Actual implementation for Android
actual fun fetchUserData(): String {
    // Using Retrofit or any other Android networking library
    return "User data from Android"
}

// Actual implementation for iOS
actual fun fetchUserData(): String {
    // Using Ktor or another iOS networking library
    return "User data from iOS"
}        

39. You’re developing a mobile app that needs to send push notifications to both Android and iOS. How would you implement this in Kotlin Multiplatform?

Approach:

  1. Shared Code: The logic for managing notifications, like displaying alerts, can be written in the shared module (commonMain).
  2. Platform-Specific Implementation: You will need to write platform-specific code for handling push notification APIs (Firebase for Android, and APNs for iOS).
  3. Expect/Actual: Use expect/actual to define platform-specific methods for subscribing to and receiving push notifications.
  4. Dependencies: On Android, use Firebase Cloud Messaging (FCM), and on iOS, use Apple’s Push Notification service (APNs)

Example Code:

// Common code (shared module)
expect fun subscribeToPushNotifications()

// Actual implementation for Android
actual fun subscribeToPushNotifications() {
    // Use Firebase Cloud Messaging to subscribe to push notifications on Android
    FirebaseMessaging.getInstance().subscribeToTopic("user_topic")
}

// Actual implementation for iOS
actual fun subscribeToPushNotifications() {
    // Use APNs to subscribe to push notifications on iOS
    // Note: iOS code for subscribing to APNs
}        

40. You need to implement a feature in your Kotlin Multiplatform app where you fetch data from a backend API. The API response includes a list of items, but the format of the response differs between Android and iOS (different field names). How would you handle this?

Approach:

  1. Expect/Actual Mechanism: Define a common response data structure (expect) in the shared module, and use the actual keyword to map the response fields differently on Android and iOS.
  2. Data Mapping: For the platform-specific implementations, use JSON serialization to map the response fields correctly. You can use kotlinx.serialization to handle this.
  3. Error Handling: Handle cases where fields might be missing or different by using nullable types or default values.

Example Code:

// Common code (shared module)
@Serializable
expect data class ApiResponse(val id: String, val name: String)

// Actual implementation for Android
actual data class ApiResponse(val id: String, val name: String) {
    // Android-specific response mapping
    constructor(json: JsonObject) : this(
        json.get("userId").jsonPrimitive.content, // Mapping Android-specific field
        json.get("userName").jsonPrimitive.content
    )
}

// Actual implementation for iOS
actual data class ApiResponse(val id: String, val name: String) {
    // iOS-specific response mapping
    constructor(json: JsonObject) : this(
        json.get("uid").jsonPrimitive.content, // Mapping iOS-specific field
        json.get("fullName").jsonPrimitive.content
    )
}        

41. Your Kotlin Multiplatform app has a feature where the data needs to be saved in local storage. On Android, you’re using Room, but on iOS, you’re using SQLite. How would you handle this in Kotlin Multiplatform?

Approach:

  1. Expect/Actual for Data Access: Define a common interface for data storage in the shared code using the expect/actual mechanism.
  2. Platform-Specific Implementations: Implement the actual storage logic for each platform. For Android, use Room, and for iOS, use SQLite directly or a wrapper like GRDB.
  3. Shared Code for Business Logic: Keep the business logic in the shared code and abstract the data storage logic via interfaces.

Example Code:

// Common code (shared module)
expect class LocalDatabase {
    fun saveData(data: String)
    fun getData(): String
}

// Actual implementation for Android (Room)
actual class LocalDatabase actual constructor() {
    private val database: AppDatabase = Room.databaseBuilder(
        context,
        AppDatabase::class.java, "app-database"
    ).build()

    actual fun saveData(data: String) {
        // Room-specific code to save data
    }

    actual fun getData(): String {
        // Room-specific code to retrieve data
        return "Data from Android"
    }
}

// Actual implementation for iOS (SQLite or GRDB)
actual class LocalDatabase actual constructor() {
    private val database: SQLiteDatabase = SQLiteDatabase.openOrCreateDatabase("db", null)

    actual fun saveData(data: String) {
        // iOS-specific code to save data using SQLite
    }

    actual fun getData(): String {
        // iOS-specific code to retrieve data
        return "Data from iOS"
    }
}        

42. You’re developing a Kotlin Multiplatform app, and you need to implement a feature that requires access to platform-specific hardware features (e.g., camera on Android, and photo library on iOS). How would you implement this feature?

Approach:

  1. Platform-Specific Code: Access hardware features like the camera or photo library via platform-specific APIs. For Android, you would use Camera2 API or CameraX, and for iOS, you would use UIImagePickerController.
  2. Expect/Actual Mechanism: Write a common interface in shared code for accessing hardware features and then implement platform-specific functionality using expect/actual.
  3. Permissions: Ensure that you handle platform-specific permissions for accessing the camera or photo library (e.g., Manifest.permission.CAMERA on Android and NSCameraUsageDescription in iOS’s Info.plist).

Example Code:

// Common code (shared module)
expect fun capturePhoto(): String

// Actual implementation for Android
actual fun capturePhoto(): String {
    // Android-specific camera logic using CameraX or Camera2 API
    return "Photo captured on Android"
}

// Actual implementation for iOS
actual fun capturePhoto(): String {
    // iOS-specific camera logic using UIImagePickerController
    return "Photo captured on iOS"
}        

Summary:

Kotlin Multiplatform is NOT about “write once and forget” — it’s about “write core logic once, build great native apps”.  If you love Kotlin and want to work across Android, iOS, and other platforms without learning 5 different languages, then KMP is made for you!


Thank you for reading. 🙌🙏✌.

Found this helpful? Don’t forgot to clap 👏 and follow me for more such useful articles about Android development and Kotlin or buy us a coffee here

If you need any help related to Android, Kotlin. I’m always happy to help you.

Follow me on:

Medium, Github, Instagram , YouTube & WhatsApp

To view or add a comment, sign in

Others also viewed

Explore topics