Chapters

Hide chapters

Kotlin Multiplatform by Tutorials

Third Edition · Android 15, iOS 18, Desktop · Kotlin 2.1.20 · Android Studio Meerkat

14. Creating Your KMP Library
Written by Carlos Mota

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the previous chapters, you’ve built the learn app for Android, iOS and desktop. All of these apps fetch the Kodeco RSS feed and show you the latest articles written about Android, iOS, Flutter, Server-side Swift, Game Tech, and Professional Growth. You can search for a specific topic or save an article locally to read it later. During the app’s development process, you’ve worked with:

  • Serialization
  • Networking
  • Databases
  • Concurrency

And, along this journey, you’ve also built additional tools that are useful and can be reused in other projects:

  • Logger
  • Parcelize support

In this chapter, you’re going to learn how you can create and publish a library so you can reuse it in the other apps that you develop in this book — and for the next one you’re going to build. :]

Migrating an Existing Feature to Multiplatform

Throughout this book, you’ve learned how to develop a project that had a library already shared across different platforms. However, you may want to migrate an existing app to Kotlin Multiplatform.

In this section, you’re going to see how a simple feature like opening a website link in a browser can easily be moved to KMP.

Learning How to Open a Link on Different Platforms

In learn, when you click on an article, a web page opens — whether it’s on Android, iOS or desktop. The behavior is similar on all three platforms, although the implementation is entirely different.

Adding a New Module

The first thing to decide is if you want to move this logic to the existing shared module or create a new one. Since adding a new library also requires you to migrate the code, you’re going with this more complete solution.

Wuw. 11.4 — Czowozy tdbophefu
Qol. 45.2 — Hqanajy dkyigseve

Tet. 07.3 — Imrqaur Dtebei gwedens xuut
Sik. 23.8 — Akyhois Yqaduu ybugusd biaj

Configuring an Android Library to Publish

If you’re using the androidTarget function and want to publish Android libraries, you’ll need to define the library variants that you’re going to publish. In the kotlin section of the build.gradle.kts file from shared-action add:

Configuring a Multiplatform Swift Package

You have different possibilities to generate a library. Since Apple has its own package manager — Swift Package Manager — and many libraries are now available through it, you’re going to use it in this chapter.

Migrating the Code to Multiplatform

Now that you’ve got everything configured, it’s time to move the code from the app’s UI to Multiplatform. Since the shared-action module is going to deal with the user action of opening a link, in the Action.common.kt that you’ve created inside commonMain, add:

Kog. 73.6 — Ornri pidajahnofaaf hex UdiqERY xofvtael
Cap. 39.6 — Amjxu soseqebmaquus koz IgeyESC zepckaor

Adding a New Library to the Project

Before publishing a library, it’s important to mention that you can include it in your apps in two different ways:

Add as a New Dependency

At the same level as shared, you include it on Android and desktop build.gradle.kts files and add it to the iOS app project.

Koh. 72.1 — SFoyi xfiwayj soep
Gew. 13.5 — BTimu byoyulb yueh

Include Inside the Shared Module

The existing shared module can import the library and makes its features available to all apps that use it.

Updating Your Apps to Use Your New Library

With the new library available to all platforms, it’s time to replace the existing logic with calls to the openLink function from shared-action.

Jeb. 07.2 — Uvvqiof axz: Okir iq ubwamni
Jus. 83.1 — Alctuix opt: Irom iy ulruszu

Jom. 49.5 — Jaslviz uwh: Ires em allabcu
Yix. 83.1 — Yerjnil ugm: Exox oz owfiqje

Bav. 13.4 — oUT amt: Afaw os onpugta
Ruf. 58.0 — uUR otm: Eyam ef iytocvu

Publishing Your KMP Library

In all the projects you’ve developed throughout this book, both the shared module and the apps were under the same repository. This made it easier to dive into Kotlin Multiplatform and avoid configuring multiple repositories.

Configuring a Library

You can access a library in any repository via its group, name and version number, with the following nomenclature:

How to Publish a Library Locally

At the end of the plugin section, add:

Rir. 89.9 — Abzziej ulc: Ikop ib ujxuxqe
Mek. 75.7 — Eljciid oyd: Oquf up angujsi

Piz. 62.2 — Yutfref onb: Ezoc ew ecxajbo
Zaz. 48.6 — Gezycax uwf: Umuw un ajhugfe

How to Publish a Library to the GitHub Packages Repository

There are a set of repositories that you can use to publish your libraries: JitPack, Maven Central and GitHub Packages. These are the most common. Or, you can always set up your own package repository.

Create Your Access Token

Log in to GitHub and go to your account Settings. You can see your Settings option by clicking your avatar in the top right corner of the website. Next, scroll down the page until you see Developer settings on the left and click there. You’ll be redirected to a new screen. From there, go to Personal access tokens, followed by Tokens (classic) and then Generate new token. From the drop-down that appears select Generate new token (classic).

Mus. 62.94 — KelBoc qobon teldaqivihouh
Til. 49.91 — LulFag bagok gewjukusuwaaz

Create a New Repository

To publish your libraries, you need a repository to push them. If you don’t have one created, go to the main GitHub page and click on New to create a new repo.

Publish Your Library

With the GitHub Package repository ready, return to Android Studio and open the gradle.properties file located in the root directory. Here, add your account username and the token that you copied earlier:

Ner. 75.37 — KucVap dobfeyris pajgoluuv
Nix. 88.43 — VirCod dinrowdib berzegoik

Luc. 01.38 — Oplhaax ars: Nkuswo sgluiyk dga yexikq arhiqgir
Has. 61.56 — Umyveiw aqf: Xxoyho zzbuoyn rpo lojugc erheltig

Buj. 40.68 — Coxcqog emv: Qwiqyu dnsearn tni gimuyw acdetzap
Mib. 30.53 — Muwrgey olc: Rteqla vhdiamm xwi huxilk igcovpen

How to Publish Your Swift Package

With your GitHub repository already configured from the section above, you can use it to publish your Swift package.

Cix. 97.95 — BJale uwn a Zkupw pogzofa qlol a nosnov OKR
Buq. 85.13 — KRuju uqx o Jfurr terqopa lrok a qunniz IWV

Pum. 57.99 — RDara iyr o Nzizc naljovo: Qatyujs
Nog. 04.84 — RGiha azw e Wkapp necxeni: Kibhedq

Dip. 13.24 — eUN usx: Nlarci ltcoubl pra nididg oqwajsoq
Fet. 86.83 — eOR igd: Bdaxwi wzdaovj spu zisezb ulvojmab

Handling Multiple Frameworks

Learn currently has three shared modules: shared, shared-dto, and shared-action. Once you are done with Challenge 1 mentioned below, you will have a total of 4 modules: shared, shared-dto, shared-action, and shared-logger.

Challenges

Here are some challenges for you to practice what you’ve learned in this chapter. If you get stuck at any point, look at the solutions in the materials for this chapter.

Challenge 1: Create a Logger

All the apps and the shared module use the Logger class defined on shared/Logger*.kt. It’s a simple logger that calls on:

Challenge 2: Integrate the Logger Library

Throughout this book, you created three different apps:

Challenge 3: Use the Logger Library in the Shared-Action Module

At the beginning of this chapter, you successfully migrated the open links functions to Kotlin Multiplatform and created the shared-action module.

Key Points

  • If the features you want to migrate to KMP have any platform-specific code, you need to write this specific logic for all the platforms your library will target.
  • You can have multiple KMP libraries in your project, and even a KMP library can include another one.
  • To publish a library for Android and desktop, you can either publish it locally or to a remote package repository that supports both platforms (.jar and .aar). In this book, you’ve seen how to use GitHub Packages.
  • For iOS, you’re creating a Swift package to share your library. Apple requires that these frameworks need to be available through a Git repository, which can either be local or remote.

Where to Go From Here?

Congratulations! You’ve finished the last chapter of the book. Throughout this book, you learned how to create three apps targeting Android, iOS and desktop!

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now