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. Dagger 2.0 has been
    released!
    at potatotips #16

    View full-size slide

  2. About me
    Job $> Android Engineer v0.1
    GitHub $> https://github.com/ogaclejapan
    Company $> CyberAgent, Inc.
    Name $> Masaki Ogata (@ogaclejapan)

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  6. About Dagger

    View full-size slide

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

    View full-size slide

  8. Dagger2.0 Overview

    View full-size slide

  9. 100% Proguard friendly
    @Generated("dagger.internal.codegen.ComponentProcessor")
    public final class AppModule_ProvideApplicationFactory
    implements Factory {
    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;
    }
    }

    View full-size slide

  10. 100% Proguard friendly
    ɾ θϩϦϑϨΫγϣϯ
    ɾ ࣗಈੜ੒ίʔυ΋σόοάτϨʔε͕Մೳ
    ɾύϑΥʔϚϯε͕޲্

    View full-size slide

  11. @Component
    Dagger1:
    FeatureModule
    ObjectGraph
    SubModule1
    SubModule2
    SubModule3
    load
    Includes
    AppModule
    plus
    SubGraph

    View full-size slide

  12. @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());

    View full-size slide

  13. @Component
    Dagger2:
    AppComponent
    FeatureComponent
    Module1
    Module2
    Module3
    modules
    FeatureModule
    AppModule
    modules
    dependencies

    View full-size slide

  14. @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);

    View full-size slide

  15. Migrate Dagger 1.x to 2.0

    (ͦͷұ)

    View full-size slide

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

    View full-size slide

  17. Error: AppComponent (unscoped) may not reference
    scoped bindings:
    @Singleton @Provides Hoge
    Compile Error…

    View full-size slide

  18. Fix
    @Module
    public class
    @Provided
    public
    return new
    }
    }
    @Component(modules = {
    public interface AppComponent {
    //…
    }
    @Singleton

    View full-size slide

  19. Custom Scope
    Component୯ҐͰΠϯελϯεΛ؅ཧ͍ͯ͠Δ
    @Generated(“…”)
    public final class DaggerAppComponent {
    //…
    private void initialize(final Builder builder) {
    this.…Provider = ScopedProvider.create(…);
    }
    //…
    }
    ScopedProviderͰSingleton੍ޚ͕͞Ε͍ͯΔ

    View full-size slide

  20. Custom Scope
    @Scope
    @Retention(RetentionPolicy.RUNTIME)
    public @interface PerActivity {
    }
    @Module(modules={…})
    public class ActivityModule {
    @PerActivity
    @Provided
    public Hoge provideHoge() {
    return new Hoge();
    }
    }
    Dagger2Ͱ͸ScopeΛಠࣗʹఆٛͯ͠࢖͏

    View full-size slide

  21. Migrate Dagger 1.x to 2.0
    (ͦͷ್)

    View full-size slide

  22. 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 });
    }
    }

    View full-size slide

  23. 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 });
    }
    }

    View full-size slide

  24. 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 });
    }
    }

    View full-size slide

  25. 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 });
    }
    }

    View full-size slide

  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 });
    }
    }

    View full-size slide

  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 {
    AppModule appModule() {
    return new DebugAppModule();
    }
    }

    View full-size slide

  28. 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
    }
    }

    View full-size slide

  29. Let’s enjoy Dagger2 !!

    View full-size slide

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

    View full-size slide