Version First Edition
Platform Android 12, iOS 15, Desktop
Language Kotlin 1.6.10
Editor Android Studio Bumblebee

The best book to teach you how to share code across platforms using Kotlin Multiplatform. You’ll gain the foundation of creating user interfaces using native UI toolkits and then writing common code for serialization, networking and persistence. You’ll also learn how dependency injection, testing and different architectures fit in with Kotlin Multiplatform so that you build maintainable and scalable apps for the real world.

Before You Begin

This section tells you a few things you need to know before you get started, such as what you’ll need for hardware and software, where to find the project files for this book, and more.

i

iii

iv

vi

Section I: Beginning Kotlin Multiplatform

One of the core benefits of Kotlin Multiplatform is that you can share code across native apps. You can continue to develop the UI layer using native UI toolkits like Jetpack Compose for Android and SwiftUI for iOS.

In this section, you’ll learn how to add a new Gradle module to write your business logic only once. You’ll also learn how to create the native UI for Android, iOS and desktop apps, all while sharing the common module.

Kotlin Multiplatform (KMP) is one of the latest technologies to help you share code across platforms. This chapter introduces you to KMP, how you can use it across app layers and how to set up your first KMP project.

1

Having reliable build tools and consistent dependency versions is crucial to getting started on a multiplatform project. This chapter will cover how you can use Gradle and its buildSrc module to provide dependencies to all modules. You'll also learn how to share business logic across target platforms using a multiplatform time zone library.

2

Jetpack Compose is a new declarative library aimed at simplifying UI development on Android. This chapter will walk you through creating multiple screens for your app using Jetpack Compose.

3

SwiftUI is the modern way to program UI for iOS applications. In this chapter, you'll learn how to develop the UI for iOS using the SwiftUI framework.

4

It's now possible to share the same UI code across desktop and Android applications. In this chapter, you'll learn how to develop the UI for desktop using the new Compose Multiplatform declarative library.

5

Section II: Kotlin Multiplatform: Intermediate

To effectively share code across apps, there are multiple things to keep in mind: access to platform-specific APIs, support for existing software engineering practices and persistence.

In this section, you’ll learn how to use Kotlin features to access platform-specific APIs in your shared module and how Kotlin Multiplatform fits in with your current architecture. You’ll also learn about dependency injection and how you can use it to test features present in your shared modules. Finally, you’ll learn how to use a common codebase to handle persistence on different platforms.

When working with KMP, you'll often need to access platform-specific APIs. In this chapter, you'll take a deeper dive into the expect/actual pattern and how you can use them to access platform-specific APIs.

6

Software architecture is one of the most crucial aspects that affect quality and maintainability over time. In this chapter, you'll get an overview of different architectures and how you can use one of them in your KMP application.

7

Writing tests is an important part of writing code confidently. This chapter will provide an overview of adding unit tests and UI tests to an app created using KMP.

8

Dependency Injection is a technique that leads to maintainable and testable code. This chapter will provide an overview of integrating the Koin dependency injection library into a multiplatform project.

9

Most modern applications need to persist data across sessions. This chapter will provide an overview of data persistence in KMP. It discusses key-value saving as well as using an SQL database.

10

Section III: Kotlin Multiplatform: Advanced

Networking is crucial to most modern apps, and it usually involves implementing similar logic using different frameworks and languages. Under the hood, it also involves concepts like serialization and concurrency. Fortunately, Kotlin Multiplatform has dedicated libraries for each of these.

In this section, you’ll learn how to use serialization to decode JSON data to Kotlin objects. You’ll then learn how to use a common networking library that leverages this common serialization to fetch data from the internet. To make the networking performant, you’ll also learn about concurrency in Kotlin using coroutines and the considerations for different platforms. Finally, you’ll learn how to extract an existing feature to a Kotlin Multiplatform library and also different ways of publishing this library.

When transmitting data over a network, you need to serialize and deserialize it. In this chapter, you'll learn how to use serialization in a Kotlin Multiplatform project.

11

Networking is one of the core features of any modern app. In this chapter, you'll lean how to use networking in a Kotlin Multiplatform project.

12

In this chapter, you'll learn how to use deal with concurrency on your Kotlin Multiplatform projects.

13

In this chapter, you'll learn how you can migrate an existing feature to KMP. You also learn how to publish a shared library and make it available for Android, iOS and desktop.

14

Section IV: Appendices

In this chapter, you'll learn about several functionalities of Kotlin and their comparison with Swift.

A

Xcode doesn't support debugging Kotlin Native code out of the box. In this chapter, you'll learn how you can debug your shared code module from Xcode.

B

In this chapter, you'll learn how you can share your Compose UI between Android and desktop apps.

C

Meet the team

Contributors

Who is this book for

This book is for mobile developers and managers who want to explore how they can use Kotlin Multiplatform in different use cases to share code across Android, iOS and desktop apps. If you want to reduce development and testing time by writing certain parts of your apps only once, this book will help.

Concepts covered in this book

  • Jetpack Compose Android
  • Compose Multiplatform
  • SwiftUI
  • Testing
  • Dependency Injection with Koin
  • Persistence using SQLDelight
  • Serialization
  • Ktor
  • Concurrency using coroutines

Reviews

Version history

First Edition · Android 12, iOS 15, Desktop, Kotlin 1.6.10, Android Studio Bumblebee

v1.0 · Mar 16 2022 · Carlos Mota, Saeed Taheri & Kevin D Moore
See book details
Pro

Kotlin Multiplatform by Tutorials

By Carlos Mota, Saeed Taheri and Kevin D Moore

Build native apps faster and more reliably by sharing code across Android, iOS and desktop.

Read for Free with the Pro subscription* * Includes this and all other books in our online library See all benefits
Buy Individually $59.99* *Includes access to all of our online reading features.
See buying options
Hide buying options

All videos. All books.
One low price.

A raywenderlich.com subscription is the best way to learn and master mobile development — plans start at just $19.99/month!

Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.