Skip to content
Roger Hu edited this page Jan 28, 2016 · 15 revisions

Overview

Although creating Android Parcelables is usually at least 10x faster than using Serializable, creating Parcelable objects requires creating a lot of boilerplate code in defining exactly the stream of data that should be serialized and deserialized. While there are IDE plugins to help facilitate the creating of these objects, another option is to leverage a third-party library called Parceler that will help automate this work. Underneath the surface this library generates the necessary wrapper classes for you at compile time automatically, saving you the repetitive steps required for leveraging the performance benefits of Parcelables.

Setup

To setup, we need to add the android-apt plugin to our classpath in our root build.gradle file:

dependencies {
  classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}

Inside the app/build.gradle file, we should apply the plugin before the Parceler dependencies are added. This way, the apt keyword can be used, which is primarily used for annotation processing and keeps the libraries from being added to the classpath.

apply plugin: 'com.neenbedankt.android-apt'

dependencies {
  compile 'org.parceler:parceler-api:1.0.4'
  apt 'org.parceler:parceler:1.0.4'
}

Converting a model from Serializable objects

Suppose we have an User object that implements the Serializable interface:

public class User implements Serializable {
    private String firstName;
    private String lastName;

    public User(String firstName, String lastName) {
       this.firstName = firstName;
       this.lastName = lastName;
    }
}

There are several requirements to convert this object to one that can be used by this library:

  1. Remove the Serializable interface back to its original form.
  2. Annotate the class with the @Parcel decorator.
  3. Use only public fields (private fields cannot be detected during annotation)
  4. Create a public constructor with no arguments for the annotation library too.
@Parcel
public class User implements Serializable {
    // fields must be public
    String firstName;
    String lastName;

    // empty constructor needed by the Parceler library 
    public User() {
    }

    public User(String firstName, String lastName) {
       this.firstName = firstName;
       this.lastName = lastName;
    }
}

Next, simply wrap your objects with Parcel.wrap():

User user = new User("John", "Doe");
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("user", Parcels.wrap(user));
startActivity(intent);

On the receiving side, we simply need to unwrap the object:

User user = (User) Parcels.unwrap(getIntent().getParcelableExtra("user"));

The Parceler library works by using the @Parcel annotation to generate the wrapper classes for you. It works with many of the most standard Java types, including the ones defined here.

References

Finding these guides helpful?

We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.

Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.

Interested in ramping up on Android quickly?

(US Only) If you are an existing engineer with 2+ years of professional experience in software development and are serious about ramping up on Android quickly, be sure to apply for our free evening 8-week Android bootcamp.

We've trained over a thousand engineers from top companies including Apple, Twitter, Airbnb, Uber, and many others leveraging this program. The course is taught by Android experts from the industry and is specifically designed for existing engineers.

Not in the United States? Please fill out our application of interest form and we’ll notify you as classes become available in your area powered by local organizers.

Clone this wiki locally