Project Url: xizzhu/auto-droid
Introduction: AutoValue Extension for Android
More: Author   ReportBugs   OfficialWebsite   

Build Status Release

An extension for Google's AutoValue that generates the following for for @AutoValue annotated objects:

  • createFromSharedPreferences(SharedPreferences sharedPreferences)
  • createFromCursor(Cursor cursor)
  • Parcelable implementations if the class implements Parcelable


Add the following to your build.gradle:

buildscript {
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

repositories {
    maven { url "" }

apply plugin: ''

dependencies {
    compile ''
    apt ''


From shared preference

// annotate your AutoValue objects as usual
public abstract class MyPreference {
    // indicate that the factory will retrieve the value from shared preferences
    @SharedPreference(key = "keyOfMyInt", defaultValue = "8964")
    abstract int myIntFromPreference();

    // adds the factory method
    public static MyPreference create(SharedPreferences sharedPreferences) {
        return AutoValue_MyPreference.createFromSharedPreferences(sharedPreferences);

For objects constructed from shared preferences, the following types are supported:

  • boolean / Boolean: the default value is false if not specified
  • int / Integer: the default value is 0 if not specified
  • long / Long: the default value is 0L if not specified
  • float / Float: the default value is 0.0F if not specified
  • double / Double: the default value is 0.0 if not specified
  • String: the default value is null if not specified
  • Set<String>: the default value is always null

From cursor

// annotate your AutoValue objects as usual
public abstract class MyCursor {
    // indicate that the factory will retrieve the value from cursor
    abstract int myIntFromCursor();

    // indicate to use MyColumnAdapterFactory to generate the value
    // MyColumnAdapterFactory must provide a `static` method that
    // takes a `Cursor` and returns a `MyColumnAdapter`
    abstract MyColumnAdapter myCustomColumn();

    // indicate to implement a method to create a ContentValues object with values put in
    abstract ContentValues toContentValues();

    // adds the factory method
    public static MyCursor create(Cursor cursor) {
        return AutoValue_MyCursor.createFromCursor(cursor);

For objects constructed from cursors, the following types are supported:

  • int / Integer
  • long / Long
  • short / Short
  • float / Float
  • double / Double
  • byte[]
  • String

Implements parcelable

// annotate your AutoValue objects as usual
// and just tells that it impements Parcelable
public abstract class MyCursor implements Parcelable {
    // whatever you may have here


Copyright 2016 Xizhi Zhu

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.
About Me
Google+: Trinea trinea
GitHub: Trinea