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


    Sr. Software Development Engineer, Last Mile Driver Assistance Technology

    Amazon
    Berlin
    • Full Time
    apply now

    Talent Acquisition Manager – Technology

    FanDuel
    New York, NY; Atlanta, GA
    • Full Time
    apply now

    Android Engineer

    American Express
    London | New York | Phoenix
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

, ,

The Evolution of Android Graphics in Android 12/13

Android 12 and 13 both added significant new capabilities to Android platform graphics, including RenderEffect, RuntimeShader, and more. At the same time, RenderScript has been deprecated and we’ve introduced the RenderScript Intrinsics Replacement Toolkit. This…
Watch Video

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Advocate and Fast Talking YouTuber
Google

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Ad ...
Google

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Advocat ...
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 develop a small multi-platform(android, desktop) project for finding cars at an auction using…
READ MORE
blog
One of my 2021 new year’s resolutions was to dive in into Kotlin Multiplatform Mobile (KMM).…
READ MORE
blog
We will review a framework for building KMM apps with the objective of not…
READ MORE

Leave a Reply

Your email address will not be published.

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

Menu