This tutorial describes the usage of the AutoValue library for creating value objects. It also overs AutoValue extensions, e.g, to convert to Parcelable.
1. Using AutoValue and its extensions
Auto is collection of code generators that automate certain repetitive tasks, like creating value objects. The tools create the code you would have written, but without the bugs.
1.1. AutoValue
AutoValue allows the creation of immutable value objects. Value objects are classes which treat any two instances with suitably equal field values as interchangeable.
1.2. Using AutoValue on Android
Auto-Value annotations allows to use AutoValue on Android.
Add the following dependencies to your build.gradle
file.
dependencies {
annotationProcessor 'com.google.auto.value:auto-value:1.4'
provided 'com.jakewharton.auto.value:auto-value-annotations:1.4'
}
1.3. How to use AutoValue
In AutoValue you write an abstract class, and AutoValue implements it.
You can provide a create
method which allows you to return the generated instance of the class.
package com.vogella.android.autovalue;
import com.google.auto.value.AutoValue;
@AutoValue public abstract class Task {
public abstract long id();
public abstract String summary();
public abstract String description();
public static Task create(long id, String summary, String description) {
return new AutoValue_Task(id, summary, description);
}
}
1.4. Using Builder
Autovalue allows allows to create a Builder
API.
package com.vogella.android.butterknifeexample;
import com.google.auto.value.AutoValue;
@AutoValue public abstract class Task {
public abstract long id();
public abstract String summary();
public abstract String description();
static Builder builder() {
return new AutoValue_Task.Builder();
}
@AutoValue.Builder
abstract static class Builder {
abstract Builder setId(long value);
abstract Builder setSummary(String value);
abstract Builder setDescription(String value);
abstract Task build();
}
}
1.4.1. AutoValue extensions
The AutoValue extensions allows to support things like:
-
Parcelable
-
Json
-
Moschi
-
Gson
See Auto Value libraries.
For example, to make an object Parcelable you simple add the following dependency to your build file.
annotationProcessor 'com.ryanharter.auto.value:auto-value-parcel:0.2.5'
2. Exercise: Use auto-value-parcel to create a Parcelable for Android
In this exercise you learn how to use the auto-value library and its extensions to implement Parcelable
.
Create or continue to use an Android project with the com.vogella.android.usinglibs top-level package.
2.1. Create second activity
Create a second activity and allow to start it from the first activity via a button click.
2.2. Using Auto-Value
Add the following dependencies to your app/build.gradle
file.
dependencies {
annotationProcessor 'com.google.auto.value:auto-value:1.4'
annotationProcessor 'com.ryanharter.auto.value:auto-value-parcel:0.2.5'
provided 'com.jakewharton.auto.value:auto-value-annotations:1.4'
}
Create a value object using auto-value.
package com.vogella.android.usinglibs;
import android.os.Parcelable;
import com.google.auto.value.AutoValue;
@AutoValue public abstract class Task implements Parcelable{
public abstract long id();
public abstract String summary();
public abstract String description();
public static Task create(long id, String summary, String description) {
return new AutoValue_Task(id, summary, description);
}
}
Pass the Task
object to the second activity, as intent parameter.
Intent intent = new Intent(this, SecondActivity.class);
Task task = Task.create(1,"hello","Testing");
intent.putExtra("task", task);
startActivity(intent);
In the second activity, display the Task
you received via the Intent
.
2.3. Optional: Switch to a builder API
Change your code to generate a builder API for your Task.
Define a static abstract class called builder and define a builder API with the @AutoValue.Builder
annotation.
Change your code which passed the intent to the second activity.
Intent intent = new Intent(this, SecondActivity.class);
Task task = Task.builder().setId(1).setSummary("hello").setDescription("Testing").build();
intent.putExtra("task", task);
startActivity(intent);
The following shows a possible solution.
package com.vogella.android.usinglibs;
import android.os.Parcelable;
import com.google.auto.value.AutoValue;
@AutoValue public abstract class Task implements Parcelable{
public abstract long id();
public abstract String summary();
public abstract String description();
static Builder builder() {
return new AutoValue_Task.Builder();
}
@AutoValue.Builder
abstract static class Builder {
abstract Builder setId(long value);
abstract Builder setSummary(String value);
abstract Builder setDescription(String value);
abstract Task build();
}
}
3. AutoValue resources
If you need more assistance we offer Online Training and Onsite training as well as consulting