How KMP work

Learn about the development, compilation, and execution phases to see how KMP simplifies

Codefy Labs's photo
·

3 min read

How KMP work

Here's an easy breakdown of how a Kotlin Multiplatform project flows from start to finish:

  • Development Phase: You start by writing common code that works everywhere and noting down what each platform needs to handle specifically. This is where you set up "expectations."

  • Compilation Phase: When you compile your project, the Kotlin compiler checks that every 'expect' in your common code has a matching 'actual' in each platform-specific module. If something's missing, you'll get a compilation error—a friendly reminder to complete your work.

  • Execution Phase: Once everything is running, the magic happens. Depending on whether you’re on an Android phone or an iPhone, the platform-specific code takes over, making sure your app works well on any device.

The standard way of working with Kotlin Multiplatform: we use shared code as much as possible in commonMain, and when we need platform-specific implementations, we use the expected/actual syntax. Now, at this point, you may wonder: but how does my Kotlin code have a reference to something on iOS? (the AVPlayer is not from Kotlin, but from Swift/Objective-C). The explanation follows below.

Kotlin native and interoperability with Object-C and Swift

Kotlin Native is what allows our Kotlin code to work smoothly with Swift and Objective-C. Think of it as a translator that doesn't need the Java Virtual Machine (JVM). Instead of using the JVM, which is common for Android or backend services, Kotlin Native compiles your code directly into the machine's native language.

Here's how it works: Kotlin Native has its own compiler, powered by LLVM—a well-known compiler technology that works with many hardware setups and operating systems like iOS, macOS, Windows, and Linux. This makes it great for cross-platform development, letting your app interact with APIs written in languages like C, C++, Swift, and Objective-C.

The best part? Since it translates directly to native machine code, your apps run faster and more efficiently than those running in a virtual machine. This efficiency is crucial in Kotlin Multiplatform projects, making Kotlin Native an important tool for ensuring your app performs well on different platforms. It's like having a universal adapter that makes sure your app works great no matter where it's used!

Kotlin Multiplatform is for who?

Kotlin Multiplatform is great for Android developers who want to work on multiple platforms. We've talked about iOS and Android, but you can also use it for the web, JVM, Linux, and more. A cool thing is that the tools and ecosystem Android developers know—like Android Studio, Gradle builds, and Jetpack Compose—work well with Kotlin Multiplatform. About 90% of what you know can be used directly, with only about 10% needing changes for things like iOS APIs.

Kotlin Multiplatform is a great option for creating products that work on different platforms. It uses the skills and knowledge you already have, making it a smart choice for any modern development team.

Wrapping It Up

Kotlin Multiplatform is a game-changer for businesses wanting an easy and efficient way to build apps for multiple platforms. The creators of Kotlin designed it to meet business needs, making sure your app can handle various tasks smoothly.

Plus, Kotlin Multiplatform works well with Java libraries. This means you can use a lot of existing code and modules to start your project quickly.

So, if you’re looking for a reliable and efficient tool for cross-platform app development, Kotlin Multiplatform should definitely be on your list.