Home Tutorials Training Consulting Books Company Contact us


Get more...

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](https://github.com/google/auto/) is a collection of code generators that automate certain repetitive tasks, such as creating value objects. These tools generate the code you would have written, minimizing the risk of bugs.

1.1. AutoValue

AutoValue enables the creation of immutable value objects. Value objects are classes that treat any two instances with equivalent field values as interchangeable.

1.2. Using AutoValue on Android

The [Auto-Value annotations](https://github.com/JakeWharton/AutoValueAnnotations) allow you to use AutoValue on Android. To get started, 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

With AutoValue, you write an abstract class, and AutoValue implements it for you. You can provide a create method that returns an instance of the generated 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 the Builder Pattern

AutoValue also allows you to create a Builder API for more flexible object creation.

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 support various features, including:

  • Parcelable

  • JSON

  • Moshi

  • Gson

For a complete list of AutoValue libraries, visit [Maven Central](http://search.maven.org/#search%7Cga%7C1%7Cauto-value).

For example, to make an object Parcelable, simply 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 will learn how to use the AutoValue library and its extensions to implement Parcelable. You can create a new Android project or continue using an existing one with the com.vogella.android.usinglibs top-level package.

2.1. Create a Second Activity

Create a second activity and allow it to be started 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'
}

Next, create a value object using AutoValue:

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 an 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

Refactor your code to generate a Builder API for your Task. Define a static abstract class called Builder and annotate it with @AutoValue.Builder.

Update your code that passes 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();
    }
}

2.4. Explanation of the Code

In the provided solution, the Task class is defined as an immutable data structure that implements Parcelable. The create method allows you to instantiate a Task object directly, while the Builder pattern provides a more flexible way to create Task instances. By using the Builder API, you can set values for each field in a more readable manner before building the final object. The use of Parcelable ensures that Task objects can be passed between activities in an Android application seamlessly, making it easier to manage state and data transfer.

3. AutoValue resources