Kotlin Multiplatform fundamentals with Implementation
Kotlin Multiplatform
Support for multiplatform programming is one of Kotlin’s key benefits. It reduces time spent writing and maintaining the same code for different platforms while retaining the flexibility and benefits of native programming.
Sharing code between mobile platforms is one of the major Kotlin Multiplatform use cases. With Kotlin Multiplatform Mobile (KMM), you can build multiplatform mobile applications sharing code, such as business logic, connectivity, and more, between Android and iOS.
Three flavors of Kotlin
- JVM : Android, Server, Desktop
- JS : Web, Cloud
- Native : iOS, Desktop, WASM(Web Assembly)
Kotlin 1.3 introduces a new plug-in thats called Kotlin multiplatform.
If we apply to our any of the modules, we can select from set of presets to target any of the platforms.
plugin {
id 'com.android.library'
id 'org.jetbrains.kotlin.multiplatform'
}
// here i am target the android library and js target
kotlin{
targets {
fromPreset(presets.android, 'android' )
fromPreset(presets.js, 'js')
}
}
When we add this plugin to our module it automatically creates a source sets for each of these platforms specific Kotlin files. So if you put your Kotlin files in the js main folder, they will get compiled or transpiled to JavaScript files.
What is platform specific kotlin ?
If we browse the reference pages for Kotlin packages, in the top right the chips will tell us which compilation target this library is available on.
Many of the core libraries and functions are available across all compilation targets.
Kotlin Common
It's a library that can run independent of any platform that's it's targeting.
In fact if you add multiplatform plugin to your project, along with the platform specific source sets. We also get the common source set where you can put platform independent code.
Platform-independent code is it cannot call any of the platform APIs.
It cannot call any of the js specific or Android specific APIs.
Platform-specific code : for any of the flavors depends on shared common library or source set.
Kotlin multiplatform
It's not a toolkit that lets you write an app once and run anywhere
We still need to create an android app with android specific code, lifecycle, ui, ecc..
But what is done, is all shared business logic : Ex. Sudoku engine that builds and generated the board
so we take it out and put it in a shared library using Kotlin common.
The only source set I have is the common main. So i put all my code there. And that means it is available across all the platforms that i choose to target.
Drawing on the screen that is completely platform independent like a multiplatform canvas?
Actually i want to have a delegate to each of the platform implementations.
Eg. WE can use AndroidCanvas to draw on Android , HTML5 Canvas to draw on the website.
Kotlin common code cannot call any platform interfaces. So i can't really depend on it and export them from this module.
So how does it work in Kotlin ?
There is this expect and actual mechanism that lets you declare expected classes in your common code.
Which is something like defining an interface in java.
And then in each of my platform specific source sets, I provide the actual implementation that can depend and use platform APIs such as Android canvas.
Now when i add that dependency from my common source set to other one it looks something like this.
But actually, when compiling for a specific platform , like js, this dependency will use correct HTML5 canvas.
Setup
Update the Kotlin plugin to version 1.4.20 or higher.
In Android Studio, select Tools | Kotlin | Configure Kotlin Plugin Updates and update to the latest version in the Stable update channel.
Install the Kotlin Multiplatform Mobile plugin.
In Android Studio, select Preferences | Plugins, search for the plugin Kotlin Multiplatform Mobile in Marketplace and install it.
In Android Studio, select Preferences | Plugins, search for the plugin Kotlin Multiplatform Mobile in Marketplace and install it.
Further Details here:
https://kotlinlang.org/docs/mobile/create-first-app.html
Comments
Post a Comment