Tuesday 29 October 2013

Droidcon 2013: Boundbox & Memento + Annotation Processing

Stéphane Nicolas, Octo Technology

Matthias Käppler, Soundcloud @mttkay

boundbox

Slides available online

https://github.com/stephanenicolas/boundbox

  • break encapsulation to ease testing (with Android in mind)
  • let’s you access things from Android SDK that you wouldn’t normally access
  • also lets you add fields (e.g. for stub Android jars that only have methods available)
  • can indicate how far up the hierarchy you want to expose
  • don’t need to change your code — just adjust your tests
  • access using reflection, but access checked at compile time
  • open source on github
  • compares with WhiteBox from PowerMock
  • intended for helping test UI layer
    • find views by field name
    • invoke methods & inner classes directly
    • faster and more direct than robotium & UIAutomator
    • as fast as espresso

memento

https://github.com/mttkay/memento

  • dealing with keeping state when the screen rotates
  • the activity gets destroyed and recreated and all your state is thrown away…
  • can use onRetainNonConfigurationInstance to pass objects through configuration changes
    • but what if you need to retain more than one object
    • deprecated in place of fragments — these keep their configuration using setRetainInstance
    • but watch out: setRetainInstance doesn’t work for child fragments and is super-prone for data leaks
  • but what if you have multiple fragments sharing the same state?
  • mark a package private field as @Retain
  • override the new onLaunch method (used to indicate first time initialisation of the class i.e. not after a config change)
  • then use Memento.retain(this)

annotation processing in general

  • use JavaWriter from squareup
  • look at how square does it for dagger dependency injection
  • Google has a library so you can assert over java source files
    • lets you compile with a processor and check the output
  • want to split up the annotation processor into two:
    • one for the client-side annotation itself and any required classes
    • one for the processor (don’t want it in your app)

No comments: