How to Create a Kotlin Multiplatform Library
We will go through two scenarios: the first is to create a project from scratch with complete configuration, and the second shows how to add an iOS module to an existing Android project.
Requirements for creating a Kotlin Multiplatform library:
- The latest version of Android Studio is preferred, but here's a list of the versions this plugin runs on – Kotlin Multiplatform Mobile - IntelliJ IDEs Plugin | Marketplace.
- A plugin that supports the development of applications for IOS and Android platforms – Kotlin Multiplatform Mobile - IntelliJ IDEs Plugin | Marketplace.
- The latest Xcode.
- To run the iOS emulator in Android Studio, it must be configured appropriately
We will be using XCFramework as a way to add dependencies to the iOS applications.
First Scenario: Creating an application from scratch
- Create Project and configure Gradle file
Sample of how to create an application from scratch
A short note: the iOS module is already automatically configured.
- Launching the application
Configure the iOS simulator
Sample of how to launch the application on both platforms
Second Scenario: Add iOS Module to existing Android application
- Create Project and configure Gradle file
Sample of how to add the module to an existing project
- Link extra module to iOS app
Sample of how to link added module to iOS app
More details: Multiplatform Gradle Plugin Improved for Connecting KMM Modules | The Kotlin Blog
You may need to clear the cache in Android Studio and XCode.
Building a Kotlin Multiplatform library
We presented to you the project requirements and two scenerios to create a Kotlin KMM library. We hope this article can help you speed up your multiplatform projects. If you are interested in learning more about Kotlin Multiplatform you may read How to Handle Resources in Kotlin Multiplatform.