Search code examples
javakotlinkotlin-multiplatformkotlin-multiplatform-mobile

How to convert Java object to a Kotlin Multi Platform object


I have a Java object. And I have a Kotlin Multi Platform object. How do I convert this Java object to the Kotlin Multi Platform object?

Java Code:

public class JavaRecentItemList extends ObservableBean implements Parcelable {

    @SerializedName("TotalRowCount")
    int totalRowCount;
}

Kotlin Multi Platform Shared code:

import kotlinx.serialization.Serializable

@Serializable
data class KMMRecentItemList(
    val TotalRowCount: Int? = null
)

I have a KMM library that needs a KMMRecentItemList. i.e:

suspend fun foo(itemList: KMMRecentItemList) {
}

Solution

  • You just copy it the straightforward way. Nothing is special about these objects from Kotlin's perspective.

     val converted = KMMREcentItemList(javaRecentItemList.totalRowCount)