If your goal is to leverage Kotlin to share code among your native apps, this is the book for you.
Maintaining multiple native apps with duplicated code can be a time-consuming process. This duplication also increases the testing effort, eventually slowing down the project and increasing costs.
You can use Kotlin Multiplatform to share code between your Android, iOS and desktop apps but there are multiple considerations. You should be able to develop the UI natively using the framework of your choice. Using the right frameworks can drastically reduce the UI development time and provide you with flexible APIs.
At the same time, you need to figure out how Kotlin Multiplatform fits in with your current architecture and how you can access platform-specific APIs. Choosing the right architecture can make your app testable, maintainable and easy to work with.
Then you need to figure out which layers of your app you can migrate to a shared module and how you can use different libraries to assist this migration. Finally, you should be able to publish and share your shared module so that you can use it across apps on multiple platforms.
How to read this book
In this book, every chapter contains theory about the specific topic and a simple practical task to learn implementation faster. There are three awesome applications you’ll develop — one per section. They each focus on important aspects of Kotlin Multiplatform.
To learn and notice every little detail, read the chapters in order. However, this book is for advanced users, and you might want to skip some chapters. In that case, be sure to continue from the starter project of the chapter you’re moving to. The starter projects contain all steps implemented in the previous chapters of a certain section.
While going through the chapter, you can type the code in Android Studio immediately. Feel free to play with the code and investigate the references provided in the chapter. Some of the chapters contain fun challenges for you to expand upon the topics you learned.
This book is split into three main sections:
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.
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.
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.