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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
In this chapter, you’ll learn how to use deal with concurrency on your Kotlin Multiplatform projects.
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.
Section IV: Appendices
In this chapter, you’ll learn about several functionalities of Kotlin and their comparison with Swift.
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.
In this chapter, you’ll learn how you can share your Compose UI between Android and desktop apps.