Using Android Studio and the Kotlin plugin, I found an easy way to convert my old Java Parcelable with no additional plugins (if all you want to do is turn the new data class into Parcelable , skip to the fourth code snippet).
Say you have a Person class with an entire Parcelable boiler Parcelable :
public class Person implements Parcelable{ public static final Creator<Person> CREATOR = new Creator<Person>() { @Override public Person createFromParcel(Parcel in) { return new Person(in); } @Override public Person[] newArray(int size) { return new Person[size]; } }; private final String firstName; private final String lastName; private final int age; public Person(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } protected Person(Parcel in) { firstName = in.readString(); lastName = in.readString(); age = in.readInt(); } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(firstName); dest.writeString(lastName); dest.writeInt(age); } @Override public int describeContents() { return 0; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public int getAge() { return age; } }
Start by removing the Parcelable implementation, leaving the old old Java object empty (the properties must be final and set by the constructor):
public class Person { private final String firstName; private final String lastName; private final int age; public Person(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public int getAge() { return age; } }
Then let the Code > Convert Java file to Kotlin File option do its magic:
class Person(val firstName: String, val lastName: String, val age: Int)
Convert this to the data class:
data class Person(val firstName: String, val lastName: String, val age: Int)
And finally, enable it again in Parcelable . Place your class name and Android Studio will offer you the Add Parcelable Implementation option. The result should look like this:
data class Person(val firstName: String, val lastName: String, val age: Int) : Parcelable { constructor(parcel: Parcel) : this( parcel.readString(), parcel.readString(), parcel.readInt() ) override fun writeToParcel(parcel: Parcel, flags: Int) { parcel.writeString(firstName) parcel.writeString(lastName) parcel.writeInt(age) } override fun describeContents(): Int { return 0 } companion object CREATOR : Parcelable.Creator<Person> { override fun createFromParcel(parcel: Parcel): Person { return Person(parcel) } override fun newArray(size: Int): Array<Person?> { return arrayOfNulls(size) } } }
As you can see, the Parcelable implementation is some automatically generated code added to you data class definition.
Notes:
- Attempting to convert Java
Parcelable directly to Kotlin will not produce the same result with the current version of the Kotlin plugin ( 1.1.3 ). - I had to remove some extra curly braces that the current
Parcelable code generator Parcelable . There should be a little mistake.
I hope this tip will work for you as well as for me.