Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Clean Apps with Dagger 2

Clean Apps with Dagger 2

Dagger 2 is a fast dependency injector for both Java and Android. This talk explores using Dagger 2 to enable clean, lean and maintainable Android apps.

Demo App: https://github.com/alexrwegener/dagger2-scopes
Video link to follow.

Clean Apps with Dagger 2 was presented on June 16th, 2015 at the Indy GDG.

Avatar for Alex Wegener

Alex Wegener

June 16, 2015
Tweet

Other Decks in Technology

Transcript

  1. • Design Pattern • Facilitates Single Responsiblity Principle • Simple

    unit tests • Cleaner code Dependency Injection
  2. public final class GDG { private final Devs devs; private

    final Location location; private final Speaker speaker; public GDG() { this.location = new DeveloperTown(); this.speaker = new BillMote(); this.devs = new RandomPeopleFromTheirHut(); } public void meetup() { location.host(); do { speaker.present(); } while (devs.areEatingPizza()); } }
  3. public final class GDG { private final Devs devs; private

    final Location location; private final Speaker speaker; public GDG(Devs devs, Location location, Speaker speaker) { this.devs = devs; this.location = location; this.speaker = speaker; } public void meetup() { location.host(); do { speaker.present(); } while (devs.areEatingPizza()); } }
  4. public static void main(String[] args) { final GDG gdg =

    new GDG(new AndroidEnthusiasts(), new DeveloperTown(), new AlexWegener()); gdg.meetup(); } public static void main(String[] args) { final GDG gdg = new GDG(); gdg.meetup(); }
  5. InjectConstructorValidator injectConstructorValidator = new InjectConstructorValidator(); InjectFieldValidator injectFieldValidator = new InjectFieldValidator();

    InjectMethodValidator injectMethodValidator = new InjectMethodValidator(); ModuleValidator moduleValidator = new ModuleValidator(types, elements, methodSignatureFormatter, Module.class, Provides.class); ProvidesMethodValidator providesMethodValidator = new ProvidesMethodValidator(elements); BuilderValidator componentBuilderValidator = new BuilderValidator(elements, types, ComponentDescriptor.Kind. COMPONENT); BuilderValidator subcomponentBuilderValidator = new BuilderValidator(elements, types, ComponentDescriptor.Kind. SUBCOMPONENT); ComponentValidator subcomponentValidator = ComponentValidator.createForSubcomponent(elements, types, moduleValidator, subcomponentBuilderValidator); ComponentValidator componentValidator = ComponentValidator.createForComponent(elements, types, moduleValidator, subcomponentValidator, subcomponentBuilderValidator); MapKeyValidator mapKeyValidator = new MapKeyValidator(); ModuleValidator producerModuleValidator = new ModuleValidator(types, elements, methodSignatureFormatter, ProducerModule.class, Produces.class); ProducesMethodValidator producesMethodValidator = new ProducesMethodValidator(elements); ProductionComponentValidator productionComponentValidator = new ProductionComponentValidator();
  6. Why Dagger 2? • Extremely fast • Proguard friendly •

    Generated code is human readable • Compile time validation errors • Clean stack traces • Scopes
  7. #MoreAndroidDevProblems • Giant ball of mud • Vast amount of

    managed state • Historically hard to unit / ui test • Humans are bad at testing
  8. Android View Android Java Presenter Java Interactor Store Api Intent

    Owner Toolbar Key Interface Connection Owner
  9. @ActivityScope @Component(dependencies = AppComponent.class, modules = ActivityModule.class) public interface ActivityComponent

    { void inject(MainActivity activity); AuthInteractor authInteractor(); NavigationOwner navigationOwner(); Application application(); }
  10. Collaborator / Contract Relationship UserInteractor UserStore Find the authenticated user’s

    pref? Can I find the authenticated user’s pref? I can return a user’s pref or an error Can I handle a user’s pref and an error?
  11. Android View Android Java Presenter Java Interactor Store Api Intent

    Owner Toolbar Key Interface Connection Owner
  12. Android View Android Java Presenter Java Interactor Store Api Intent

    Owner Toolbar Key Interface Connection Owner
  13. References Tools: • Mockito: http://mockito.org/ • Google Truth: https://github.com/google/truth •

    Dagger 2 documentation: http://google.github.io/dagger/ • APT: https://bitbucket.org/hvisser/android-apt
  14. References Blogs / Articles: • Martin Fowler on DI: http://martinfowler.com/articles/injection.html

    • SOLID Principles: http://en.wikipedia.org/wiki/SOLID_%28object- oriented_design%29 • Fernando Cejas on clean architecture: http: //fernandocejas.com/2014/09/03/architecting-android-the-clean-way/ • Uncle Bob on Clean Architecture: http://blog.8thlight. com/uncle-bob/2012/08/13/the-clean-architecture.html
  15. References Talks: • JW on Dagger 1: https://www.parleys.com/tutorial/architecting-android- applications-dagger •

    JW on Dagger 2: https://www.parleys.com/tutorial/the-future-dependency- injection-dagger-2 • Greg Kick on Dagger 2: https://www.youtube.com/watch? v=oK_XtfXPkqw
  16. References Talks: • Pierre-Yves on Mortar & Flow: https://www.youtube.com/watch? v=R8NbpkpSuw8

    • JBrains on unit / integration tests: https://vimeo. com/80533536