ParcelerでParcelableをサクッと実装する

## Parceler
アクティビティ自分で作ったクラスのオブジェクトを渡す時にParcelableを実装するんですが面倒くさい。Parcelerというライブラリを使えばアノテーションでサクッと実装できるということを知ったので手順を残しておく。

https://github.com/johncarl81/parceler

```xml:build.gradle(project)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'

        //追加
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
       
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}


```

```xml:build.gradle(Module:app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"
    defaultConfig {
        applicationId "jp.co.demo.myapplication"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

// 追加
apply plugin: 'com.neenbedankt.android-apt'

// 追加
dependencies {
    compile "org.parceler:parceler-api:1.0.3"
    apt "org.parceler:parceler:1.0.3"
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.1.1'
    testCompile 'junit:junit:4.12'
}


```

MainアクティビティからSubアクティビティへ渡すオブジェクトのクラス

```java:Person.java

import org.parceler.Parcel;

@Parcel
public class Person {
    String name;
    int age;

    public Person() {}


    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }

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

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

```

```java:MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        createBtn();

    }
    private void createBtn() {
        Button btn = new Button(this);
        btn.setText("移動");
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Person person = new Person(18,"山田太郎");
                Bundle bundle = new Bundle();
                bundle.putParcelable("person", Parcels.wrap(person));
                Intent intent = new Intent(getApplication(), SubActivity.class);
                intent.putExtras(bundle);  // 内部的に Bundle.putAll() が呼ばれる
                startActivity(intent);
            }
        });
        LinearLayout layout = new LinearLayout(this);
        layout.addView(btn, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        setContentView(layout);
    }
}

```


```java:SubActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import org.parceler.Parcels;

public class SubActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub);
        Person person = Parcels.unwrap(getIntent().getParcelableExtra("person"));
        String message = person.getName() + "さん、" + person.getAge() + "歳です";
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}


```