Replace with dagger.android

Replace with dagger.android

107c7e4625bacbe78ee562359d89b318?s=128

Yuji Koketsu

October 25, 2017
Tweet

Transcript

  1. REPLACE WITH DAGGER.ANDROID

  2. HELLO ABOUT ME Yuji Koketsu @kettsun0123 ▸ Android Developer ▸

    CyberAgent, Inc ▸ ݩ AbemaTV >> ৽نϝσΟΞ
  3. REPLACE WITH DAGGER.ANDROID What is dagger.android?

  4. WHAT IS DAGGER.ANDROID? DAGGER.ANDROID IS… ▸ Dagger2ͷAndroid֦ுDIϥΠϒϥϦ ▸ v2.10 ▸

    core͕ϦϦʔε ▸ v2.11 ▸ @ContributesAndroidInjector௥Ճ ▸ DaggerApplication௥Ճ ▸ v2.12 ▸ ࠓ࠷৽൛ʂ https://github.com/google/dagger/releases
  5. WHAT IS DAGGER.ANDROID? class MainActivity : AppCompatActivity() { lateinit var

    instance: Hoge override fun onCreate(state: Bundle?) { super.onCreate(state) ... } ... }
  6. WHAT IS DAGGER.ANDROID? class MainActivity : AppCompatActivity() { @Inject lateinit

    var instance: Hoge override fun onCreate(state: Bundle?) { AndroidInjection.inject(this) super.onCreate(state) ... } ... }
  7. REPLACE WITH DAGGER.ANDROID dagger.androidͰஔ͖׵͑Δ

  8. REPLACE WITH DAGGER.ANDROID App MainActivity AppModule class Hoge

  9. REPLACE WITH DAGGER.ANDROID dependencies { // dagger.android implementation "com.google.dagger:dagger-android:2.12" implementation

    "com.google.dagger:dagger-android-support:2.12" kapt "com.google.dagger:dagger-android-processor:2.12" }
  10. REPLACE WITH DAGGER.ANDROID AppComponent.kt @Singleton @Component(modules = arrayOf( AppModule::class ))

    interface AppComponent { fun inject(app: App) fun activityComponent(): ActivityComponent }
  11. REPLACE WITH DAGGER.ANDROID AppComponent.kt @Singleton @Component(modules = arrayOf( AndroidInjectionModule::class, AppModule::class

    )) interface AppComponent : AndroidInjector<App> { @Component.Builder interface Builder { fun application(application: App): Builder fun build(): AppComponent } override fun inject(app: App) }
  12. REPLACE WITH DAGGER.ANDROID ActivityComponent.kt @Subcomponent(modules = arrayOf( ... )) interface

    ActivityComponent { fun inject(activity: MainActivity) }
  13. REPLACE WITH DAGGER.ANDROID ActivityComponent.kt @Subcomponent(modules = arrayOf( ... )) interface

    ActivityComponent : AndroidInjector<MainActivity> { @Subcomponent.Builder abstract class Builder : AndroidInjector.Builder<MainActivity>() }
  14. REPLACE WITH DAGGER.ANDROID ActivityModule.kt @Module(subcomponents = ActivityComponent::class) internal abstract class

    ActivityModule { @Binds @IntoMap @ActivityKey(MainActivity::class) internal abstract fun bindMainInjector( builder: ActivityComponent.Builder ): AndroidInjector.Factory<out Activity> } SubcomponentΛModuleͰbind͢Δ
  15. REPLACE WITH DAGGER.ANDROID AppComponent.kt @Singleton @Component(modules = arrayOf( AndroidInjectionModule::class, AppModule::class

    )) interface AppComponent : AndroidInjector<App> { @Component.Builder interface Builder { fun application(application: App): Builder fun build(): AppComponent } override fun inject(app: App) }
  16. REPLACE WITH DAGGER.ANDROID AppComponent.kt @Singleton @Component(modules = arrayOf( AndroidInjectionModule::class, ActivityModule::class,

    AppModule::class )) interface AppComponent : AndroidInjector<App> { @Component.Builder interface Builder { fun application(application: App): Builder fun build(): AppComponent } override fun inject(app: App) }
  17. REPLACE WITH DAGGER.ANDROID class App : Application() { val appComponent

    : AppComponent by lazy { DaggerAppComponent.builder() .appModule(AppModule(this)) .build() } override fun onCreate() { super.onCreate() ... } ... } App.kt
  18. REPLACE WITH DAGGER.ANDROID App.kt class App : DaggerApplication() { override

    fun applicationInjector() = DaggerAppComponent.builder() .application(this) .build() override fun onCreate() { super.onCreate() ... } ... }
  19. REPLACE WITH DAGGER.ANDROID MainActivity.kt class MainActivity : AppCompatActivity() { @Inject

    lateinit var instance: Hoge override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) application.appComponent.activityComponent.inject(this) ... } ... }
  20. REPLACE WITH DAGGER.ANDROID MainActivity.kt class MainActivity : AppCompatActivity() { @Inject

    lateinit var instance: Hoge override fun onCreate(savedInstanceState: Bundle?) { AndroidInjection.inject(this) super.onCreate(savedInstanceState) ... } ... }
  21. MORE SIMPLE ΋ͬͱγϯϓϧʹॻ͚·͢

  22. MORE SIMPLE @ContributesAndroidInjector Dagger v2.11 ~

  23. MORE SIMPLE ActivityComponent.kt @Subcomponent(modules = ...) interface ActivityComponent : AndroidInjector<MainActivity>

    { @Subcomponent.Builder abstract class Builder : AndroidInjector.Builder<MainActivity>() }
  24. MORE SIMPLE ActivityComponent.kt @Subcomponent(modules = ...) interface ActivityComponent : AndroidInjector<MainActivity>

    { @Subcomponent.Builder abstract class Builder : AndroidInjector.Builder<MainActivity>() } Component࡞Βͳͯ͘ྑ͍
  25. MORE SIMPLE ActivityModule.kt @Module(subcomponents = ActivityComponent::class) internal abstract class ActivityModule

    { @Binds @IntoMap @ActivityKey(MainActivity::class) internal abstract fun bindMainInjector( builder: ActivityComponent.Builder ): AndroidInjector.Factory<out Activity> }
  26. MORE SIMPLE ActivityModule.kt @Module(subcomponents = ActivityComponent::class) internal abstract class ActivityModule

    { @Binds @IntoMap @ActivityKey(MainActivity::class) internal abstract fun bindMainInjector( builder: ActivityComponent.Builder ): AndroidInjector.Factory<out Activity> } Subcomponentͷbind΋ॻ͔ͳ͍͍ͯ͘
  27. MORE SIMPLE ActivityBuilder.kt @Module internal abstract class ActivityBuilder { @ContributesAndroidInjector(modules

    = arrayOf(...)) internal abstract fun contributeMainActivityInjector(): MainActivity } ͜Ε͚ͩͰSubcomponent͕Ͱ͖Δ
  28. MORE SIMPLE @ContributesAndroidInjectorͰγϯϓϧʹ

  29. FIN Thanks:)