Slide 1

Slide 1 text

Dagger 2.0 New dependency injection for Android Radosław Piekarz

Slide 2

Slide 2 text

Spring ● Popular in the Java community ● Polularized DI/IoC ● XML configuration

Slide 3

Slide 3 text

Guice / RoboGuice ● Used new features in Java 1.5 ● No XML configuration ● Not so easy to debug ● Runtime validation

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Dagger 1 ● Compile time validation ● Do more work during compile and less in runtime ● Faster than Guice/RoboGuice

Slide 7

Slide 7 text

Dagger 1 ● Not proguard safe ● Runtime graph composition

Slide 8

Slide 8 text

Source: Jake Wharton

Slide 9

Slide 9 text

Minimal graph creation time - not just fast, but hand-written DI fast

Slide 10

Slide 10 text

Full, static inspection of code paths

Slide 11

Slide 11 text

No reflection, no explicit classloading

Slide 12

Slide 12 text

Dagger 2 API ● @Module & @Provides - mechanism for providing dependencies ● @Inject - mechanism for requesting dependencies ● @Component bridge between modules and injections

Slide 13

Slide 13 text

Example module @Module class PumpModule { @Provides Pump providePump(Thermosiphon pump) { return pump; } }

Slide 14

Slide 14 text

@Inject class CoffeeMaker { private final Lazy heater; private final Pump pump; @Inject CoffeeMaker(Lazy heater, Pump pump) { this.heater = heater; this.pump = pump; }

Slide 15

Slide 15 text

@Component @Component(modules = {DripCoffeeModule.class}) public interface Coffee { CoffeeMaker maker(); }

Slide 16

Slide 16 text

Example app public class CoffeeApp { public static void main(String[] args) { Coffee coffee = Dagger_Coffee.builder() .build(); coffee.maker().brew(); } }

Slide 17

Slide 17 text

Dagger 1 vs Dagger 2 public class CoffeeApp implements Runnable { @Inject CoffeeMaker coffeeMaker; @Override public void run() { coffeeMaker.brew(); } public static void main(String[] args) { ObjectGraph objectGraph = ObjectGraph.create(new DripCoffeeModule()); CoffeeApp coffeeApp = objectGraph.get(CoffeeApp.class); coffeeApp.run(); } }

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Quick demos

Slide 20

Slide 20 text

u2020 & u2021 https://github.com/Jak eWharton/u2020 https://github.com/cgr uber/u2020/tree/dagg er2

Slide 21

Slide 21 text

Dagger 2 Pros ● It is really fast ● Compile time validation for entire graph ● Easy debugging Cons ● Not flexible ● There is no dynamism

Slide 22

Slide 22 text

https://github.com/google/dagger/ https://google.github.io/dagger/

Slide 23

Slide 23 text

Q&A Thank you for your attention! radzio RadoslawPiekarz { }