Blog Infos
Author
Published
Topics
,
Published

Kotlin Multiplatform despite all of its benefits sometimes has its own challenges. One of these challenges we recently faced was creating Parcelable objects in the shared module for the Android platform. After a little searching, I found this suggestion from

 here. And in this tutorial, I want to show you how to address this challenge, in the way he’s said.

This tutorial consists of two parts:

  1. Getting familiar with type alias and their usage in Kotlin multiplatform projects.
1. Typealias

As the Kotlin documentation says:

Type aliases provide alternative names for existing types. If the type name is too long you can introduce a different shorter name and use the new one instead.

The concept is very simple and just for clarity, I show you an example.
Assume that you are using a prebuilt class that has a long and inappropriate name, like this:

class TooLongAndUglyNameClass

By using type alias you can change the name of the class in your project and use it in your code. It means by using your preferred class name you can build a new instance of the class.

typealias MyClass = TooLongAndUglyNameClass
fun foo(p: MyClass) {
// Some logics
}
view raw Example.kt hosted with ❤ by GitHub
Typealias in Kotlin multiplatform

In Kotlin multiplatform projects, by declaring a class with except keyword in the commonMain directory, if, in one of the platforms, already there is a class that has all the methods with the exact signature of the expected class, by using type alias in that platform we can map the name of the class to the expected name.

Assume we have an expected interface in the commonMain directory like this:

// commonMain/Platform.kt
expect interface CommonParcelable
view raw Platform.kt hosted with ❤ by GitHub

Then on the Android platform, we want to map the Android Parcelable interface to it.
So we achieve this by using type alias for actual interface in the androidMain, like this:

// androidMain/Platform.kt
actual typealias CommonParcelable = Parcelable
view raw Platform.kt hosted with ❤ by GitHub

In this way, by implementing the CommonParcelable interface, on the Android platform, the Android Parcelable will be used.

2. Creating Parcelable classes in kotlin multiplatform

The first step is adding the kotlin-parcelize plugin to the shared module build.gralde file, till being able to use Parcelize annotation:

// shared module
plugins {
...
id("kotlin-parcelize")
}

As you know in regular Android projects if we want to make a class Parcelable, we should add the Parcelize annotation to it and implement the Parcelable interface. So in the commonMain directory, we must create an annotation for the Parcelize and an interface for the Parcelabe.

In the commonMain directory create an annotation and name it CommonParceLize:

// commonMain/Platform.kt
@OptIn(ExperimentalMultiplatform::class)
@OptionalExpectation
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.BINARY)
expect annotation class CommonParcelize()
view raw Platform.kt hosted with ❤ by GitHub

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

, ,

Migrating to Jetpack Compose – an interop love story

Most of you are familiar with Jetpack Compose and its benefits. If you’re able to start anew and create a Compose-only app, you’re on the right track. But this talk might not be for you…
Watch Video

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer for Jetpack Compose
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engin ...
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer f ...
Google

Jobs

This annotation is like the Parcelize annotation, having all its annotations with two more annotations which are:

  1. @OptIn(ExperimentalMultiplatform::class): because we are using OptionalExpecation which is currently experimental.

Then in the commonAndroid directory, we create the actual CommonParcelize and map the Parcelize annotation to it using type alias:

// androidMain/Platform.kt
actual typealias CommonParcelize = Parcelize
view raw Platform.kt hosted with ❤ by GitHub

Note: Because we’ve added OptionalExpectation annotation, there is no need to create corresponding actual classes in other platforms.

For the Parcelable we act like the Parcelize, so first in the commonMain, we create an expected interface and name it CommonParcelable:

// commonMain/Platform.kt
expect interface CommonParcelable
view raw Platform.kt hosted with ❤ by GitHub

And in the androidMain:

// androidMain/Pltafrom.kt
actual typealias CommonParcelable = Parcelable
view raw Platform.kt hosted with ❤ by GitHub

Also for other platforms

// ios/js/...Main
actual interface CommonParcelable
view raw Platform.kt hosted with ❤ by GitHub

Everything is done and now all we need is to use our annotation and interface

// CommonMain
@CommonParcelize
data class User(
val id: Int,
val firstname: String
): CommonParcelable
view raw Platform.kt hosted with ❤ by GitHub

In this way, on the Android platform, Parcelize instead of CommonParcelize and Parcelable instead of CommonParcelable will be used. And in other platforms, the CommonParcelize will be ignored because we’ve not declared the actual corresponding classes for them, and the CommonParcelable itself will be used which is an interface with no methods.

You can find the source code here:

I hope you found this tutorial helpful and enjoyed it, thanks for the devoted time.

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
I love Swift enums, even though I am a Kotlin developer. And iOS devs…
READ MORE
blog
After successfully implementing the basic Kotlin multiplatform app in our last blog, we will…
READ MORE
blog
I develop a small multi-platform(android, desktop) project for finding cars at an auction using…
READ MORE
blog
Kotlin Multiplatform Mobile (or simply Multiplatform Mobile) is a new SDK from JetBrains that…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu