AutoValue for Easier Life

AutoValue for Easier Life

AutoVaulue is a library that generates Immutable value-type codes for Java 1.6+, maintained by Google, inc.

I have talked about AutoValue and AutoValue Extensions in general and some pros and cons that my team and I have figured out while developing our up with AutoValue.

While AutoValue is not applicable for ALL applications in general (For example, in Kotlin?), I am sure it may help most of the developers make their development a lot easier with scalable, maintainable generated codes and possibly additional performance optimization.

* auto/value at master · google/auto
https://github.com/google/auto/tree/master/value

* An Introduction to AutoValue
http://ryanharter.com/blog/2016/03/22/autovalue/

* A Deeper Look at AutoValue - Ryan Harter

http://ryanharter.com/blog/2016/04/08/autovalue-deep-dive/

* AutoValue Extensions - Ryan Harter

http://ryanharter.com/blog/2016/05/16/autovalue-extensions/

* AutoValue Extensions - Jake Wharton

http://jakewharton.com/auto-value-extensions-ny-android-meetup/

* Faster JSON Deserialization with AutoValue GSON extension
http://dubedout.eu/2016/07/11/faster-json-deserialization-autovalue-extension/

F9856cc7a15ed2cb9e6ebfab41fdf1cf?s=128

Shohei Kawano

July 28, 2016
Tweet

Transcript

  1. AutoValue for Easier Life shaunkawano

  2. AutoValue for Easier Life • Background of Ameba • AutoValue

    • Extra Android based talking
  3. (One of the)
 Oldest App in CyberAgent, Inc. Own Abstraction,

    Unique Implementation New Team Members Inconsistency Codes Throughout the App Writing Tests, Removing Tests Unknown Specification
  4. For Starting Out, We’d like to do 2 things. 2.

    Avoid Writing Boilerplate Codes 1. Remove Boilerplate Codes
  5. AutoValue https://github.com/google/auto/tree/master/value

  6. Article.class Amazingly Simple POJO

  7. None
  8. None
  9. We want Article to be Value-Typed

  10. We want Article to be Value-Typed (immutable, null-safe)

  11. 1. Finalize class and fields

  12. 1. Finalize class and fields 2. Add constructor

  13. 1. Finalize class and fields 2. Add constructor 3. Add

    getters
  14. None
  15. None
  16. None
  17. Is only referential equality safe enough?

  18. Override equals()

  19. None
  20. Override hashCode() (if use Article as key in Set or

    Map(s))
  21. None
  22. toString() is nice for debugging!

  23. None
  24. Parcelable?

  25. None
  26. Before-After

  27. None
  28. None
  29. ~100 Lines

  30. Add New Field? Remove New Field? Inner Classes with Parcelable?

    Adding New Parcelable Tests? Removing Parcelable Tests?
  31. AutoValue https://github.com/google/auto/tree/master/value

  32. Usage // root dependencies { classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8’ … } …

    // app dependencies { provided 'com.google.auto.value:auto-value:1.3-rc2' }
  33. AutoValue

  34. AutoValue • Add @AutoValue to make objects value- typed using

    generated boilerplate codes
  35. @AutoValue

  36. @AutoValue

  37. None
  38. AutoValue • Add @AutoValue to make objects value- typed using

    generated boilerplate codes
  39. AutoValue • Add @AutoValue to make objects value- typed using

    generated boilerplate codes • Add @AutoValue.Builder to create Builder class for your auto-value object
  40. @AutoValue.Builder

  41. @AutoValue.Builder

  42. None
  43. AutoValue • Add @AutoValue to make objects value- typed using

    generated boilerplate codes • Add @AutoValue.Builder to create Builder class for your auto-value object
  44. AutoValue • Add @AutoValue to make objects value- typed using

    generated boilerplate codes • Add @AutoValue.Builder to create Builder class for your auto-value object • AutoValue Extension support for 
 Parcelable, Gson, Moshi, Cursor, and more..
  45. Usage(auto-value-parcel) // root dependencies { classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8’ … } …

    // app dependencies { provided ‘com.google.auto.value:auto-value:1.3-rc2' // auto-value-parcel apt 'com.ryanharter.auto.value:auto-value-parcel:0.2.3-rc2' }
  46. Usage(auto-value-parcel) // root dependencies { classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8’ … } …

    // app dependencies { provided ‘com.google.auto.value:auto-value:1.3-rc2' // auto-value-parcel apt 'com.ryanharter.auto.value:auto-value-parcel:0.2.3-rc2' }
  47. AutoValue Extension: auto-value-parcel

  48. AutoValue Extension: auto-value-parcel

  49. None
  50. // AutoValue provided 'com.google.auto.value:auto-value:1.3-rc2' apt 'com.ryanharter.auto.value:auto-value-parcel:0.2.3-rc2' apt 'com.ryanharter.auto.value:auto-value-gson:0.3.2-rc1' apt 'com.gabrielittner.auto.value:auto-value-with:1.0.0-rc1'

    AutoValue in Our App
  51. Extra

  52. Extra 1. AutoValue Cos 2. AutoValue Pros

  53. AutoValue Cons ɾStrict Null Safety ɾCostly Field Update

  54. Costly Field Update

  55. Costly Field Update mSomeValue.value = “value”

  56. Costly Field Update mSomeValue.value = “value” Not Accessible OF COURSE.

  57. Costly Field Update mSomeValue = newValue

  58. “With-er” auto-value-with public abstract SomeValue withNewId(String newId); @Override public final

    SomeValue withNewId(String newId) { return new AutoValue_SomeValue(newId); } Generated Code:
  59. “With-er” auto-value-with public abstract SomeValue withNewId(String newId); @Override public final

    SomeValue withNewId(String newId) { return new AutoValue_SomeValue(newId); } Generated Code:
  60. “With-er” auto-value-with mSomeValue = mSomeValue.witNewId(newId);

  61. Strict Null Safety

  62. Strict Null Safety Non-fatal Exception: java.lang.IllegalStateException: Missing required properties: someValue

  63. AutoValue Pros

  64. AutoValue Pros ɾCommunication with Server Engineers ɾFaster JSON De/Serialization

  65. Faster JSON De/Serialization

  66. auto-value-gson // The public static method returning a TypeAdapter<Foo> is

    what // tells auto-value-gson to create a TypeAdapter for Foo. public static TypeAdapter<Foo> typeAdapter(Gson gson) { return new AutoValue_Foo.GsonTypeAdapter(gson); } } final Gson gson = new GsonBuilder() .registerTypeAdapterFactory(new AutoValueGsonTypeAdapterFactory()) .create(); https://github.com/rharter/auto-value-gson
  67. http://dubedout.eu/2016/07/11/faster-json-deserialization-autovalue-extension/

  68. http://dubedout.eu/2016/07/11/faster-json-deserialization-autovalue-extension/ “We gain from 200% to 400% speed.”

  69. Communication with Server Engineers

  70. Non-fatal Exception: java.lang.IllegalStateException: Missing required properties: someValue …

  71. “Is this value @Nullable”?

  72. “Is this value @Nullable”? “Is this document Updated?”

  73. More Active Communication Between Native Engineers and Server-side Engineers!

  74. Let’s use AutoValue for making our life easier!

  75. AutoValue for Easier Life shaunkawano fin.