This chapter has hopefully illustrated the interesting potential of Kotlin multiplatform to achieve code sharing between multiple targets. Kotlin multiplatform differs from other cross-platform solutions. In this, it aims to share logic and models, not UI. Because of this distinction, Kotlin multiplatform offers a lower-risk proposition when compared to frameworks such as React Native or Flutter. It is quite simple to write a small function or model in Kotlin and then share that across multiple platforms, where it can be consumed as a simple dependency.
Kotlin multiplatform projects can be configured and built via Gradle, making it easy to define various build targets and then generate any required build artifacts. Within a Kotlin multiplatform project, implementations can be shared across all platforms, or they can be customized based on each platform using the expect and actual keywords. By leveraging these keywords, and clean software engineering practices, it's possible to create common APIs that leverage platform-specific tools and implementations where needed.
While Kotlin multiplatform is a promising proposition for achieving code sharing, it does have its limitations. The Kotlin multiplatform ecosystem is still in its infancy, and may not have third-party libraries available for tools/services you're accustomed to using such as Firebase, or simple logging. Additionally, there are complexities associated with how threading works across various platforms that may require a strong understanding of concurrency to ensure your applications are performant and error free. As the Kotlin multiplatform ecosystem and support continue to advance, it will likely become easier and easier to build applications using shared Kotlin code.
In the next chapter, we'll explore how Kotlin can be used to build backend services.