Using Android Studio and the Kotlin plugin, I found an easy way to convert my old Java Parcelable
s with no extra plugins (if all you want is to turn a brand new data
class into a Parcelable
, skip to the 4th code snippet).
Let's say you have a Person
class with all the Parcelable
boiler plate:
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 stripping out the Parcelable
implementation, leaving a bare-bones, plain, old Java object (properties should 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 into a data
class:
data class Person(val firstName: String, val lastName: String, val age: Int)
And finally, let's turn this into a Parcelable
again. Hover the class name and Android Studio should give you the option to Add Parcelable Implementation
. 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 auto-generated code appended to you data
class definition.
Notes:
- Trying to convert a Java
Parcelable
directly into 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 the current
Parcelable
code generator introduces. Must be a minor bug.
I hope this tip works for you as well as it did for me.