Have you tried Kotlin Multiplatform?

Have you tried Kotlin Multiplatform?

You are reading "Goobar," an Android development newsletter exploring the latest tools, tips, and trends from the world of Android. We're dedicated to helping you learn and grow in your career as an Android developer, so if you like what you see, be sure to subscribe.

Welcome devs ??

Did you know you can use Kotlin to build applications for the web, iOS, Mac, Linux, Android, and more?

Kotlin Multiplatform makes this possible, and while the technology is still rapidly evolving, it’s quickly reaching the point of viability beyond the early adopter audience.


Have you tried Kotlin Multiplatform?


What is Kotlin Multiplatform?

So what exactly is Kotlin Multiplatform? A Kotlin Multiplatform project enables the sharing of common code across your various build targets.  

Building For Multiple Targets

Business logic, data objects, interfaces, repositories, etc can all be defined within a common module. That module is included in your project, and consumed as if it was any other third-party library. For each platform you are building for, you are free to provide platform specific implementations of classes, interfaces, properties, etc. These all exist within the specific platform modules and are consumed by any consumers of that platform module.

These platform specific implementations work using the expect and actual keywords. Using expect, you add your class/property/etc declaration to your shared module. By using the actual keyword, you define the actual platform specific implementation of the expected declaration.  

This allows a developer to take into account any platform specific dependencies or technical requirements that are necessary. While expect/actual provide a language feature specific to this type of multiplatform development, it’s also still possibly to simply define a common interface and implement that interface for each build target as well.

Currently, it’s possible to build for the following targets:

  • Javascript
  • Windows, Linux, WebAssembly
  • JVM, Android
  • macOS, tvOS, iOS, watchOS
  • And more…

For each build target you include in your configuration, you can include specific platform dependencies, write platform implementations of common classes and apis, and ultimately generate a unique build artifact to be consumed by each individual platform application.


What Is The Value Proposition Of Kotlin Multiplatform?

Why are Kotlin Multiplatform projects an interesting approach to cross platform development?  

Using a Kotlin Multiplatform approach, teams can share code using a modern, high-level language, without having to cover the entire api surface of target platforms. Kotlin Multiplatform aims to simplify code sharing by leaving it up to the developer to decide how much code needs to be shared.  

Only want to share one specific, core algorithm? That’s fine. You can build a Kotlin Multiplatform project that includes a single function, or the majority of your app’s logic, models, etc.

Because Kotlin Multiplatform projects aren’t aiming to build an entire cross-platform application, the shared code is consumed as you would any other library or module. This makes it a straightforward task to swap that module out for something else if it fails to meet your needs. This flexibility enables teams to integrate Kotlin Multiplatform without locking themselves into a new tech stack for their entire application. This lowers the barrier to entry for trying this method of code sharing and for making a determination about whether it might be viable for your project or team.


How Does Kotlin Multiplatform Differ From Other Cross-Platform Approaches?

How is a Kotlin Multiplatform project different from other cross-platform approaches? Why does Kotlin Multiplatform exist when we already have solutions such as Flutter or React Native?

These are understandable questions. To me, while Kotlin Multiplatform is similar to these other cross-platform solutions, it’s not in direct competition with them. A Kotlin Multiplatform project isn’t promising a build once deploy everywhere solution. Kotlin Multiplatform isn’t trying to unify all platform apis. It’s not trying to simplify or recreate the UI framework for target platforms.  

Kotlin Multiplatform projects are completely focused on the sharing of common code, but leaves developers to build each platform application however they see fit.

This lets developers share as much, or as little, code as necessary. Building with Kotlin Multiplatform is not an all or nothing approach. If you’re targeting Android and iOS, you’ll still be building two separate native applications. However, if done properly, you can encapsulate the bulk of the logic within shared code so that building those native applications is ideally a smaller, and very similar task.

For each platform application target, the UI will be completely native. However you currently build your UI for a specific target is how you would continue to build UI with a Kotlin Multiplatform approach. This keeps native developers using the tools they are familiar and productive with. It also reduces the number of layers within your application architecture as there is no bridging or other non-native layer between the native platform apis and your code.


What Are The Current Limitations Of Kotlin Multiplatform?

Where is Kotlin Multiplatform today? What are the main limitations to building multiplatform applications with Kotlin?

Kotlin Multiplatform is still evolving rapidly. As such, it’s been difficult to keep up with how to setup and configure a project. Tutorials and samples become quickly out of date as the support and tooling changes.

Speaking of tooling, the quality of tooling support for Kotlin Multiplatform projects varies quite a bit from platform to platform. Support for JVM/Android projects is great as that is where the primary Kotlin experience is focused. Support developing for iOS or native targets is not as polished; particularly when trying to debug from your native application into the shared Kotlin code.

There is focused work being done on this front however. At KotlinConf 2019, it was announced that a new Android Studio plugin is in development to simplify the running and debugging of Kotlin code on an iOS simulator.

There are a couple other areas where support varies across platforms, but that should be addressed pretty soon.  

One, is a multiplatform support for dates. There are currently multiple third-party implementations for date/time support, but the JetBrains team is working on a first-party solution for this.  

The other area is in multi-threaded coroutine support which is present on JVM/Android but is not quite ready for native platforms yet. Within the past couple of months, Kevin Galligan from Touchlab wrote about the state of native coroutines and about the draft PR that indicates that this native coroutine support is close. Having full multiplatform support for multithreaded coroutines will be a big step forward for the Kotlin Multiplatform ecosystem and will hopefully open the doors for more third-party libraries to be created.


What Are Some Examples Of Kotlin Multiplatform?

There are several production apps out there right now that are using Kotlin Multiplatform:

At KotlinConf 2019, JetBrains announced their new team collaboration tool Space which is built from the ground up using Kotlin Multiplatform.

  • JetBrains Spaces app
  • Droidcon NYC app
  • Twitter thread with other examples

I also came across a recent Twitter thread in which quite a few developers have shared their experience in using Kotlin Multiplatform so far. As someone that’s been watching this technology for a while now, it's exciting to see how many are now experimenting with it.


Where Can You Learn More?

Are you intrigued yet? If you are, and you want to start exploring Kotlin MultiPlatform, here are a number of resources to help guide you in the right direction:


Interesting Tidbits

Here are a few interesting tidbits I’ve come across from the world of Android development:



What in the world of Android development are you interested in right now? Join in the conversation in the comments below.

See you next time devs ??

Jeremy Teichmann

AI Squad Technical Lead & Generative AI Expert@Bosch Digital

4 年

I tried to use such a approach for our current project but after evaluation we saw that it's only a nice idea and we would not really safe any time. The only thing what you can share are kotlin only code files, without any android specific library.

回复
Kevin Galligan

Technology Partner at Touchlab

4 年

"Kotlin Multiplatform is still evolving rapidly. As such, it’s been difficult to keep up with how to setup and configure a project." Yeah, temporary issue, but a big one. I know you saw it on twitter, but we posted this today to help people getting started (for native mobile, anyway):?https://github.com/touchlab/KaMPKit

回复
Arnold Villasanta

Innovator ? Solutions Provider for Embedded Systems and Smart IoT devices ? Engineer ? Precautious AI+IoT Advocate

4 年

First time seeing this... it seems it is somewhat ready for android, but for iOS it says the tooling is not there yet (unpolished, fast phase updates frequently). But nevertheless, since it's not a code-once deploy-to-all platform it is more endearing to engage with since no lockdown, plug/pull-out is no problem, and most of all, native UI code is still in there.

Aviv Zucker

Product Manager at Moon Active

4 年

Using kotlin for most of my projects, I'm excited about kotlin multiplatform but still, waiting for mature version of this ??

要查看或添加评论,请登录

社区洞察

其他会员也浏览了