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

android-multi-module-with-dagger

 android-multi-module-with-dagger

DroidKaigi2019
Day 01 11:20~
「マルチモジュールプロジェクトでの Dagger2を用いた Dependency Injection」
での発表資料です

サンプルコード
https://github.com/kgmyshin/Multi-Module-Project-With-Dagger

Shinnosuke Kugimiya

February 07, 2019
Tweet

More Decks by Shinnosuke Kugimiya

Other Decks in Programming

Transcript

  1. X © DMM.com w %FQFOEFODZ*OKFDUJPOͱ͸ʢ؆୯ʹʣ w %BHHFS࠶ೖ໳ w %BHHFSͷ"OESPJEαϙʔτ࠶ೖ໳ w

    γϯάϧϞδϡʔϧͰͷ%BHHFSΛ༻͍ͨ%FQFOEFODZ*OKFDUJPO w ϚϧνϞδϡʔϧͰͷ%FQFOEFODZ*OKFDUJPOͷਏ͞ w %BHHFSΛ࢖ͬͯϚϧνϞδϡʔϧϓϩδΣΫτͰ%*Λ࣮ݱ͢Δ w %ZOBNJD'FBUVSF.PEVMF͕͋Δ࣌ͷ࿩ʢ͕࣌ؒ͋Ε͹ʣ "HFOEB ΰʔϧ ϚϧνϞδϡʔϧͰߏ੒͞ΕͨϓϩδΣΫτͰɺ %BHHFSΛ༻͍ͨ%FQFOEFODZ*OKFDUJPOΛߦ͏ࡍͷ ೰ΈϙΠϯτΛ஌ΓɺͦͷղܾࡦΛཧղ͢Δɻ
  2. X © DMM.com BHFOEB w %FQFOEFODZ*OKFDUJPOͱ͸ʢ؆୯ʹʣ w %BHHFS࠶ೖ໳ w %BHHFSͷ"OESPJEαϙʔτ࠶ೖ໳

    w γϯάϧϞδϡʔϧͰͷ%BHHFSΛ༻͍ͨ%FQFOEFODZ*OKFDUJPO w ϚϧνϞδϡʔϧͰͷ%FQFOEFODZ*OKFDUJPOͷਏ͞ w %BHHFSΛ࢖ͬͯϚϧνϞδϡʔϧϓϩδΣΫτͰ%*Λ࣮ݱ͢Δ w %ZOBNJD'FBUVSF.PEVMF͕͋Δ࣌ͷ࿩
  3. X © DMM.com %FQFOEFODZ*OKFDUJPOͱ͸ʢ؆୯ʹʣ internal class AllergenRepositoryImpl : AllergenRepository {

    private val apiClient = ApiClient() override fun findAll( ): Single<List<Allergen>> = apiClient.getAllergens() override fun findById( allergenId: AllergenId ): Maybe<Allergen> = apiClient.getAllergen(allergenId.value) } %*͠ͳ͍
  4. X © DMM.com %FQFOEFODZ*OKFDUJPOͱ͸ʢ؆୯ʹʣ internal class AllergenRepositoryImpl @Inject constructor( private

    val apiClient: ApiClient ) : AllergenRepository { override fun findAll( ): Single<List<Allergen>> = apiClient.getAllergens() override fun findById( allergenId: AllergenId ): Maybe<Allergen> = apiClient.getAllergen(allergenId.value) } internal class AllergenRepositoryImpl : AllergenRepository { private val apiClient = ApiClient() override fun findAll( ): Single<List<Allergen>> = apiClient.getAllergens() override fun findById( allergenId: AllergenId ): Maybe<Allergen> = apiClient.getAllergen(allergenId.value) } %*͠ͳ͍ %*͢Δ
  5. X © DMM.com %FQFOEFODZ*OKFDUJPOͱ͸ʢ؆୯ʹʣ internal class AllergenRepositoryImpl @Inject constructor( private

    val apiClient: ApiClient ) : AllergenRepository { override fun findAll( ): Single<List<Allergen>> = apiClient.getAllergens() override fun findById( allergenId: AllergenId ): Maybe<Allergen> = apiClient.getAllergen(allergenId.value) } internal class AllergenRepositoryImpl : AllergenRepository { private val apiClient = ApiClient() override fun findAll( ): Single<List<Allergen>> = apiClient.getAllergens() override fun findById( allergenId: AllergenId ): Maybe<Allergen> = apiClient.getAllergen(allergenId.value) } %*͠ͳ͍ %*͢Δ
  6. X © DMM.com %FQFOEFODZ*OKFDUJPOͱ͸ʢ؆୯ʹʣ ϝϦοτ w ݁߹͕ૄʹͳΔ w ୯ମςετ͕͠΍͍͢ w

    ಛఆͷϥΠϒϥϦ΍ϑϨʔϜϫʔΫ΁ͷґଘ౓ΛԼ͛Δ͜ͱ͕Ͱ ͖Δ
  7. X © DMM.com %BHHFSͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ

     ֤ॴͰJOKFDU %BHHFSΛ࢖࣮ͬͨ૷ͷྲྀΕ ˞ɹखॱ̏͸৔߹ʹΑΔ͕ɺ͜͜Ͱ͸؆ུԽͷͨΊʹ͜ͷखॱʹݻఆ͢Δ ˞ɹҎ߱ͷྫͰ͸'SBHNFOU΁ͷ*OKFDUΛର৅ͱ͍ͯ͠Δ͕ɺଞͷΫϥεʹஔ͖׵͑ͯ΋໰୊ͳ͍
  8. X © DMM.com %BHHFSͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ

     ֤ॴͰJOKFDU %BHHFSΛ࢖࣮ͬͨ૷ͷྲྀΕ ˞ɹखॱ̏͸৔߹ʹΑΔ͕ɺ͜͜Ͱ͸؆ུԽͷͨΊʹ͜ͷखॱʹݻఆ͢Δ ˞ɹҎ߱ͷྫͰ͸'SBHNFOU΁ͷ*OKFDUΛର৅ͱ͍ͯ͠Δ͕ɺଞͷΫϥεʹஔ͖׵͑ͯ΋໰୊ͳ͍
  9. X © DMM.com ᶃ.PEVMFΛ༻ҙ͢Δ @Module internal abstract class GreetingModule {

    @Binds abstract fun provideGreeter(impl: GreeterImpl): Greeter } @Module internal class CounterModule { @Singleton @Provides fun provideCounter(): Counter = CounterImpl() } .PEVMFͷॻ͖ํͷྫܕͱΠϯελϯεͷඥ෇͚Λߦ͏
  10. X © DMM.com ᶃ.PEVMFΛ༻ҙ͢Δ @Module internal abstract class GreetingModule {

    @Binds abstract fun provideGreeter(impl: GreeterImpl): Greeter } @Module internal class CounterModule { @Singleton @Provides fun provideCounter(): Counter = CounterImpl() } .PEVMFͷॻ͖ํͷྫܕͱΠϯελϯεͷඥ෇͚Λߦ͏
  11. X © DMM.com ᶃ.PEVMFΛ༻ҙ͢Δ @Module internal abstract class GreetingModule {

    @Binds abstract fun provideGreeter(impl: GreeterImpl): Greeter } @Module internal class CounterModule { @Singleton @Provides fun provideCounter(): Counter = CounterImpl() } .PEVMFͷॻ͖ํͷྫܕͱΠϯελϯεͷඥ෇͚Λߦ͏
  12. X © DMM.com ᶃ.PEVMFΛ༻ҙ͢Δ @Module internal abstract class GreetingModule {

    @Binds abstract fun provideGreeter(impl: GreeterImpl): Greeter } @Module internal class CounterModule { @Singleton @Provides fun provideCounter(): Counter = CounterImpl() } .PEVMFͷॻ͖ํͷྫ
  13. X © DMM.com ᶃ.PEVMFΛ༻ҙ͢Δ @Module internal abstract class GreetingModule {

    @Binds abstract fun provideGreeter(impl: GreeterImpl): Greeter } @Module internal class CounterModule { @Singleton @Provides fun provideCounter(): Counter = CounterImpl() } .PEVMFͷॻ͖ํͷྫܕͱΠϯελϯεͷඥ෇͚Λߦ͏
  14. X © DMM.com %BHHFSͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ

     ֤ॴͰJOKFDU %BHHFSΛ࢖࣮ͬͨ૷ͷྲྀΕ ˞ɹखॱ̏͸৔߹ʹΑΔ͕ɺ͜͜Ͱ͸؆ུԽͷͨΊʹ͜ͷखॱʹݻఆ͢Δ ˞ɹҎ߱ͷྫͰ͸'SBHNFOU΁ͷ*OKFDUΛର৅ͱ͍ͯ͠Δ͕ɺଞͷΫϥεʹஔ͖׵͑ͯ΋໰୊ͳ͍
  15. X © DMM.com %BHHFSͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ

     ֤ॴͰJOKFDU %BHHFSΛ࢖࣮ͬͨ૷ͷྲྀΕ ˞ɹखॱ̏͸৔߹ʹΑΔ͕ɺ͜͜Ͱ͸؆ུԽͷͨΊʹ͜ͷखॱʹݻఆ͢Δ ˞ɹҎ߱ͷྫͰ͸'SBHNFOU΁ͷ*OKFDUΛର৅ͱ͍ͯ͠Δ͕ɺଞͷΫϥεʹஔ͖׵͑ͯ΋໰୊ͳ͍
  16. X © DMM.com ᶄ$PNQPOFOUΛ༻ҙ͢Δ @Singleton @Component(modules = [GreetingModule::class]) interface GreetingComponent

    { fun inject(activity: GreetingActivity) fun inject(fragment: GreetingFragment) fun greeter(): Greeter } $PNQPOFOUͷॻ͖ํͷྫJOKFDUઌΛҾ਺ʹͨؔ͠਺Λఆٛ͢Δ
  17. X © DMM.com ᶄ$PNQPOFOUΛ༻ҙ͢Δ @Singleton @Component(modules = [GreetingModule::class]) interface GreetingComponent

    { fun inject(activity: GreetingActivity) fun inject(fragment: GreetingFragment) fun greeter(): Greeter } $PNQPOFOUͷॻ͖ํͷྫJOKFDUઌΛҾ਺ʹͨؔ͠਺Λఆٛ͢Δ
  18. X © DMM.com ᶄ$PNQPOFOUΛ༻ҙ͢Δ @Singleton @Component(modules = [GreetingModule::class]) interface GreetingComponent

    { fun inject(activity: GreetingActivity) fun inject(fragment: GreetingFragment) fun greeter(): Greeter } $PNQPOFOUͷॻ͖ํͷྫJOKFDUઌΛҾ਺ʹͨؔ͠਺Λఆٛ͢Δ
  19. X © DMM.com ᶄ$PNQPOFOUΛ༻ҙ͢Δ @Singleton @Component(modules = [GreetingModule::class]) interface GreetingComponent

    { fun inject(activity: GreetingActivity) fun inject(fragment: GreetingFragment) fun greeter(): Greeter } $PNQPOFOUͷॻ͖ํͷྫJOKFDUઌΛҾ਺ʹͨؔ͠਺Λఆٛ͢Δ
  20. X © DMM.com %BHHFSͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ

     ֤ॴͰJOKFDU %BHHFSΛ࢖࣮ͬͨ૷ͷྲྀΕ ˞ɹखॱ̏͸৔߹ʹΑΔ͕ɺ͜͜Ͱ͸؆ུԽͷͨΊʹ͜ͷखॱʹݻఆ͢Δ ˞ɹҎ߱ͷྫͰ͸'SBHNFOU΁ͷ*OKFDUΛର৅ͱ͍ͯ͠Δ͕ɺଞͷΫϥεʹஔ͖׵͑ͯ΋໰୊ͳ͍
  21. X © DMM.com %BHHFSͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ

     ֤ॴͰJOKFDU %BHHFSΛ࢖࣮ͬͨ૷ͷྲྀΕ ˞ɹखॱ̏͸৔߹ʹΑΔ͕ɺ͜͜Ͱ͸؆ུԽͷͨΊʹ͜ͷखॱʹݻఆ͢Δ ˞ɹҎ߱ͷྫͰ͸'SBHNFOU΁ͷ*OKFDUΛର৅ͱ͍ͯ͠Δ͕ɺଞͷΫϥεʹஔ͖׵͑ͯ΋໰୊ͳ͍
  22. X © DMM.com ᶅ"QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ class Application : android.app.Application(){ lateinit var

    appComponent: AppComponent override fun onCreate() { super.onCreate() appComponent = DaggerAppComponent.create() } }
  23. X © DMM.com ᶅ"QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ class Application : android.app.Application(){ lateinit var

    appComponent: AppComponent override fun onCreate() { super.onCreate() appComponent = DaggerAppComponent.create() } }
  24. X © DMM.com %BHHFSͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ

     ֤ॴͰJOKFDU %BHHFSΛ࢖࣮ͬͨ૷ͷྲྀΕ ˞ɹखॱ̏͸৔߹ʹΑΔ͕ɺ͜͜Ͱ͸؆ུԽͷͨΊʹ͜ͷखॱʹݻఆ͢Δ ˞ɹҎ߱ͷྫͰ͸'SBHNFOU΁ͷ*OKFDUΛର৅ͱ͍ͯ͠Δ͕ɺଞͷΫϥεʹஔ͖׵͑ͯ΋໰୊ͳ͍
  25. X © DMM.com %BHHFSͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ

     ֤ॴͰJOKFDU %BHHFSΛ࢖࣮ͬͨ૷ͷྲྀΕ ˞ɹखॱ̏͸৔߹ʹΑΔ͕ɺ͜͜Ͱ͸؆ུԽͷͨΊʹ͜ͷखॱʹݻఆ͢Δ ˞ɹҎ߱ͷྫͰ͸'SBHNFOU΁ͷ*OKFDUΛର৅ͱ͍ͯ͠Δ͕ɺଞͷΫϥεʹஔ͖׵͑ͯ΋໰୊ͳ͍
  26. X © DMM.com ᶆ֤ॴͰ*OKFDU͢Δ class MainFragment : Fragment() { @Inject

    lateinit var greeter: Greeter override fun onAttach(context: Context?) { super.onAttach(context) (requireActivity().application as Application) .appComponent.inject(this) } : }
  27. X © DMM.com ᶆ֤ॴͰ*OKFDU͢Δ class MainFragment : Fragment() { @Inject

    lateinit var greeter: Greeter override fun onAttach(context: Context?) { super.onAttach(context) (requireActivity().application as Application) .appComponent.inject(this) } : }
  28. X © DMM.com ᶆ֤ॴͰ*OKFDU͢Δ class MainFragment : Fragment() { @Inject

    lateinit var greeter: Greeter override fun onAttach(context: Context?) { super.onAttach(context) (requireActivity().application as Application) .appComponent.inject(this) } : }
  29. X © DMM.com %BHHFSͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ

     ֤ॴͰJOKFDU %BHHFSΛ࢖࣮ͬͨ૷ͷྲྀΕ ˞ɹखॱ̏͸৔߹ʹΑΔ͕ɺ͜͜Ͱ͸؆ུԽͷͨΊʹ͜ͷखॱʹݻఆ͢Δ ˞ɹҎ߱ͷྫͰ͸'SBHNFOU΁ͷ*OKFDUΛର৅ͱ͍ͯ͠Δ͕ɺଞͷΫϥεʹஔ͖׵͑ͯ΋໰୊ͳ͍
  30. X © DMM.com %BHHFS"OESPJEͷϞνϕʔγϣϯ class MainFragment : Fragment() { @Inject

    lateinit var greeter: Greeter override fun onAttach(context: Context?) { super.onAttach(context) (requireActivity().application as Application) .appComponent.inject(this) } : }
  31. X © DMM.com %BHHFS"OESPJEͷϞνϕʔγϣϯ class MainFragment : Fragment() { @Inject

    lateinit var greeter: Greeter override fun onAttach(context: Context?) { super.onAttach(context) (requireActivity().application as Application) .appComponent.inject(this) } : } *OKFDU͞ΕΔଆ͕ɺ*OKFDUPSʹ͍ͭͯͷৄࡉΛ஌ͬͯ͠·͍ͬͯΔɻ
  32. X © DMM.com %BHHFS"OESPJEͷϞνϕʔγϣϯ class MainFragment : Fragment() { @Inject

    lateinit var greeter: Greeter override fun onAttach(context: Context?) { super.onAttach(context) (requireActivity().application as Application) .appComponent.inject(this) } : } *OKFDU͞ΕΔଆ͕ɺ*OKFDUPSʹ͍ͭͯͷৄࡉΛ஌ͬͯ͠·͍ͬͯΔɻ Ͳ͜ʹ*OKFDUPS͕͋Δͷ͔ʁͲͷ*OKFDUPS͕ࣗ෼Λ*OKFDUͰ͖Δͷ͔ʁͳͲ
  33. X © DMM.com %BHHFS"OESPJEͷϞνϕʔγϣϯ class MainFragment : Fragment() { @Inject

    lateinit var greeter: Greeter override fun onAttach(context: Context?) { AndroidSupportInjection.inject(this) super.onAttach(context) } : } ͜͏͢Ε͹ɺྑͦ͞͏
  34. X © DMM.com EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ /FX 

     $PNQPOFOUΛ༻ҙ͢Δ 6QEBUFE   "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ 6QEBUFE   ֤ॴͰJOKFDU 6QEBUFE EBHHFSBOESPJEΛ࢖࣮ͬͨ૷ͷྲྀΕ
  35. X © DMM.com EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ

     "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU EBHHFSBOESPJEΛ࢖࣮ͬͨ૷ͷྲྀΕ
  36. X © DMM.com EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ

     "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU EBHHFSBOESPJEΛ࢖࣮ͬͨ૷ͷྲྀΕ
  37. X © DMM.com ᶃ.PEVMFΛ༻ҙ͢Δ @Module abstract class AppModule { @Binds

    abstract fun messageProvider(impl: MessageProviderImpl): MessageProvider }
  38. X © DMM.com EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ

     "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU EBHHFSBOESPJEΛ࢖࣮ͬͨ૷ͷྲྀΕ
  39. X © DMM.com EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ

     "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU EBHHFSBOESPJEΛ࢖࣮ͬͨ૷ͷྲྀΕ
  40. X © DMM.com EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ

     "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU EBHHFSBOESPJEΛ࢖࣮ͬͨ૷ͷྲྀΕ
  41. X © DMM.com EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ

     "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU EBHHFSBOESPJEΛ࢖࣮ͬͨ૷ͷྲྀΕ
  42. X © DMM.com ᶅ$PNQPOFOUΛ༻ҙ͢Δ @Singleton @Component( modules = [ AndroidInjectionModule::class,

    AppModule::class, MainFragmentModule::class ] ) interface AppComponent : AndroidInjector<Application>
  43. X © DMM.com ᶅ$PNQPOFOUΛ༻ҙ͢Δ @Singleton @Component( modules = [ AndroidInjectionModule::class,

    AppModule::class, MainFragmentModule::class ] ) interface AppComponent : AndroidInjector<Application>
  44. X © DMM.com ᶅ$PNQPOFOUΛ༻ҙ͢Δ @Singleton @Component( modules = [ AndroidInjectionModule::class,

    AppModule::class, MainFragmentModule::class ] ) interface AppComponent : AndroidInjector<Application>
  45. X © DMM.com ᶅ$PNQPOFOUΛ༻ҙ͢Δ @Singleton @Component( modules = [ AndroidInjectionModule::class,

    AppModule::class, MainFragmentModule::class ] ) interface AppComponent : AndroidInjector<Application>
  46. X © DMM.com ᶅ$PNQPOFOUΛ༻ҙ͢Δ @Singleton @Component( modules = [ AndroidInjectionModule::class,

    AppModule::class, MainFragmentModule::class ] ) interface AppComponent : AndroidInjector<Application> Ͳ͜ʹ*OKFDU͢Δ͔͸ɺ'SBHNFOU.PEVMF΍"OESPJE*OKFDUPSʹ ୅ସ͞ΕͯΔͷͰॻ͔ͳ͍
  47. X © DMM.com EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ

     "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU EBHHFSBOESPJEΛ࢖࣮ͬͨ૷ͷྲྀΕ
  48. X © DMM.com EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ

     "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU EBHHFSBOESPJEΛ࢖࣮ͬͨ૷ͷྲྀΕ
  49. X © DMM.com ᶆ"QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ class Application : android.app.Application(), HasSupportFragmentInjector {

    @Inject lateinit var dispatchingFragmentInjector: DispatchingAndroidInjector<Fragment> override fun supportFragmentInjector(): AndroidInjector<Fragment> = dispatchingFragmentInjector override fun onCreate() { super.onCreate() DaggerAppComponent.create().inject(this) } }
  50. X © DMM.com ᶆ"QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ class Application : android.app.Application(), HasSupportFragmentInjector {

    @Inject lateinit var dispatchingFragmentInjector: DispatchingAndroidInjector<Fragment> override fun supportFragmentInjector(): AndroidInjector<Fragment> = dispatchingFragmentInjector override fun onCreate() { super.onCreate() DaggerAppComponent.create().inject(this) } }
  51. X © DMM.com EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ

     "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU EBHHFSBOESPJEΛ࢖࣮ͬͨ૷ͷྲྀΕ
  52. X © DMM.com EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ

     "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU EBHHFSBOESPJEΛ࢖࣮ͬͨ૷ͷྲྀΕ
  53. X © DMM.com ᶇ֤ॴͰ*OKFDU class MainFragment : Fragment() { @Inject

    lateinit var messageProvider: MessageProvider override fun onAttach(context: Context?) { AndroidSupportInjection.inject(this) super.onAttach(context) } … }
  54. X © DMM.com ᶇ֤ॴͰ*OKFDU class MainFragment : Fragment() { @Inject

    lateinit var messageProvider: MessageProvider override fun onAttach(context: Context?) { AndroidSupportInjection.inject(this) super.onAttach(context) } … }
  55. X © DMM.com EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ

     "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU EBHHFSBOESPJEΛ࢖࣮ͬͨ૷ͷྲྀΕ
  56. X © DMM.com ࢓૊Έ $PNQPOFOU w .BJO'SBHNFOUʹJOKFDU͢Δํ๏ w 4VC'SBHNFOUʹJOKFDU͢Δํ๏ w

    .BJO"DUJWJUZʹJOKFDU͢Δํ๏ w 4VC"DUJWJUZʹJOKFDU͢Δํ๏ w .BJO'SBHNFOUʹJOKFDU͢Δํ๏ w 4VC'SBHNFOUʹJOKFDU͢Δํ๏ w .BJO"DUJWJUZʹJOKFDU͢Δํ๏ w 4VC"DUJWJUZʹJOKFDU͢Δํ๏
  57. X © DMM.com ࢓૊Έ $PNQPOFOU w .BJO'SBHNFOUʹJOKFDU͢Δํ๏ w 4VC'SBHNFOUʹJOKFDU͢Δํ๏ w

    .BJO"DUJWJUZʹJOKFDU͢Δํ๏ w 4VC"DUJWJUZʹJOKFDU͢Δํ๏ w .BJO'SBHNFOUʹJOKFDU͢Δํ๏ w 4VC'SBHNFOUʹJOKFDU͢Δํ๏
  58. X © DMM.com ࢓૊Έ $PNQPOFOU w .BJO'SBHNFOUʹJOKFDU͢Δํ๏ w 4VC'SBHNFOUʹJOKFDU͢Δํ๏ w

    .BJO"DUJWJUZʹJOKFDU͢Δํ๏ w 4VC"DUJWJUZʹJOKFDU͢Δํ๏ w .BJO'SBHNFOUʹJOKFDU͢Δํ๏ w 4VC'SBHNFOUʹJOKFDU͢Δํ๏ class Application : HasSupportFragmentInjector { @Inject lateinit var dispatchingFragmentInjector … DaggerAppComponent.create().inject(this) … }
  59. X © DMM.com class Application : HasSupportFragmentInjector { @Inject lateinit

    var dispatchingFragmentInjector } ࢓૊Έ $PNQPOFOU w .BJO'SBHNFOUʹJOKFDU͢Δํ๏ w 4VC'SBHNFOUʹJOKFDU͢Δํ๏ w .BJO"DUJWJUZʹJOKFDU͢Δํ๏ w 4VC"DUJWJUZʹJOKFDU͢Δํ๏ w .BJO'SBHNFOUʹJOKFDU͢Δํ๏ w 4VC'SBHNFOUʹJOKFDU͢Δํ๏
  60. X © DMM.com ࢓૊Έ class Application : HasSupportFragmentInjector { @Inject

    lateinit var dispatchingFragmentInjector } w .BJO'SBHNFOUʹJOKFDU͢Δํ๏ w 4VC'SBHNFOUʹJOKFDU͢Δํ๏
  61. X © DMM.com ࢓૊Έ class Application : HasSupportFragmentInjector { @Inject

    lateinit var dispatchingFragmentInjector } w .BJO'SBHNFOUʹJOKFDU͢Δํ๏ w 4VC'SBHNFOUʹJOKFDU͢Δํ๏ class MainFragment : Fragment() { ɹɹ… AndroidSupportInjection.inject(this) … }
  62. X © DMM.com ࢓૊Έ class Application : HasSupportFragmentInjector { @Inject

    lateinit var dispatchingFragmentInjector } w .BJO'SBHNFOUʹJOKFDU͢Δํ๏ w 4VC'SBHNFOUʹJOKFDU͢Δํ๏ class MainFragment : Fragment() { ɹɹ… AndroidSupportInjection.inject(this) … } class MainActivity : AppCompatActivity() { … } HasSupportFragmentInjectorΛ࣮૷ͯ͠Δ͔ʁ
  63. X © DMM.com ࢓૊Έ class Application : HasSupportFragmentInjector { @Inject

    lateinit var dispatchingFragmentInjector } w .BJO'SBHNFOUʹJOKFDU͢Δํ๏ w 4VC'SBHNFOUʹJOKFDU͢Δํ๏ class MainFragment : Fragment() { ɹɹ… AndroidSupportInjection.inject(this) … } class MainActivity : AppCompatActivity() { … } HasSupportFragmentInjectorΛ࣮૷ͯ͠Δ͔ʁ
  64. X © DMM.com ࢓૊Έ class Application : HasSupportFragmentInjector { @Inject

    lateinit var dispatchingFragmentInjector } w .BJO'SBHNFOUʹJOKFDU͢Δํ๏ w 4VC'SBHNFOUʹJOKFDU͢Δํ๏ class MainFragment : Fragment() { ɹɹ… AndroidSupportInjection.inject(this) … } class MainActivity : AppCompatActivity() { … } HasSupportFragmentInjectorΛ࣮૷ͯ͠Δ͔ʁ
  65. X © DMM.com ࢓૊Έ class MainFragment : Fragment() { ɹɹ…

    AndroidSupportInjection.inject(this) … } class MainActivity : AppCompatActivity() { … } HasSupportFragmentInjectorΛ࣮૷ͯ͠Δ͔ʁ class Application : HasSupportFragmentInjector { @Inject lateinit var dispatchingFragmentInjector } w .BJO'SBHNFOUʹJOKFDU͢Δํ๏ w 4VC'SBHNFOUʹJOKFDU͢Δํ๏
  66. X © DMM.com class Application : HasSupportFragmentInjector { @Inject lateinit

    var dispatchingFragmentInjector } w .BJO'SBHNFOUʹJOKFDU͢Δํ๏ w 4VC'SBHNFOUʹJOKFDU͢Δํ๏ ࢓૊Έ class MainFragment : Fragment() { ɹɹ… AndroidSupportInjection.inject(this) … } class MainActivity : AppCompatActivity() { … } HasSupportFragmentInjectorΛ࣮૷ͯ͠Δ͔ʁ
  67. X © DMM.com class Application : HasSupportFragmentInjector { @Inject lateinit

    var dispatchingFragmentInjector } w .BJO'SBHNFOUʹJOKFDU͢Δํ๏ w 4VC'SBHNFOUʹJOKFDU͢Δํ๏ ࢓૊Έ class MainFragment : Fragment() { ɹɹ… AndroidSupportInjection.inject(this) … } class MainActivity : AppCompatActivity() { … } HasSupportFragmentInjectorΛ࣮૷ͯ͠Δ͔ʁ
  68. X © DMM.com EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ

     "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ
  69. X © DMM.com EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ

     "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ
  70. X © DMM.com ᶆ"QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ class Application : HasSupportFragmentInjector, … {

    @Inject lateinit var dispatchingFragmentInjector DispatchingAndroidInjector<Fragment>() override fun supportFragmentInjector(): AndroidInjector<Fragment> = dispatchingFragmentInjector override fun onCreate() { super.onCreate() DaggerCounterComponent.create().inject(this) DaggerGreetingComponent.create().inject(this) … } }
  71. X © DMM.com ᶆ"QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ class Application : HasSupportFragmentInjector, … {

    @Inject lateinit var dispatchingFragmentInjector DispatchingAndroidInjector<Fragment>() override fun supportFragmentInjector(): AndroidInjector<Fragment> = dispatchingFragmentInjector override fun onCreate() { super.onCreate() DaggerCounterComponent.create().inject(this) DaggerGreetingComponent.create().inject(this) … } }
  72. X © DMM.com EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ

     "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ
  73. X © DMM.com EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  $PNQPOFOUΛ༻ҙ͢Δ

     "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ
  74. X © DMM.com ᶇ֤ॴͰ*OKFDU class CounterFragment : Fragment() { @Inject

    lateinit var counter: Counter override fun onAttach(context: Context?) { AndroidSupportInjection.inject(this) super.onAttach(context) } … } class GreetingFragment : Fragment() { @Inject lateinit var greeter: Greeter override fun onAttach(context: Context?) { AndroidSupportInjection.inject(this) super.onAttach(context) } … }
  75. X © DMM.com ᶇ֤ॴͰ*OKFDU class CounterFragment : Fragment() { @Inject

    lateinit var counter: Counter override fun onAttach(context: Context?) { AndroidSupportInjection.inject(this) super.onAttach(context) } … } class GreetingFragment : Fragment() { @Inject lateinit var greeter: Greeter override fun onAttach(context: Context?) { AndroidSupportInjection.inject(this) super.onAttach(context) } … }
  76. X © DMM.com $PNQPOFOUΛॳظԽͷॱ൪ΛೖΕସ͑ͯΈΔͱ class Application : HasSupportFragmentInjector, … {

    @Inject lateinit var dispatchingFragmentInjector DispatchingAndroidInjector<Fragment>() override fun supportFragmentInjector(): AndroidInjector<Fragment> = dispatchingFragmentInjector override fun onCreate() { super.onCreate() DaggerCounterComponent.create().inject(this) DaggerGreetingComponent.create().inject(this) … } }
  77. X © DMM.com $PNQPOFOUΛॳظԽͷॱ൪ΛೖΕସ͑ͯΈΔͱ class Application : HasSupportFragmentInjector, … {

    @Inject lateinit var dispatchingFragmentInjector DispatchingAndroidInjector<Fragment>() override fun supportFragmentInjector(): AndroidInjector<Fragment> = dispatchingFragmentInjector override fun onCreate() { super.onCreate() DaggerGreetingComponent.create().inject(this) DaggerCounterComponent.create().inject(this) … } }
  78. X © DMM.com $SBTI͢Δཧ༝ class Application : HasSupportFragmentInjector, … {

    @Inject lateinit var dispatchingFragmentInjector DispatchingAndroidInjector<Fragment>() … override fun onCreate() { super.onCreate() DaggerCounterComponent.create().inject(this) DaggerGreetingComponent.create().inject(this) … } }
  79. X © DMM.com $SBTI͢Δཧ༝ class Application : HasSupportFragmentInjector, … {

    @Inject lateinit var dispatchingFragmentInjector DispatchingAndroidInjector<Fragment>() … override fun onCreate() { super.onCreate() DaggerCounterComponent.create().inject(this) DaggerGreetingComponent.create().inject(this) … } }
  80. X © DMM.com $SBTI͢Δཧ༝ class Application : HasSupportFragmentInjector, … {

    @Inject lateinit var dispatchingFragmentInjector DispatchingAndroidInjector<Fragment>() … override fun onCreate() { super.onCreate() DaggerCounterComponent.create().inject(this) DaggerGreetingComponent.create().inject(this) … } }
  81. X © DMM.com class Application : HasSupportFragmentInjector { @Inject lateinit

    var dispatchingFragmentInjector } w (SFFUJOH'SBHNFOUʹJOKFDU͢Δํ๏ ࢓૊Έ class CounterFragment : Fragment() { ɹɹ… AndroidSupportInjection.inject(this) … }
  82. X © DMM.com class Application : HasSupportFragmentInjector { @Inject lateinit

    var dispatchingFragmentInjector } w (SFFUJOH'SBHNFOUʹJOKFDU͢Δํ๏ ࢓૊Έ class CounterFragment : Fragment() { ɹɹ… AndroidSupportInjection.inject(this) … }
  83. X © DMM.com class ModuleInjector<T> : AndroidInjector<T> { private val

    injectors = mutableListOf<HasDispatchingInjector<T>>() fun addInjector(injector: HasDispatchingInjector<T>) { injectors.add(injector) } fun removeInjector(injector: HasDispatchingInjector<T>) { injectors.remove(injector) } … override fun inject(instance: T) { val wasInjected = injectors.any { it.dispatchingAndroidInjector().maybeInject(instance) } if (!wasInjected) { throw IllegalArgumentException("injection failed") } } } .PEVMF*OKFDUPS
  84. X © DMM.com class ModuleInjector<T> : AndroidInjector<T> { private val

    injectors = mutableListOf<HasDispatchingInjector<T>>() fun addInjector(injector: HasDispatchingInjector<T>) { injectors.add(injector) } fun removeInjector(injector: HasDispatchingInjector<T>) { injectors.remove(injector) } … override fun inject(instance: T) { val wasInjected = injectors.any { it.dispatchingAndroidInjector().maybeInject(instance) } if (!wasInjected) { throw IllegalArgumentException("injection failed") } } } .PEVMF*OKFDUPS
  85. X © DMM.com class ModuleInjector<T> : AndroidInjector<T> { private val

    injectors = mutableListOf<HasDispatchingInjector<T>>() fun addInjector(injector: HasDispatchingInjector<T>) { injectors.add(injector) } fun removeInjector(injector: HasDispatchingInjector<T>) { injectors.remove(injector) } … override fun inject(instance: T) { val wasInjected = injectors.any { it.dispatchingAndroidInjector().maybeInject(instance) } if (!wasInjected) { throw IllegalArgumentException("injection failed") } } } .PEVMF*OKFDUPS
  86. X © DMM.com class ModuleInjector<T> : AndroidInjector<T> { private val

    injectors = mutableListOf<HasDispatchingInjector<T>>() fun addInjector(injector: HasDispatchingInjector<T>) { injectors.add(injector) } fun removeInjector(injector: HasDispatchingInjector<T>) { injectors.remove(injector) } … override fun inject(instance: T) { val wasInjected = injectors.any { it.dispatchingAndroidInjector().maybeInject(instance) } if (!wasInjected) { throw IllegalArgumentException("injection failed") } } } .PEVMF*OKFDUPS interface HasDispatchingInjector<T> { fun dispatchingAndroidInjector(): DispatchingAndroidInjector<T> }
  87. X © DMM.com class ModuleInjector<T> : AndroidInjector<T> { private val

    injectors = mutableListOf<HasDispatchingInjector<T>>() fun addInjector(injector: HasDispatchingInjector<T>) { injectors.add(injector) } fun removeInjector(injector: HasDispatchingInjector<T>) { injectors.remove(injector) } … override fun inject(instance: T) { val wasInjected = injectors.any { it.dispatchingAndroidInjector().maybeInject(instance) } if (!wasInjected) { throw IllegalArgumentException("injection failed") } } } .PEVMF*OKFDUPS
  88. X © DMM.com class ModuleInjector<T> : AndroidInjector<T> { private val

    injectors = mutableListOf<HasDispatchingInjector<T>>() fun addInjector(injector: HasDispatchingInjector<T>) { injectors.add(injector) } fun removeInjector(injector: HasDispatchingInjector<T>) { injectors.remove(injector) } … override fun inject(instance: T) { val wasInjected = injectors.any { it.dispatchingAndroidInjector().maybeInject(instance) } if (!wasInjected) { throw IllegalArgumentException("injection failed") } } } .PEVMF*OKFDUPS
  89. X © DMM.com EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ NVMUJNPEVMF  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ 

    *OKFDUPSΛ༻ҙ͢Δ /FX   $PNQPOFOUΛ༻ҙ͢Δ 6QEBUFE   "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ 6QEBUFE   ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ
  90. X © DMM.com  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  *OKFDUPSΛ༻ҙ͢Δ 

    $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ NVMUJNPEVMF
  91. X © DMM.com  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  *OKFDUPSΛ༻ҙ͢Δ 

    $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ NVMUJNPEVMF
  92. X © DMM.com ᶃ.PEVMFΛ༻ҙ͢Δ @Module internal abstract class CounterModule {

    @Singleton @Binds abstract fun provideCounter(impl: CounterImpl): Counter } @Module internal abstract class GreetingModule { @Binds abstract fun provideGreeter(impl: GreeterImpl): Greeter }
  93. X © DMM.com  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  *OKFDUPSΛ༻ҙ͢Δ 

    $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ NVMUJNPEVMF
  94. X © DMM.com  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  *OKFDUPSΛ༻ҙ͢Δ 

    $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ NVMUJNPEVMF
  95. X © DMM.com ᶄ'SBHNFOU.PEVMFΛ༻ҙ͢Δ @Module internal abstract class GreetingFragmentModule {

    @ContributesAndroidInjector abstract fun greetingFragment(): GreetingFragment } @Module internal abstract class CounterFragmentModule { @ContributesAndroidInjector abstract fun counterFragment(): CounterFragment }
  96. X © DMM.com  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  *OKFDUPSΛ༻ҙ͢Δ 

    $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ NVMUJNPEVMF
  97. X © DMM.com  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  *OKFDUPSΛ༻ҙ͢Δ 

    $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ NVMUJNPEVMF
  98. X © DMM.com ᶅ*OKFDUPSΛ༻ҙ͢Δ class CounterInjector : HasDispatchingInjector<Fragment> { @Inject

    lateinit var supportFragmentInjector: DispatchingAndroidInjector<Fragment> override fun dispatchingAndroidInjector(): DispatchingAndroidInjector<Fragment> = supportFragmentInjector } class GreetingInjector : HasDispatchingInjector<Fragment> { @Inject lateinit var supportFragmentInjector: DispatchingAndroidInjector<Fragment> override fun dispatchingAndroidInjector(): DispatchingAndroidInjector<Fragment> = supportFragmentInjector }
  99. X © DMM.com  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  *OKFDUPSΛ༻ҙ͢Δ 

    $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ NVMUJNPEVMF
  100. X © DMM.com  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  *OKFDUPSΛ༻ҙ͢Δ 

    $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ NVMUJNPEVMF
  101. X © DMM.com ᶆ$PNQPOFOUΛ༻ҙ͢Δ @Singleton @Component( modules = [ AndroidInjectionModule::class,

    CounterModule::class, CounterFragmentModule::class]) interface CounterComponent : AndroidInjector<CounterInjector> @Singleton @Component( modules = [ AndroidInjectionModule::class, GreetingModule::class, GreetingFragmentModule::class]) interface GreetingComponent : AndroidInjector<GreetingInjector>
  102. X © DMM.com  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  *OKFDUPSΛ༻ҙ͢Δ 

    $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ NVMUJNPEVMF
  103. X © DMM.com  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  *OKFDUPSΛ༻ҙ͢Δ 

    $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ NVMUJNPEVMF
  104. X © DMM.com ᶇ"QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ͢Δ class Application : android.app.Application(), HasSupportFragmentInjector {

    private val fragmentInjector = ModuleInjector<Fragment>() override fun supportFragmentInjector(): AndroidInjector<Fragment> = fragmentInjector override fun onCreate() { super.onCreate() val counterInjector = CounterInjector() DaggerCounterComponent.create().inject(counterInjector) val greetingInjector = GreetingInjector() DaggerGreetingComponent.create().inject(greetingInjector) fragmentInjector.addInjector(counterInjector) fragmentInjector.addInjector(greetingInjector) } }
  105. X © DMM.com class Application : android.app.Application(), HasSupportFragmentInjector { private

    val fragmentInjector = ModuleInjector<Fragment>() override fun supportFragmentInjector(): AndroidInjector<Fragment> = fragmentInjector override fun onCreate() { super.onCreate() val counterInjector = CounterInjector() DaggerCounterComponent.create().inject(counterInjector) val greetingInjector = GreetingInjector() DaggerGreetingComponent.create().inject(greetingInjector) fragmentInjector.addInjector(counterInjector) fragmentInjector.addInjector(greetingInjector) } } ᶇ"QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ͢Δ
  106. X © DMM.com class Application : android.app.Application(), HasSupportFragmentInjector { private

    val fragmentInjector = ModuleInjector<Fragment>() override fun supportFragmentInjector(): AndroidInjector<Fragment> = fragmentInjector override fun onCreate() { super.onCreate() val counterInjector = CounterInjector() DaggerCounterComponent.create().inject(counterInjector) val greetingInjector = GreetingInjector() DaggerGreetingComponent.create().inject(greetingInjector) fragmentInjector.addInjector(counterInjector) fragmentInjector.addInjector(greetingInjector) } } ᶇ"QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ͢Δ
  107. X © DMM.com class Application : android.app.Application(), HasSupportFragmentInjector { private

    val fragmentInjector = ModuleInjector<Fragment>() override fun supportFragmentInjector(): AndroidInjector<Fragment> = fragmentInjector override fun onCreate() { super.onCreate() val counterInjector = CounterInjector() DaggerCounterComponent.create().inject(counterInjector) val greetingInjector = GreetingInjector() DaggerGreetingComponent.create().inject(greetingInjector) fragmentInjector.addInjector(counterInjector) fragmentInjector.addInjector(greetingInjector) } } ᶇ"QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ͢Δ
  108. X © DMM.com class Application : android.app.Application(), HasSupportFragmentInjector { private

    val fragmentInjector = ModuleInjector<Fragment>() override fun supportFragmentInjector(): AndroidInjector<Fragment> = fragmentInjector override fun onCreate() { super.onCreate() val counterInjector = CounterInjector() DaggerCounterComponent.create().inject(counterInjector) val greetingInjector = GreetingInjector() DaggerGreetingComponent.create().inject(greetingInjector) fragmentInjector.addInjector(counterInjector) fragmentInjector.addInjector(greetingInjector) } } ᶇ"QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ͢Δ
  109. X © DMM.com  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  *OKFDUPSΛ༻ҙ͢Δ 

    $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ NVMUJNPEVMF
  110. X © DMM.com  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  *OKFDUPSΛ༻ҙ͢Δ 

    $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ NVMUJNPEVMF
  111. X © DMM.com ᶈ֤ॴͰ*OKFDU class CounterFragment : Fragment() { @Inject

    lateinit var counter: Counter override fun onAttach(context: Context?) { AndroidSupportInjection.inject(this) super.onAttach(context) } … } class GreetingFragment : Fragment() { @Inject lateinit var greeter: Greeter override fun onAttach(context: Context?) { AndroidSupportInjection.inject(this) super.onAttach(context) } … }
  112. X © DMM.com ྲྀΕ class Application : …, HasSupportFragmentInjector {

    private val moduleFragmentInjector … } class CounterFragment : Fragment() { ɹɹ… AndroidSupportInjection.inject(this) … }
  113. X © DMM.com ྲྀΕ class Application : …, HasSupportFragmentInjector {

    private val moduleFragmentInjector … } class CounterFragment : Fragment() { ɹɹ… AndroidSupportInjection.inject(this) … } class MainActivity : AppCompatActivity() { … } HasSupportFragmentInjectorΛ࣮૷ͯ͠Δ͔ʁ
  114. X © DMM.com ྲྀΕ class Application : …, HasSupportFragmentInjector {

    private val moduleFragmentInjector … } class CounterFragment : Fragment() { ɹɹ… AndroidSupportInjection.inject(this) … } class MainActivity : AppCompatActivity() { … } HasSupportFragmentInjectorΛ࣮૷ͯ͠Δ͔ʁ
  115. X © DMM.com ྲྀΕ class Application : …, HasSupportFragmentInjector {

    private val moduleFragmentInjector … } class CounterFragment : Fragment() { ɹɹ… AndroidSupportInjection.inject(this) … } class MainActivity : AppCompatActivity() { … } HasSupportFragmentInjectorΛ࣮૷ͯ͠Δ͔ʁ
  116. X © DMM.com ྲྀΕ class Application : …, HasSupportFragmentInjector {

    private val moduleFragmentInjector … } class CounterFragment : Fragment() { ɹɹ… AndroidSupportInjection.inject(this) … } class MainActivity : AppCompatActivity() { … } HasSupportFragmentInjectorΛ࣮૷ͯ͠Δ͔ʁ
  117. X © DMM.com  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  *OKFDUPSΛ༻ҙ͢Δ 

    $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ NVMUJNPEVMF
  118. X © DMM.com  .PEVMFΛ༻ҙ͢Δ  'SBHNFOU.PEVMFΛ༻ҙ͢Δ  *OKFDUPSΛ༻ҙ͢Δ 

    $PNQPOFOUΛ༻ҙ͢Δ  "QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ  ֤ॴͰJOKFDU ࠓ·Ͱͱಉ͡खॱͰɺEBHHFSBOESPJEΛ࢖࣮ͬͯ૷ͯ͠ΈΔ EBHHFSBOESPJEͰ%FQFOEFODZ*OKFDUJPO͢Δ NVMUJNPEVMF
  119. X © DMM.com ᶇ"QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ͢Δ class Application : android.app.Application(), HasSupportFragmentInjector {

    private val fragmentInjector = ModuleInjector<Fragment>() override fun supportFragmentInjector(): AndroidInjector<Fragment> = fragmentInjector override fun onCreate() { super.onCreate() val counterInjector = CounterInjector() DaggerCounterComponent.create().inject(counterInjector) val greetingInjector = GreetingInjector() DaggerGreetingComponent.create().inject(greetingInjector) fragmentInjector.addInjector(counterInjector) fragmentInjector.addInjector(greetingInjector) } }
  120. X © DMM.com ᶇ"QQMJDBUJPOPO$SFBUFͰ$PNQPOFOUΛॳظԽ͢Δ class Application : android.app.Application(), HasSupportFragmentInjector {

    private val fragmentInjector = ModuleInjector<Fragment>() override fun supportFragmentInjector(): AndroidInjector<Fragment> = fragmentInjector override fun onCreate() { super.onCreate() val counterInjector = CounterInjector() DaggerCounterComponent.create().inject(counterInjector) val greetingInjector = GreetingInjector() DaggerGreetingComponent.create().inject(greetingInjector) fragmentInjector.addInjector(counterInjector) fragmentInjector.addInjector(greetingInjector) } }
  121. X © DMM.com ͨͱ͑͹ɺ͜Μͳؔ਺Λ࡞Δ object GreetingInjection { fun inject(fragment: Fragment)

    { val application = fragment.requireActivity().application as Application if (application.moduleFragmentInjector.injectors().all { it !is GreetingInjector }) { val injector = GreetingInjector() val component = DaggerGreetingComponent.builder() .counter(application.counterComponent().counter()) .build() component.inject(injector) application.addFragmentInjector(injector) } AndroidSupportInjection.inject(fragment) } }
  122. X © DMM.com ͨͱ͑͹ɺ͜Μͳؔ਺Λ࡞Δ object GreetingInjection { fun inject(fragment: Fragment)

    { val application = fragment.requireActivity().application as Application if (application.moduleFragmentInjector.injectors().all { it !is GreetingInjector }) { val injector = GreetingInjector() val component = DaggerGreetingComponent.builder() .counter(application.counterComponent().counter()) .build() component.inject(injector) application.addFragmentInjector(injector) } AndroidSupportInjection.inject(fragment) } }
  123. X © DMM.com ͨͱ͑͹ɺ͜Μͳؔ਺Λ࡞Δ object GreetingInjection { fun inject(fragment: Fragment)

    { val application = fragment.requireActivity().application as Application if (application.moduleFragmentInjector.injectors().all { it !is GreetingInjector }) { val injector = GreetingInjector() val component = DaggerGreetingComponent.builder() .counter(application.counterComponent().counter()) .build() component.inject(injector) application.addFragmentInjector(injector) } AndroidSupportInjection.inject(fragment) } }
  124. X © DMM.com ͨͱ͑͹ɺ͜Μͳؔ਺Λ࡞Δ object GreetingInjection { fun inject(fragment: Fragment)

    { val application = fragment.requireActivity().application as Application if (application.moduleFragmentInjector.injectors().all { it !is GreetingInjector }) { val injector = GreetingInjector() val component = DaggerGreetingComponent.builder() .counter(application.counterComponent().counter()) .build() component.inject(injector) application.addFragmentInjector(injector) } AndroidSupportInjection.inject(fragment) } }
  125. X © DMM.com ͨͱ͑͹ɺ͜Μͳؔ਺Λ࡞Δ object GreetingInjection { fun inject(fragment: Fragment)

    { val application = fragment.requireActivity().application as Application if (application.moduleFragmentInjector.injectors().all { it !is GreetingInjector }) { val injector = GreetingInjector() val component = DaggerGreetingComponent.builder() .counter(application.counterComponent().counter()) .build() component.inject(injector) application.addFragmentInjector(injector) } AndroidSupportInjection.inject(fragment) } } "DUJWJUZʹNPEVMF*OKFDUPS͕͋Δ৔߹Λߟྀͯ͠ɺ "OESPJE4VQQPSU*OKFDUJPOͱಉ͡Α͏ͳ࣮૷ʹ͢Δͷ΋Մ
  126. X © DMM.com ͨͱ͑͹ɺ͜Μͳؔ਺Λ࡞Δ object GreetingInjection { fun inject(fragment: Fragment)

    { val application = fragment.requireActivity().application as Application if (application.moduleFragmentInjector.injectors().all { it !is GreetingInjector }) { val injector = GreetingInjector() val component = DaggerGreetingComponent.builder() .counter(application.counterComponent().counter()) .build() component.inject(injector) application.addFragmentInjector(injector) } AndroidSupportInjection.inject(fragment) } } ˞"DUJWJUZͰ*OKFDUPSΛ࣋ͭͱ͖͸͜ͷํ๏ͩͱ͏·͍͔͘ͳ͍ͷͰ ৚݅෼ذ͢Δඞཁ͕͋Γ·͢ʂ
  127. X © DMM.com ͦ͏͢ΔͱɺJOKFDUઌ͸͜ͷมߋ͚ͩͰࠓ·Ͱ௨Γಈ͘Α͏ʹͳΔ class GreetingFragment : Fragment() { @Inject

    lateinit var greeter: Greeter override fun onAttach(context: Context?) { GreetingInjection.inject(this) super.onAttach(context) } … } class GreetingFragment : Fragment() { @Inject lateinit var greeter: Greeter override fun onAttach(context: Context?) { AndroidSupportInjection.inject(this) super.onAttach(context) } … }
  128. X © DMM.com ͦ͏͢ΔͱɺJOKFDUઌ͸͜ͷมߋ͚ͩͰࠓ·Ͱ௨Γಈ͘Α͏ʹͳΔ class GreetingFragment : Fragment() { @Inject

    lateinit var greeter: Greeter override fun onAttach(context: Context?) { GreetingInjection.inject(this) super.onAttach(context) } … } class GreetingFragment : Fragment() { @Inject lateinit var greeter: Greeter override fun onAttach(context: Context?) { AndroidSupportInjection.inject(this) super.onAttach(context) } … }
  129. X © DMM.com ࿩ͨ͜͠ͱৼΓฦΓ w %FQFOEFODZ*OKFDUJPOͱ͸ w %BHHFSͷ࢖͍ํͷҰྫɺखॱɺ࢓૊Έ w %BHHFS"OESPJEαϙʔτͷ࢖͍ํͷҰྫɺखॱɺ࢓૊Έ

    w ͦͷ··Ͱ͸ϚϧνϞδϡʔϧରԠͰ͖ͳ͍έʔε͕͋Δ͜ͱ w .PEVMF*OKFDUPSΛಋೖ͢Δ͜ͱͰରԠͰ͖Δ͜ͱ w %ZOBNJD'FBUVSF.PEVMF࣌͸ɺద੾ͳλΠϛϯάͰ*OKFDUPSΛ .PEVMF*OKFDUPSʹࠩ͠ࠐΜͰ͋͛Δඞཁ͕͋Δ͜ͱ