1. 程式人生 > >Kotlin and the rise of Multiplatform

Kotlin and the rise of Multiplatform

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

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.

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.