Create your Kotlin Multiplatform library – tutorial
In this tutorial, you'll learn how to create a multiplatform library in IntelliJ IDEA, publish the library to a local Maven repository, and add it as a dependency in another project.
The tutorial is based on our multiplatform library template, which is a simple library containing a function to generate the Fibonacci sequence.
Set up the environment
Install all the necessary tools and update them to the latest versions.
Create a project
In IntelliJ IDEA, select File | New | Project from Version Control.
Enter the URL of the multiplatform library template project:
https://github.com/Kotlin/multiplatform-library-templateClick Clone.
Examine the project structure
The Kotlin Multiplatform library template project provides a foundational structure for developing Kotlin Multiplatform libraries. This template helps create libraries that can operate across various platforms.
In the template project, library
serves as the core module and contains the main source code and build resources for the Multiplatform library.

The library
module is structured to accommodate shared code as well as platform-specific implementations. Here's a breakdown of the contents in its main source code (src
):
commonMain
: contains Kotlin code shared across all target platforms. This is where you place code that doesn't rely on any platform-specific APIs.androidMain
,iosMain
,jvmMain
, andlinuxX64Main
: contain code specific to the Android, iOS, JVM, and Linux platforms. This is where you implement functionalities that are unique to these platforms.commonTest
,androidUnitTest
,iosTest
,jvmTest
, andlinuxX64Test
: contain tests for the sharedcommonMain
code and tests specific to the Android, iOS, JVM, and Linux platforms, respectively.
Let's focus on the library
code that is shared across all platforms. Inside the src/commonMain/kotlin
directory, you can find the CustomFibi.kt
file with Kotlin Multiplatform code that defines a Fibonacci sequence generator:
The firstElement
and secondElement
properties are placeholders that the platform-specific code can implement. Each target should provide actual values by using the actual
keyword in their respective source sets.
The expect
declarations are matched with actual
implementations. This mechanism is useful when writing cross-platform code that requires platform-specific behavior.
In this case, the multiplatform library template includes platform-specific implementations of the firstElement
and secondElement
properties. The androidMain
, iosMain
, jvmMain
, and linuxX64Main
directories contain actual
declarations that provide values for these properties.
For example, here's the Android implementation included in androidMain/kotlin/fibiprops.android.kt
:
The other platforms follow the same pattern, with variations in the values of firstElement
and secondElement
properties.
Add a new platform
Now that you're familiar with how shared and platform-specific code work in the template, let's extend the project by adding support for an additional platform.
Configure support for the Kotlin/Wasm platform by using the expect
/actual
mechanism. You can implement platform-specific functionality for the firstElement
and secondElement
properties.
Add the Kotlin/Wasm target to your project
In the
library/build.gradle.kts
file, add the Kotlin/Wasm target (wasmJs
) and source sets:kotlin { // ... @OptIn(org.jetbrains.kotlin.gradle.ExperimentalWasmDsl::class) wasmJs { browser() // ... binaries.executable() } // ... sourceSets { //... val wasmJsMain by getting { dependencies { // Wasm-specific dependencies } } } }Synchronize the Gradle files by clicking the Sync Gradle Changes icon (
) that appears in your build file. Alternatively, click the refresh button in the Gradle tool window.
Create platform-specific code for Wasm
After adding the Wasm target, you need a Wasm directory to hold the platform-specific implementation of firstElement
and secondElement
:
Right-click the
library/src
directory and select New | Directory.Select wasmJsMain/kotlin from the Gradle Source Sets lists.
Right-click the newly created
wasmJsMain/kotlin
directory and select New | Kotlin Class/File.Enter fibiprops.wasm as the name of the file and select File.
Add the following code to the
fibiprops.wasm.kt
file:package io.github.kotlin.fibonacci actual val firstElement: Int = 3 actual val secondElement: Int = 5This code sets a Wasm-specific implementation, defining
actual
values forfirstElement
as3
andsecondElement
as5
.
Build the project
Make sure your project compiles correctly with the new platform:
Open the Gradle tool window by selecting View | Tool Windows | Gradle.
In multiplatform-library-template | library | Tasks | build, run the build task.
Alternatively, run the following command in the terminal from the
multiplatform-library-template
root directory:./gradlew build
You can see the successful output in the Build tool window.
Publish your library to the local Maven repository
Your multiplatform library is ready for publishing locally so that you can use it in other projects on the same machine.
To publish your library, use the maven-publish
Gradle plugin as follows:
In the
library/build.gradle.kts
file, locate theplugins { }
block and apply themaven-publish
plugin:plugins { // ... // Add the following line: id("maven-publish") }Locate the
mavenPublishing { }
block and comment out thesignAllPublications()
method to indicate that the publication is local-only:mavenPublishing{ // ... // Comment out the following method: // signAllPublications() }Synchronize the Gradle files by clicking the Sync Gradle Changes icon (
) that appears in your build file. Alternatively, click the refresh button on the Gradle tool window.
In the Gradle tool window, go to multiplatform-library-template | Tasks | publishing and run the publishToMavenLocal Gradle task.
Alternatively, run the following command in the terminal from the
multiplatform-library-template
root directory:./gradlew publishToMavenLocal
Your library is published to the local Maven repository.
To locate your published library, use your file explorer or terminal and navigate to .m2\repository\io\github\kotlin\library\1.0.0\
in your user home directory.
Add your library as a dependency in another project
After publishing your Multiplatform library to the local Maven repository, you can use it in other Kotlin projects on the same machine.
In your consumer project's build.gradle.kts
file, add a dependency on the published library:
The repositories{}
block tells Gradle to resolve the library from the local Maven repository and make it available in shared code.
The implementation
dependency consists of your library's group and version specified in its build.gradle.kts
file.
If you're adding it to another multiplatform project, you can add it to shared or platform-specific source sets:
Sync the consumer project and start using your library!
What's next
We encourage you to explore multiplatform development further:
Join the community:
Compose Multiplatform GitHub: star the repository and contribute
Kotlin Slack: Get an invitation and join the #multiplatform channel
Stack Overflow: Subscribe to the "kotlin-multiplatform" tag
Kotlin YouTube channel: Subscribe and watch videos about Kotlin Multiplatform