injections • Fail as early as possible (compile-time, not runtime) • Eliminate reflection on methods and annotations at runtime • Have negligible memory impact
Jesse Wilson • Bob Lee served as technical advisor • “Giant” boolean switch in our applications • 2 weeks after, dropped Guice completely • Renamed to Dagger before first release
@Module annotation on the class provides static analysis hints • @Provider annotation on a method indicates that its return type is a dependency • Designed to be composed together
constructor arguments are dependencies • Dependencies can be stored in private and final fields • Dagger must create the object • @Provides not required for downstream injection
new TwitterModule("JakeWharton") ); // Using constructor injection: TweeterApp app = og.get(TweeterApp.class); // Using field injection: TweeterApp app = new TweeterApp(); og.inject(app);
new PersistenceModule(), new AccountModule() ); // Later... String user = "JakeWharton"; ObjectGraph userOg = og.plus(new TwitterModule(user)); // Inject app things using ‘og’... // Inject user things using ‘userOg’...
• Multiple services, activities, etc. required shared state • Platform is very difficult to test • Build system allows for dynamic flavors and build types
• Multiple services, activities, etc. required shared state • Platform is very difficult to test • Build system allows for dynamic flavors and build types • Many libraries require keeping singletons or long-lived objects
on a module • Used for aggressive static analysis • Potentially not needed for full compilation... • ...but absolutely required for incremental compilation
static Object[] list() { return new Object[] { new WalletModule() }; } } // src/debug/java ! final class Modules { static Object[] list() { return new Object[] { new WalletModule(), new DebugWalletModule() }; } }
Guice or RoboGuice may be attractive because they can simplify the code you write and provide an adaptive environment that's useful for testing and other configuration changes. However, these frameworks tend to perform a lot of process initialization by scanning your code for annotations, which can require significant amounts of your code to be mapped into RAM even though you don't need it. d.android.com/training/articles/memory.html#DependencyInjection