Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Parceler with android

I'm trying Parceler library for android. So far I've had only one same error, using plain sample from the documentation.

@Parcel(Parcel.Serialization.BEAN)
public class Example {
  private String name;
  private int age;

  public String getName() { return name; }
  public void setName(String name) { this.name = name; }

  public int getAge() { return age; }
  public void setAge(int age) { this.age = age; }
}

And

Parcelable p = Parcels.wrap(new Example());

Which crashes with

07-30 12:31:46.439: E/AndroidRuntime(4945): FATAL EXCEPTION: main
07-30 12:31:46.439: E/AndroidRuntime(4945): Process: com.sample.app.android.debug, PID: 4945
07-30 12:31:46.439: E/AndroidRuntime(4945):  org.parceler.ParcelerRuntimeException: Unable to find generated Parcelable  class for com.sample.app.android.entity.Example, verify that  your class is configured properly and that the Parcelable class  com.sample.app.android.entity.Example$$Parcelable is generated  by Parceler.
07-30 12:31:46.439: E/AndroidRuntime(4945):     at  org.parceler.Parcels$ParcelCodeRepository.get(Parcels.java:201)
07-30 12:31:46.439: E/AndroidRuntime(4945):     at org.parceler.Parcels.wrap(Parcels.java:85)
07-30 12:31:46.439: E/AndroidRuntime(4945):     at org.parceler.Parcels.wrap(Parcels.java:69)

What do I miss?

like image 409
Roman Avatar asked Jul 30 '15 14:07

Roman


People also ask

Why Parcelable is used in Android?

Parcelable and Bundle objects are intended to be used across process boundaries such as with IPC/Binder transactions, between activities with intents, and to store transient state across configuration changes.

What is Parcelable creator Android?

Parcelable.ClassLoaderCreator<T> Specialization of Creator that allows you to receive the ClassLoader the object is being created in. Interface that must be implemented and provided as a public CREATOR field that generates instances of your Parcelable class from a Parcel.

What is serialization and Parcelable in Android?

Serializable is a standard Java interface. You simply mark a class Serializable by implementing the interface, and Java will automatically serialize it in certain situations. Parcelable is an Android specific interface where you implement the serialization yourself.


2 Answers

It sounds like the annotation processor isn't running or is misconfigured. I'd recommend using the annotationProcessor scope if you're using a Gradle build along with separate scopes for the processor and api libraries:

compile 'org.parceler:parceler-api:1.1.9'
annotationProcessor 'org.parceler:parceler:1.1.9'

See the Getting Parceler section of the website for the latest.

like image 54
John Ericksen Avatar answered Oct 11 '22 14:10

John Ericksen


In the newer version, you only have to update the following dependencies in your Gradle file:

compile 'org.parceler:parceler-api:1.1.6'
annotationProcessor 'org.parceler:parceler:1.1.6' 
like image 24
Harsh Mehta Avatar answered Oct 11 '22 14:10

Harsh Mehta