Kennis Blogs Everything You Need to Know About Kotlin Multiplatform

Everything You Need to Know About Kotlin Multiplatform

Kotlin is a programming language on the rise. Jetbrains, the brains behind this language, is putting a lot of time and effort in developing this language. Kotlin is already widely used for backend and Android applications and has excellent integration with Spring Boot. Kotlin is hot right now and has proven itself in practice. In both backend and Android applications, Kotlin is compiled to Java bytecode. It has seamless integration with Java. Making it possible to call Java classes from Kotlin and vice versa.

 

Kotlin Multiplatform

In 2013, Jetbrains also released Kotlin JS, making it possible to compile Kotlin to Javascript instead of Java bytecode. Due to this, it's possible to use Kotlin for frontend applications. In 2017 Jetbrains went even further with the release of Kotlin Multiplatform in experimental form. It gave us an insight at Jetbrains plans for the future. Multiplatform combines Kotlin for backend with Kotlin for Android and frontend and also the new possibility of using Kotlin for iOS applications.

 

The idea is simple and not new; use a single project to build an application targeting both mobile, web and backend. The innovative part is sharing as much of the code as possible, instead of generating code for each platform. A lot of the logic is shared between platforms and the user interface is build natively. Developers build the application views like the are used to in iOS and android, but fill the views using shared logic. The philosophy is; let Xcode and Android Studios do what they are good at, but write the underlying code just once.

 

Business logic can be shared by different frontend applications. Models and validators for example can also be shared with the backend. No more inconsistencies between the back- and frontend! Refactors are also made easy, by refactoring all applications simultaneously automatically.


kotlin-1.2-1-768x489

Different common modules are used to share code between the different platforms. Special libraries can be used, offering the same functionality for all available platforms. Those libraries add extra functionality, like we are used to, with the addition of working the same on all platforms. Because of this, the library can be called from common code without problems and no differences between the platforms they are run on!

architecture

It's even possible to use a comparable way to call platform-specific code from common code. An example use case is to send a push message from common code. The implementation of a push message is different on Android and iOS. Using a construction called an 'expected' class, this can still be realized. An expected class is like an interface; it has a definition of a class and its functions. Every platform needs to have an 'actual' implementation of the expected class; like the implementation of an interface. When executing the common code, the platform-specific code is called. In this case to send a push message whether the code is run on Android or iOS.

 

That sounds good right?

My enthusiasm is high because of the following advantages:

  • Write code just once for backend and frontend
  • No inconsistencies between models
  • Easy refactors
  • Easy to add another platform
  • Apps are native and so is the feeling

 

At this moment the above is only a vision of how Jetbrains wants multiplatform to work. The technology is in its infancy and for the time being is only used in two applications in practice. (The KotlinConf app and the Kotlin Academy, a website/app with blogs about Kotlin). My experience so far: getting an application up-and-running is a lot of trouble. The experimental status is very noticeable: the documentation and examples are often outdated and are very limited. Standard libraries don't always function well and experimental dependencies could break at any time when they are updated.

 

In the future multiplatform will hopefully develop as a production ready product. To achieve this multiplatform needs a stable core of libraries and more Kotlin JS frameworks. Right now React has a lot of potential! My recommendation is to not use multiplatform in practice in the near future, but definitely try it out and play with it!

 

References

Harris, Richard.why-kotlin-language-use-is-skyrocketing, Appdevelopermagazine

https://kotlinlang.org/docs/reference/java-interop.html, Kotlinlang

Images

Rozov, Kirill. Sharing code between platforms

Moskala, Marcin. Multiplatform native development in Kotlin. Now with iOS! Kotlin AcademySo how does that work?