Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dagger2 has been released!

Dagger2 has been released!

Migrated story from Dagger1 to Dagger2 in Qiitanium App

Masaki Ogata

April 15, 2015
Tweet

More Decks by Masaki Ogata

Other Decks in Programming

Transcript

  1. About me Job $> Android Engineer v0.1 GitHub $> https://github.com/ogaclejapan

    Company $> CyberAgent, Inc. Name $> Masaki Ogata (@ogaclejapan)
  2. 100% Proguard friendly @Generated("dagger.internal.codegen.ComponentProcessor") public final class AppModule_ProvideApplicationFactory implements Factory<Application>

    { private final AppModule module; @Override public Application get() { Application provided = module.provideApplication(); if (provided == null) { throw new NullPointerException("Cannot return null from a non-@Nullable @Provides method"); } return provided; } }
  3. @Component Dagger1: @Module(includes={Module1.class, …}, injects={…}) public class AppModule { @Provided

    public Hoge provideHoge() { return new Hoge(); } } ObjectGraph graph = ObjectGraph.create(new Object[] { new AppModule()}); graph.inject(this); ObjectGraph sub = graph.plus(new FeatureModule());
  4. @Component Dagger2: @Component(modules={ AppModule.class, …}) public interface AppComponent { public

    void inject(Application app); } @Generated(“…”) public class DaggerAppComponent { //… } Annotation
 Processor AppComponent component = DaggerAppComponent.builder() .appModule(…).build(); component.inject(this);
  5. Scope @Module public class Module1 { @Singleton @Provided public Hoge

    provideHoge() { return new Hoge(); } } @Component(modules = { Module1.class }) public interface AppComponent { //… }
  6. Fix @Module public class @Provided public return new } }

    @Component(modules = { public interface AppComponent { //… } @Singleton
  7. Custom Scope Component୯ҐͰΠϯελϯεΛ؅ཧ͍ͯ͠Δ @Generated(“…”) public final class DaggerAppComponent { //…

    private void initialize(final Builder builder) { this.…Provider = ScopedProvider.create(…); } //… } ScopedProviderͰSingleton੍ޚ͕͞Ε͍ͯΔ
  8. Custom Scope @Scope @Retention(RetentionPolicy.RUNTIME) public @interface PerActivity { } @Module(modules={…})

    public class ActivityModule { @PerActivity @Provided public Hoge provideHoge() { return new Hoge(); } } Dagger2Ͱ͸ScopeΛಠࣗʹఆٛͯ͠࢖͏
  9. Override Module Dagger1: src/debug/java/.. @Module(addsTo=AppModule.class, override=true, …) public class DebugAppModule

    { @Singleton @Provided public Hoge provideHoge() { return new DebugHoge(); } } class Modules { Object[] list() { return ObjectGraph.create(new Object[] { new AppModule(), new DebugAppModule }); } }
  10. Override Module Dagger2: src/debug/java/.. @Module(addsTo=AppModule.class, override=true, …) public class DebugAppModule

    { @Singleton @Provided public Hoge provideHoge() { return new DebugHoge(); } } class Modules { Object[] list() { return ObjectGraph.create(new Object[] { new AppModule(), new DebugAppModule }); } }
  11. Override Module Dagger2: src/debug/java/.. @Module(addsTo=AppModule.class, override=true, …) public class DebugAppModule

    extends AppModule { @Singleton @Provided public Hoge provideHoge() { return new DebugHoge(); } } class Modules { Object[] list() { return ObjectGraph.create(new Object[] { new AppModule(), new DebugAppModule }); } }
  12. Override Module Dagger2: src/debug/java/.. @Module(addsTo=AppModule.class, override=true, …) public class DebugAppModule

    extends AppModule { @Singleton @Provided @Override public Hoge provideHoge() { return new DebugHoge(); } } class Modules { Object[] list() { return ObjectGraph.create(new Object[] { new AppModule(), new DebugAppModule }); } }
  13. Override Module Dagger2: src/debug/java/.. @Module(addsTo=AppModule.class, override=true, …) public class DebugAppModule

    extends AppModule { @Singleton @Provided @Override public Hoge provideHoge() { return new DebugHoge(); } } class Modules { Object[] list() { return ObjectGraph.create(new Object[] { new AppModule(), new DebugAppModule }); } }
  14. Override Module Dagger2: src/debug/java/.. @Module(addsTo=AppModule.class, override=true, …) public class DebugAppModule

    extends AppModule { @Singleton @Provided @Override public Hoge provideHoge() { return new DebugHoge(); } } class Modules { AppModule appModule() { return new DebugAppModule(); } }
  15. Override Module Dagger2: src/main/java/.. public class MyApplication extends Application {

    @Inject Hoge mHoge; @Override public void onCreate() { AppComponent component = DaggerAppComponent.builder() .appModule(Modules.appModule()) .build(); component.inject(this); System.out.println(mHoge instanceof DebugHoge); //true } }