0

I'm trying to migrate from kotlinx.android.parcel.Parcelize to kotlinx.parcelize.Parcelize but I'm getting following issue while doing so:

Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath: class com.myapp.models.Text.Raw.Companion, unresolved supertypes: kotlinx.parcelize.Parceler

This is the source:

import android.content.Context
import android.os.Parcel
import android.os.Parcelable
import kotlinx.android.parcel.Parceler
import kotlinx.android.parcel.Parcelize

sealed class Text : Parcelable {

    @Parcelize
    data class Raw(private val text: CharSequence) : Text() {

        override fun resolveToCharSequence(context: Context): CharSequence = text

        companion object : Parceler<Raw> {

            override fun create(parcel: Parcel): Raw {
                return Raw(checkNotNull(parcel.readString()))
            }

            override fun Raw.write(parcel: Parcel, flags: Int) {
                parcel.writeString(text.toString())
            }
        }
    }
    ... 
}

After changes:

import android.content.Context
import android.os.Parcel
import android.os.Parcelable
import kotlinx.parcelize.Parceler
import kotlinx.parcelize.Parcelize

sealed class Text : Parcelable {

    @Parcelize
    data class Raw(private val text: CharSequence) : Text() {

        override fun resolveToCharSequence(context: Context): CharSequence = text

        companion object : Parceler<Raw> {

            override fun create(parcel: Parcel): Raw {
                return Raw(checkNotNull(parcel.readString()))
            }

            override fun Raw.write(parcel: Parcel, flags: Int) {
                parcel.writeString(text.toString())
            }
        }
    }
    ... 
}

build.gradle.kts

plugins {    
    com.android.library
    `kotlin-android`
    `kotlin-parcelize`
    `kotlin-kapt`
}
...

If I remove the companion object part there is no error.

And idea what the problem could be?

David
  • 3,971
  • 1
  • 26
  • 65

1 Answers1

0

If I remove the companion object part there is no error.

And idea what the problem could be?

My guess would be the companion object part :)

The whole point of Parcelize is that you don't need to manually code the parceling logic. You're not doing anything special in the companion objects - just delete them.

dominicoder
  • 9,338
  • 1
  • 26
  • 32