Kotlin and the rise of Multiplatform

Is this the new era of building multiplatform projects?

Bruno Correia
Pink Wall

--

Photo by Henry & Co. on Unsplash

As you might know, Kotlin is becoming a very popular programming language and it’s being adopted by huge companies like Google, Netflix, JetBrains (of course 😅) among others.

But, how about Kotlin Multiplatform?

Kotlin Multiplatform Projects are a “new” way to reuse code between multiple platforms (Remember, code is like plastic, you should reuse it!). And, as opposed to other cross platform approaches, Kotlin doesn’t want you to write all your code once and use it in different platforms such as Android and iOS.

Instead, Kotlin Multiplatform focuses on sharing the part of the code that contains logic (data, business and presentation) and leaves the platform specific part to be implemented natively using its specific SDK’s and tools.

Looks cool, right? 😎

How does it work?

I’m not going to dig too much into detail in here since there are already some good posts and documentation about it.

But let me just show you the basics behind this approach. It is based on three types of modules: common, platform and regular.

The common module is the one that contains all the common code that is not specific to any platform. It’s where you should implement your data, business and presentation logic.

A platform module is responsible to implement the code that depends on a platform. The way this works is by using an expected and actual declarations mechanism. Something like this:

Expected declaration (declared in the common module)!
Actual implementation for JVM (implemented for the platform modules).

This mechanism is provided by Kotlin so we can define expected declarations in the common modules and implement its actual code in the platform specific modules¹.

The regular module is a module that targets a specific platform, like an Android and iOS application or a Web client. This is where you will implement your UI and use unique libs or API’s of each platform.

1- I know what you are thinking. This is just like an interface! Why bothering implementing a new mechanism? 🤔 The truth is that the expected and actual declarations are more powerful than interfaces. They can have constructors, top level functions and instead of writing your own code, you can use existing classes using a typealias or use external implementations.

Expectations vs Reality

Expectations vs Reality

As soon as we started to listen and read things about Kotlin multiplatform we immediately fell in love with the idea. Being able to reuse all the data, business and presentation logic for many platforms and implement the UIs natively seemed perfect.

This way, we could reuse the code that really makes sense (at least for us) and still have a native look and feel of our apps. Moreover, we can use our preferred libs for each platform (at least the UI ones). That’s great! 😃

However, what looked like a dream, in the reality, sometimes, looks like a nightmare. Let me try to explain these strong words.

First of all, I’m not saying Kotlin Multiplatform is bad. Rather, I really think that it could be the future of cross platform projects. However, it is still on the early days and it’s quite difficult, right now, to setup and develop a multiplatform project. Seriously, you really need to get your hands dirty! Read and test a lot of things and you will only succeed after having some headaches.

That being said, Kotlin Multiplatform is already great right now and I really think you should try it. But, in fact, the experience is not like we imagined.

Our experience so far…

We started a “simple” project with the aim of better understanding the potential of the Kotlin Multiplatform. And, due to the difficulties that we faced, it automatically grew up to become a template and also a sample project that everyone can use to start a Kotlin Multiplatform project.

The Kotlin Multiplatform Template was born!

It contains a backend server with an API and an Android, iOS and Web client. Check it! We hope it can help you! 🙂

So, what were the main difficulties during development?

First, you will need to have a little bit more knowledge about Gradle than I was expecting. It’s important to understand well how Gradle needs to be configured and how the modules need to be connected with each other. We learned a lot of things, but we still need to improve our Gradle configuration.

Currently, there are some very good libraries that can be used in multiplatform projects (e.g.: ktor, kotlinx.serialization, etc) but, some of them don’t work yet (or well) for all the platforms.

Moreover, some libs only work with specific versions of Gradle or Kotlin. And, therefore, you need to be very careful. Changing a version can be the reason for your project to simply stop working.

Another difficulty that we found was that by changing some common code we can break a regular module even if others still work. So, you will need to test your code very well in order to avoid surprises (I know, this is good 😎).

Other than that, current IDE’s are not prepared for multiplatform and you will need to use multiple IDE’s to build your project. Which also leads to difficulties in the debugging process.

But, not everything is bad! After having everything configured and after having a little bit of experience using Kotlin Multiplatform, developing multiple apps for different platforms is very fast. 🚀

Final thoughts

It’s been a long journey since we started this multiplatform template, things evolved, changed and this template is far from being finished. We will continue improving it and trying to update it with the most recent best practices and most used libraries (contributions are welcome 😄).

Currently, I think Kotlin Multiplatform is not ready for production yet. At least I wouldn’t use it, although some projects like the Kotlin Academy Portal by Marcin Moskala prove that I’m wrong.

Nevertheless, JetBrains and the Kotlin community are working hard to make it possible to develop Multiplatform Projects in a pleasurable way. So, I really think that Kotlin can be the future of cross platform projects or, at least, one of the most used ways to do it, like React Native is nowadays.

Let me know what are your thoughts about Kotlin Multiplatform Projects, your experiences and if you have any doubt or suggestion.

Happy coding! 🚀

References

--

--