• Stars
    star
    259
  • Rank 157,669 (Top 4 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 10 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Annotation Processor for generating Parcelable code

ParcelablePlease

An AnnotationProcessor for generating Android Parcelable boilerplate code. See this blog entry for comparison with other parcel libraries.

Dependency

Latest version: Maven Central

compile 'com.hannesdorfmann.parcelableplease:annotation:x.x.x'
apt 'com.hannesdorfmann.parcelableplease:processor:x.x.x'

In android studio you need to apply Hugo Visser's awesome android-apt gradle plugin to enable annotation processing.

How to use

Simply annotate the classes you want to make Parcelable with @ParcelablePlease and implement the Parcelable as well as the CREATOR (This step can be automated by using the Android Studio plugin, see below).

@ParcelablePlease
public class Model implements Parcelable {

  int id;
  String name;
  OtherModel otherModel;

  @Override public int describeContents() {
    return 0;
  }

  @Override public void writeToParcel(Parcel dest, int flags) {
    ModelParcelablePlease.writeToParcel(this, dest, flags);
  }

  public static final Creator<Model> CREATOR = new Creator<Model>() {
    public Model createFromParcel(Parcel source) {
      Model target = new Model();
      ModelParcelablePlease.readFromParcel(target, source);
      return target;
    }

    public Model[] newArray(int size) {
      return new Model[size];
    }
  };
}

The ParcelablePlease annotation processor will generate a class named ClassName + ParcelablePlease for you with all the code for writing and reading data in the Parcel. So from the example above: ModelParcelablePlease is generated and provides two static methods: ModelParcelablePlease.readFromParcel(Model, Parcel) and ModelParcelablePlease.writeToParcel(Model, Parcel, int)

Once you have done this basic setup by connecting the generated code with your Model class you can change the model class, add fields, remove fields etc. without worring about Parcelable because ParcelablePlease will generate the code for you everytime you compile.

Android Studio Plugin

Like mentioned above you have to write few lines of code to connect the Parcelable class with the generated code. Don't worry, you don't have to do this by hand. There is a Android Studio / IntelliJ plugin that can do that for you:

  1. Open Android Studio / IntelliJ
  2. Open the Preferences (on Mac with ⌘ + ; )
  3. Type in the searchbox "plugin" to navigate quickly to the plugins section
  4. Click on Browse repositories... button
  5. Search for ParcelablePlease and install this plugin
  6. Restart Android Studio
  7. Create a Model class and open the Generate Menu (on Mac with ``⌘ + n` ). Note that the cursor must be somewhere in the code of the class.

Preferences

Remember that you may have to compile your project to make Android Studio run annotation Processing which will generate the ParcelPlease classes.

Supported types

  • Primitives

    • byte
    • boolean
    • double
    • float
    • int
    • long
    • String
  • Primitive wrappers

    • Byte
    • Boolean
    • Double
    • Float
    • Int
    • Long
  • Android specific

    • Parcelable (anything that implements Parcelable)
    • Bundle
    • SparseBooleanArray
  • Arrays

    • int[]
    • long[]
    • double[]
    • String[]
    • float[]
    • char[]
    • boolean[]
    • byte[]
    • Parcelable[] - array of anything that implements Parcelable
  • Other

    • Serializable
    • java.util.Date (by simpling passing time as millis)
  • Collections

    • List<? extends Parcelalble>
    • ArrayList<? extends Parcelable>
    • LinkedList<? extends Parcelable>
    • CopyOnWriteArrayList<? extends Parcelable>
    • List

Bagger

Do you want to make a field Parcelable but it's not listed in the supported types list from above (i.e. java.util.Map)? No Problem: You can provide your own implementation implementing a ParcelBagger like this:

public class DateBagger implements ParcelBagger<Date> {

  @Override public void write(Date value, Parcel out, int flags) {
    if (value == null) {
      out.writeLong(-1);
    } else {
      out.writeLong(value.getTime());
    }
  }

  @Override public Date read(Parcel in) {

    long timeMillis = in.readLong();
    if (timeMillis == -1) {
      return null;
    }

    return new Date(timeMillis);
  }
}

You can use your ParcelBagger with the @Bagger annotation like this:

@ParcelablePlease
public class Person implements Parcelable {

  int id;
  String name;
  
  @Bagger(DateBagger.class)
  Date date;
  
}

Remember that you have to take care about special cases like what if the value is null. Note that java.util.Date is already supported by ParcelablePlease. The example above is just to give you an idea of how a implementation could look like.

Configuration

You can configure which fields should be serialized. There are two ways:

  1. As default all class (and super classes) fields will be serialized. You can mark field's you don't want to serialize by annotating them with @ParcelableNoThanks
  2. You can do the other way: You could change the settings to only serialize fields that are marked with @ParcelableThisPlease like this:
@ParcelablePlease( allFields = false)
public class Animal implements Parcelable {
   
   @ParcelableThisPlease
   String name;
   
   int age; // This will not be serialized 
   
}

As default ParcelablePlease will throw a compile error if it tries to serialize private fields (private fields are not supported because of visibility issues). If your class marked with @ParcelablePlease contains private fields you could mark them as not parcelable with @ParcelableNoThanks or you could cofigure ParcelablePlease to skip private fields by using @ParcelablePlease( ignorePrivateFields = true):

@ParcelablePlease( ignorePrivateFields = true)
public class Person implements Parcelable {
   
   String name;
   
   private int age; // No compile error 
   
}

Limitations

  • Fields must have at least default (package) visibility. That means private fields are not supported.
  • Private classes are not supported because of visibilitiy issues

More Repositories

1

mosby

A Model-View-Presenter / Model-View-Intent library for modern Android apps
Java
5,489
star
2

AdapterDelegates

"Favor composition over inheritance" for RecyclerView Adapters
Java
2,934
star
3

fragmentargs

Annotation Processor for setting arguments in android fragments
Java
1,077
star
4

SwipeBack

SwipeBack for Android Activities to do pretty the same as the android "back-button" will do, but in a really intuitive way by using a swipe gesture
Java
695
star
5

annotationprocessing101

Java
431
star
6

AnnotatedAdapter

Write less code with AnnotatedAdapter, an annotation processor for generating RecyclerView and AbsListView adapters
Java
195
star
7

sqlbrite-dao

DAO for SQLBrite
Java
182
star
8

debugoverlay

A tiny window overlay to log app internal on top of your android app
Java
151
star
9

mosby-conductor

Plugin for conductor to integrate Mosby
Java
131
star
10

CoordinatorsAndroid

Sample that shows how to apply the Coordinator Pattern on Android
Kotlin
121
star
11

Model-View-Intent-Android

A demo that shows how to apply Model-View-Intent on Android
Kotlin
86
star
12

AdapterCommands

Drop in solution to animate RecyclerView's dataset changes by using command pattern
Java
74
star
13

Instantiator

Tired of manually setup test data of Kotlin data classes or POJOs? Instantiator creates Instances of any class for you so that you can focus on writing tests instead of spending time and effort to setup test data
Kotlin
60
star
14

Vaadin-MVP-Lite

This is a Vaadin Addon that provides a Model-View-Presenter Framework
Java
51
star
15

CircleProgressView

Fork from SmoothProgressbar for internal development of another UI library ...
Java
38
star
16

mvi-timing

Just a simple demo app for blog post about MVI and Timing
Java
29
star
17

Todo-Testing-By-Design

Kotlin
13
star
18

rxworkshop

Kotlin
12
star
19

conductor-shared-element-transition

Kotlin
9
star
20

Vaadin-MVP-Lite-MailExample

This is an example project that shows how to use the VaadinMVP framework
JavaScript
9
star
21

appkit

Develop android application in a modern way. Write less code: appkit uses many popular Annotation Processing libraries like Butterknife, IcePick, Dependency Injection with Dagger and achieve a very clean software architecture with Model-View-Presenter (MVP)
Java
9
star
22

SecureBitcoinWallet

Java
8
star
23

SealedSubclassInstantiator

Instantiates instances of subclasses of a sealed class (Kotlin)
Kotlin
8
star
24

website-old

Personal blog, please read README
CSS
7
star
25

MosbyDagger

Example that shows best practices for Dagger2 + Mosby
4
star
26

AndroidCollections

Some usefu collection implementation like List, Map, Set (and combinations of all) etc. that can be useful in Android Projects
Java
4
star
27

OkHttpCertificate

Kotlin
3
star
28

hdlib

Java
2
star
29

fragmentargs-samples

Java
2
star
30

AndroidDesignPatterns

A Collection of Software Design Patterns for Android Applications
Java
2
star
31

ColorProgressBar

A circular (loading spinner) ProgressBar for Android that let you customize the colors of the loading spinner
Java
2
star
32

MosbyViewPagerDemo

Demo of viewpager + mosby
Java
1
star
33

RobolectircTest

Java
1
star
34

DexUtil

How many mehtods are used in your androids .dex file?
Java
1
star
35

annotationprocessing

Some utilities for writing annotation processors
Java
1
star
36

AndroidCollectionsTest

The eclipcse Android Test Project to run unit tests from eclipse
1
star
37

ason

Java
1
star
38

Vaadin-LoadingPanel

A simple LoadingPanel component, where you can switch beetween showing a loading animation and the "normal" content
Java
1
star
39

HippoHappa

Java
1
star
40

stdlib-android-test

Units test for the stdlib-android library
Java
1
star