Native Technologies

What is Kotlin Multiplatform? What is the difference between KMM and KMP?

Kotlin Multiplatform (Mobile), tech is taking off in 2021! The result of several years of development and innovation, it is in the process of succeeding in a tour de force: easily sharing code between an application ios and android without the trade-offs inherent in frameworks Cross-platform (for example React Native or Flutter).

But first of all, what is Kotlin ?

Kotlin IS from Java

It is a programming language originally intended for the Java ecosystem (JVM).

It was created by the publisher Jetbrains to improve Java.

It brings a more modern and more concise syntax, but also new features for Java developers: Null Safety, Coroutines, extension functions... While remaining 100% compatible with this language.

You can easily mix Java and Kotlin in the same project and thus migrate smoothly to this language. For more information, see our article on Why choose Kotlin for your project ?

Kotlin versions up to now

Kotlin speaks JS and “Native”

During its development, the designers of Kotlin made possible something quite innovative: the possibility of running Kotlin outside of the JVM it was originally designed for!

So we can write Kotlin and run it in a Javascript environment (in the browser or in an environment) Node.js) or native: we then do without a VM to run our program, and we can compile it for a whole bunch of platforms: Windows, Linux, MacOS or even ios !

The many frameworks that Kotlin supports

At this point, we can already write an algorithm in Kotlin and copy and paste into projects supporting different platforms, but not an entire application!

Indeed, the APIs of the different systems are completely incompatible (you don't draw a button or you don't access the network in the same way under Windows and under ios).

In addition, not all of these platforms use the same language to develop applications: C/C++ to Windows, Objective-C or Swift to ios and MacOS,...

It is these two problems that Kotlin Multiplatform Propose to solve.

Kotlin IS BECOMING Multiplatform

Kotlin Multiplatform, it is an SDK, that is to say a set of development tools that allow you to run Kotlin... on multiple platforms!

Among these tools, we find language interoperability Kotlin with the languages of other supported platforms. Indeed, although most languages allow you to call libraries written in another language (very often only C is supported), there is a tedious manual work to be done for each library and for each function of the library. The interoperability tools of Kotlin Multiplatform provide this code automatically and in both directions (you can call code Kotlin from a target language or call the code of a library written in another language from Kotlin).

Mapping types from Kotlin to Swift and Objective-C

Beyond language, the tools also make it possible to produce libraries in the same format as the target language. For example, developers ios are used to using libraries in the format .framework Written in Swift or in Objective C. The SDK Kotlin Multiplatform makes it possible to produce libraries in this same format. A developer ios will be able to use it in the same way as any other library, without even knowing that it was written in Kotlin !

Another innovation from Kotlin Multiplatform is the possibility of having different implementations for some functions depending on the platform on which the program is running. Indeed, as seen above, in some cases it is necessary to run code specific to the target platform. To solve this problem, Kotlin Multiplatform proposes to write the function several times (one for each platform) and will choose the correct one when compiling the program.

 

Kotlin Multiplatform is becoming a platform

At this point, let's focus on the most popular use case: writing code once and running it at a time on ios And on android.

As seen previously, we technically have all the tools to do this, but the fact remains that for everything related to the platform, we have to write the code 2 times.

Moreover, even if the ecosystem Kotlin is rich in hundreds of quality libraries, they are generally designed to only run in the JVM environment: impossible to reuse them as they are in our multiplatform code.

However, libraries are generally used a lot for an application: to access the network, to serialize/deserialize data, to manage a local database, dates...

The good news is that we now have a good number of libraries that are compatible Multiplatform ! Written by Jetbrains or by the community, we find for example Ktor to manage network calls, SQLDelight to manage a local database, Koin to do dependency injection...

Thanks to these libraries, we have almost no No need to write platform-specific code (except for the UI), and all written code can run on both platforms!

Kotlin Multiplatform (KMP) and Kotlin Multiplatform Mobile (KMM)

KOtlin MUltiPLatform (KMP) is the name of the SDK described in the preceding paragraphs. However, a large part of uses is directed towards code sharing between ios and android. It is therefore towards this use case that Jetbrains is focusing its efforts. KOtlin MMultiplatformMMobile (KMM) is therefore the name given to the subset of KMP for mobile uses, as well as to a plugin for Intellij (the Jetbrains IDE that allows, among other things, to code in Kotlin) which makes it easy to develop for ios and android.

The architecture of a KMM application

A mobile application is often structured in several layers. The diaper UI which contains the code for everything visible on the screen, a layer of business rules (Domain) which contains the application logic and data structures, and finally a layer called Data which contains the logic for retrieving data from the network and persisting data (for example, a database).

Today, the promoters of KMM consider that we can share the code of the Domain and Data layers, and keep the implementation of the UI specific to each platform.

It is this architecture that will give the maximum flexibility and possibilities of evolution to applications. Keeping a UI layer implemented natively allows you to design a UI that takes into account the specificities of each platform and to immediately integrate the graphic innovations of the new versions ofios And ofandroid.

How to use Kotlin Multiplatform Mobile (KMM) today

Today several renowned companies develop all or part of their product using KMM. It is therefore very easy to start a KMM project: using the templates available in Intellij, you can generate a cross-platform application skeleton in a few clicks.

The library ecosystem is growing every day and Jetbrains is working to put KMM into beta (and therefore get it out of its experimental status) by next spring.

Are you hesitating? Let's talk about it!

Développeur mobile ?

Rejoins nos équipes