Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NotSerializableException when serializing PurchasesException #2006

Open
5 tasks done
MMP0 opened this issue Dec 22, 2024 · 1 comment
Open
5 tasks done

NotSerializableException when serializing PurchasesException #2006

MMP0 opened this issue Dec 22, 2024 · 1 comment

Comments

@MMP0
Copy link

MMP0 commented Dec 22, 2024

Describe the bug

PurchasesException inherits from Exception which inherits from Throwable which implements Serializable, so it must be “serializable” but it’s not.
When you try to serialize a PurchasesException, a NotSerializableException is thrown because the underlying PurchasesError doesn’t implement Serializable.

In the case of my app (which keeps some Exceptions for error dialog), it crashes when onSaveInstanceState is called.

  1. Environment

    1. Platform: Android
    2. SDK version: 8.10.6
    3. OS version: 15
    4. Android Studio version:
    5. How widespread is the issue. Percentage of devices affected.
  2. Debug logs that reproduce the issue

  3. Steps to reproduce, with a description of expected vs. actual behavior

    1. Try to serialize PurchasesException.

    Expected: PurchasesException is serialized.
    Actual: NotSerializableException is thrown.

  4. Other information (e.g. stacktraces, related issues, suggestions how to fix, links for us to have context, eg. stackoverflow, etc.)
    Sample code:

    var exception = PurchasesException(PurchasesError(PurchasesErrorCode.UnknownError, "Serialize test."))
    
    @Suppress("USELESS_IS_CHECK")
    println(exception is Serializable)  // <- true of course
    
    var bundle = Bundle().apply {
         putSerializable("exception", exception)
    }
    
    val parcel = Parcel.obtain()
    try {
         // Write
         parcel.writeParcelable(bundle, 0)  // <- CRASH!
    
         // Read
         parcel.setDataPosition(0)
         bundle = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
             parcel.readParcelable(javaClass.classLoader, Bundle::class.java)
         } else {
             @Suppress("DEPRECATION")
             parcel.readParcelable(javaClass.classLoader)
         }!!
    } finally {
         parcel.recycle()
    }
    
    exception = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
         bundle.getSerializable("exception", PurchasesException::class.java)
    } else {
         @Suppress("DEPRECATION")
         bundle.getSerializable("exception") as PurchasesException?
    }!!
    println(exception)
    println(exception.underlyingErrorMessage)
    android.os.BadParcelableException: Parcelable encountered IOException writing serializable object (name = com.revenuecat.purchases.PurchasesException)
         at android.os.Parcel.writeSerializable(Parcel.java:2966)
         at android.os.Parcel.writeValue(Parcel.java:2732)
         at android.os.Parcel.writeValue(Parcel.java:2531)
         at android.os.Parcel.writeArrayMapInternal(Parcel.java:1395)
         at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1843)
         at android.os.Bundle.writeToParcel(Bundle.java:1483)
         at android.os.Parcel.writeParcelable(Parcel.java:2753)
         ...
    Caused by: java.io.NotSerializableException: com.revenuecat.purchases.PurchasesError
         at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1240)
         at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1620)
         at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1581)
         at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1490)
         at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1234)
         at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:354)
         at android.os.Parcel.writeSerializable(Parcel.java:2961)
         ...
    

    Here are some suggestions:

    Make PurchasesError a Serializable

    Simply implement Serializable.

    purchases/src/main/kotlin/com/revenuecat/purchases/errors.kt:

    import android.os.Parcelable
    import kotlinx.parcelize.IgnoredOnParcel
    import kotlinx.parcelize.Parcelize
    + import java.io.Serializable
    
    ...
    
    @Parcelize
    class PurchasesError(
        val code: PurchasesErrorCode,
        val underlyingErrorMessage: String? = null,
    - ) : Parcelable {
    + ) : Parcelable, Serializable {
        ...
    }

    or

    Override the writeObject / readObject methods of PurchasesException

    When serializing, convert PurchasesError into a byte array with Parcel and write it to the stream. And do the reverse when deserializing.

    purchases/src/main/kotlin/com/revenuecat/purchases/PurchasesException.kt:

    import android.os.Build
    import android.os.Parcel
    import com.revenuecat.purchases.PurchasesError
    import com.revenuecat.purchases.PurchasesErrorCode
    import java.io.IOException
    import java.io.ObjectInputStream
    import java.io.ObjectOutputStream
    
    open class PurchasesException(error: PurchasesError) : Exception() {
         @Transient
         var error: PurchasesError = error
              private set
    
         val code: PurchasesErrorCode
              get() = error.code
    
         val underlyingErrorMessage: String?
              get() = error.underlyingErrorMessage
    
         override val message: String
              get() = error.message
    
         @Throws(IOException::class)
         private fun writeObject(out: ObjectOutputStream) {
              out.defaultWriteObject()
    
              val parcel = Parcel.obtain()
              try {
                   parcel.writeParcelable(error, 0)
                   out.writeObject(parcel.marshall())
              } finally {
                   parcel.recycle()
              }
         }
    
         @Throws(IOException::class, ClassNotFoundException::class)
         private fun readObject(`in`: ObjectInputStream) {
              `in`.defaultReadObject()
    
              val data = `in`.readObject() as ByteArray
    
              val parcel = Parcel.obtain()
              try {
                   parcel.unmarshall(data, 0, data.size)
                   parcel.setDataPosition(0)
                   error = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                        parcel.readParcelable(javaClass.classLoader, PurchasesError::class.java)
                   } else {
                        @Suppress("DEPRECATION")
                        parcel.readParcelable(javaClass.classLoader)
                   }!!
              } finally {
                   parcel.recycle()
              }
         }
    }

    Add the following configuration to Proguard:
    https://www.guardsquare.com/manual/configuration/examples#serializable

    -keepclassmembers class * implements java.io.Serializable {
         private static final java.io.ObjectStreamField[] serialPersistentFields;
         private void writeObject(java.io.ObjectOutputStream);
         private void readObject(java.io.ObjectInputStream);
         java.lang.Object writeReplace();
         java.lang.Object readResolve();
    }

Additional context

Thanks.

@RCGitBot
Copy link
Contributor

👀 We've just linked this issue to our internal tracker and notified the team. Thank you for reporting, we're checking this out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants