avatarDiego Jimenez

Summary

Kotlin Multiplatform Mobile (KMM) enables developers to share Kotlin code between Android and iOS platforms, streamlining the development process for mobile applications.

Abstract

Kotlin Multiplatform Mobile (KMM) is a technology developed by JetBrains that allows for the creation of cross-platform mobile applications using a single Kotlin codebase. This approach enables developers to write business logic, data access, and other core features once and deploy them across both Android and iOS platforms. KMM integrates with mobile frameworks such as SwiftUI and Jetpack Compose, and it compiles to native code for each platform, ensuring optimal performance. The technology is relatively new and rapidly evolving, offering significant time and cost savings, as well as improved code consistency and maintainability for mobile app development teams. The article outlines the steps for integrating KMM into iOS projects using Swift and vice versa, demonstrating the bidirectional potential of KMM in mobile app development.

Opinions

  • KMM is recognized as an exciting and evolving technology in the mobile app development space, with ongoing updates and improvements.
  • The ability to share code across platforms is highlighted as a key benefit of KMM, reducing duplicate efforts and enhancing maintainability.
  • Integration of KMM with popular mobile frameworks is seen as a valuable feature, leveraging existing tools and skills in the development process.
  • The article suggests that KMM's integration capabilities make it a priceless asset for Android projects, emphasizing the value of being able to use Swift modules in Kotlin.
  • The process of integrating Swift modules into Kotlin projects is presented as a straightforward and beneficial approach, showcasing the versatility of KMM.

How To Use Kotlin Multiplatform Mobile (KMM) From Swift To Kotlin

Import Swift modules to any Kotlin project

Kotlin Multiplatform Mobile (KMM) is a technology developed by JetBrains, the creators of the Kotlin programming language, that allows developers to build cross-platform mobile applications using Kotlin. KMM enables developers to share common code between Android and iOS platforms, allowing them to write their business logic, data access, and other core features once and deploy them on multiple platforms.

KMM allows developers to share code between Android and iOS platforms through a common codebase written in Kotlin, which is then compiled into native code for each platform. This approach allows developers to reuse their existing Kotlin skills and tools while leveraging the benefits of native development on each platform. KMM also provides integration with popular mobile frameworks such as SwiftUI and Jetpack Compose.

One of the key benefits of KMM is the ability to share code for business logic, data access, and other core features between platforms, reducing the amount of duplicate code that needs to be written for each platform. This can lead to significant time and cost savings for development teams, as well as improved code consistency and maintainability.

KMM is still a relatively new technology and is evolving rapidly, with ongoing updates and improvements to its functionality and features. As such, it is an exciting area of mobile app development to watch and explore for developers looking to build cross-platform mobile applications.

Let’s see how to integrate one programming language in other, Kotlin to Swift modules and the target of this article, Swift module to Kotlin projects.

Kotlin to Swift: The natural approach

To integrate Kotlin Multiplatform Mobile (KMM) into an iOS project written in Swift, you can follow these general steps:

Create a new KMM module: You can create a new KMM module in your project by following the steps outlined in the KMM documentation. This will generate a Kotlin shared module that can be used to write cross-platform code.

Configure the KMM module for iOS: You’ll need to configure the KMM module to work with iOS by specifying the target platform and architecture. You can do this in the build.gradle.kts file of the shared module by adding the following code snippet:

kotlin {
    ios {
        binaries {
            framework("MyFramework") {
                baseName = "MyFramework"
            }
        }
    }
}

This will generate an iOS framework that you can use in your Swift code.

Export the KMM module as a framework: You’ll need to export the KMM module as a framework that can be used in your iOS project. You can do this by building the shared module using the ./gradlew :shared:assemble command, which will generate the framework in the shared/build/xcode-frameworks directory.

Add the KMM framework to your iOS project: You can add the KMM framework to your iOS project by dragging and dropping it into the project navigator in Xcode. Make sure to check the “Copy items if needed” checkbox to copy the framework into your project’s directory.

Import the KMM framework in your Swift code: You can import the KMM framework in your Swift code by adding the following line at the top of your Swift file:

import MyFramework

This will allow you to use the shared Kotlin code in your iOS project.

These are the general steps to integrate KMM into an iOS project written in Swift. However, the specific details may vary depending on your project’s configuration and requirements, so it’s important to refer to the KMM documentation and any other relevant resources for more detailed guidance.

Swift to Kotlin: Priceless help for Android projects

Now we are going to show how to integrate Swift moduels into Kotlin projects.

Here’s an example of creating a Swift module for loading an image from a URL:

Create a new Swift framework: In Xcode, select File > New > Project and choose the “Framework” template. Choose Swift as the language and select “iOS” as the platform.

Create a public function:

You’ll need to create a public function that can be used by the Kotlin code to load an image from a URL. The function should take a URL as a parameter and return a UIImage object.

import UIKit

public func loadImage(from url: URL) -> UIImage? {
    guard let data = try? Data(contentsOf: url) else {
        return nil
    }
    
    return UIImage(data: data)
}

This function uses the Data(contentsOf:) method to download the data from the URL and then creates a UIImage object from the data.

Build the Swift framework:

Select Product > Build in Xcode to build the framework.

Add the Swift framework to your Kotlin project: Copy the .framework file into the libs directory in the shared module of your KMM project.

Configure the Kotlin project to use the Swift framework:

In the build.gradle.kts file of the shared module, add the following code snippet:

kotlin {
    iosX64("ios") {
        binaries {
            framework {
                baseName = "MyImageLoader"
                exportedSymbols.add("loadImageFromURL")
                linkerOpts("-framework", "MyImageLoader")
            }
        }
    }
}

This will create a Kotlin/Native framework that can use the Swift framework. The exportedSymbols property specifies that the loadImageFromURL function should be exposed to Kotlin.

Use the Swift module in your Kotlin code:

You can use the Swift module in your Kotlin code by importing the module and calling the loadImageFromURL function.

import platform.UIKit.UIImage
import MyImageLoader.loadImageFromURL

fun loadImage(url: String): UIImage? {
    return loadImageFromURL(NSURL(string = url)!!)
}

This function imports the UIImage class from the UIKit framework, imports the loadImageFromURL function from the Swift module, and then calls the function with the URL as a parameter. The function returns a UIImage object.

Conclusion

These are the general steps to create a Swift module that can be used in Kotlin Multiplatform Mobile.

However, the specific details may vary depending on your project’s configuration and requirements, so it’s important to refer to the KMM documentation and any other relevant resources for more detailed guidance.

iOS
iOS App Development
Swift
Kotlin
Xcode
Recommended from ReadMedium