问题
Is there a way to use Parceler with Kotlin data classes and constructor for serialization without using @ParcelProperty
annotation for each field?
If I try and use library like this:
@Parcel
data class Valve @ParcelConstructor constructor(val size: Int)
I get Error:Parceler: No corresponding property found for constructor parameter arg0
. But if I add @ParcelProperty("size")
it works just fine.
Why is that?
Update:
There are other another way to use this library.
I could just remove @ParcelConstructor
annotation, but then I will get errorError:Parceler: No @ParcelConstructor annotated constructor and no default empty bean constructor found.
I think (haven't tested it) I also could make all constructor parameters optional and add @JvmOverloads
but that has a side effect that I have to check all properties of the class if they are null or not.
Update 2:
This is what worked for me:
@Parcel
data class Valve(val size: Int? = null)
In short generated Java class must have default empty constructor. One way to achieve that is to do as above - all variables should have default values.
回答1:
According to the docs, Parceler by default works with public fields. But a usual Kotlin data class
(as in your example) is rather a "traditional getter/setter bean", since every Kotlin property is represented by a private field and a getter/[setter].
TL; DR: I think this will work:
@Parcel(Serialization.BEAN)
data class Valve(val size: Int = 10)
Note the default value, it allows Kotlin to automatically generate an additional empty constructor, which is required by the Java Been specification.
Another way would be to mark the constructor that we already have:
@Parcel(Serialization.BEAN)
data class Driver @ParcelConstructor constructor(val name: String)
The specific document: https://github.com/johncarl81/parceler#gettersetter-serialization
回答2:
I know this question already has an answer, but for future viewers who are also struggling to get Parceler to work with kotlin data objects, I wrote a new annotation processor to generate the Parcelable boilerplate for Kotlin data classes. It's designed to massively reduce the boilerplate code in making your data classes Parcelable:
https://github.com/grandstaish/paperparcel
Usage:
Annotate your data class with @PaperParcel
, implement PaperParcelable
, and add a JVM static instance of the generated CREATOR
e.g.:
@PaperParcel
data class Example(
val test: Int,
...
) : PaperParcelable {
companion object {
@JvmField val CREATOR = PaperParcelExample.CREATOR
}
}
Now your data class is Parcelable
and can be passed directly to a Bundle
or Intent
Edit: Update with latest API
回答3:
Just add the default constructor:
@Parcel
data class Valve(val size: Int) {
constructor() : this(0)
}
回答4:
if you use Kotlin 1.1.4 or above it's easier to use @Parcelize annotation
For doing this first add this to build.gradle
android {
//other codes
//for using latest experimental build of Android Extensions
androidExtensions {
experimental = true
}
}
Then change your class like this
@Parcelize
data class Valve(val size: Int? = null) : Parcelable
来源:https://stackoverflow.com/questions/33891814/use-of-parceler-with-kotlin-data-class-with-constructor-for-serialization