Dagger2 has been released!

Dagger2 has been released!

Migrated story from Dagger1 to Dagger2 in Qiitanium App

9f4db21673dc0e7cbc313df8310309fb?s=128

Masaki Ogata

April 15, 2015
Tweet

Transcript

  1. Dagger 2.0 has been released! at potatotips #16

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

    Company $> CyberAgent, Inc. Name $> Masaki Ogata (@ogaclejapan)
  3. Qiitanium

  4. http://github.com/ogaclejapan/qiitanium ɾRxJava ɾClean Architecture + MVVMʢࣗশʣ ɾDagger ɾOkHttp

  5. http://github.com/ogaclejapan/qiitanium ɾ ɾ ɾDagger ɾ

  6. http://github.com/ogaclejapan/qiitanium ɾ ɾ ɾDagger 1.x ɾ Dagger 2.0 Migrated!

  7. About Dagger

  8. About Dagger Let me ask Wasabeef !! https://speakerdeck.com/wasabeef/dagger at potatotips

    #9
  9. Dagger2.0 Overview

  10. 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; } }
  11. 100% Proguard friendly ɾ θϩϦϑϨΫγϣϯ ɾ ࣗಈੜ੒ίʔυ΋σόοάτϨʔε͕Մೳ ɾύϑΥʔϚϯε͕޲্

  12. @Component Dagger1: FeatureModule ObjectGraph SubModule1 SubModule2 SubModule3 load Includes AppModule

    plus SubGraph
  13. @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());
  14. @Component Dagger2: AppComponent FeatureComponent Module1 Module2 Module3 modules FeatureModule AppModule

    modules dependencies
  15. @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);
  16. Migrate Dagger 1.x to 2.0
 (ͦͷұ)

  17. Scope @Module public class Module1 { @Singleton @Provided public Hoge

    provideHoge() { return new Hoge(); } } @Component(modules = { Module1.class }) public interface AppComponent { //… }
  18. Error: AppComponent (unscoped) may not reference scoped bindings: @Singleton @Provides

    Hoge Compile Error…
  19. Fix @Module public class @Provided public return new } }

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

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

    public class ActivityModule { @PerActivity @Provided public Hoge provideHoge() { return new Hoge(); } } Dagger2Ͱ͸ScopeΛಠࣗʹఆٛͯ͠࢖͏
  22. Migrate Dagger 1.x to 2.0 (ͦͷ್)

  23. 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 }); } }
  24. 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 }); } }
  25. 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 }); } }
  26. 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 }); } }
  27. 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 }); } }
  28. 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(); } }
  29. 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 } }
  30. Let’s enjoy Dagger2 !!

  31. ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ m(_ _)m