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

Replace with dagger.android

Yuji Koketsu
October 25, 2017

Replace with dagger.android

Yuji Koketsu

October 25, 2017
Tweet

More Decks by Yuji Koketsu

Other Decks in Programming

Transcript

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

    CyberAgent, Inc ▸ ݩ AbemaTV >> ৽نϝσΟΞ
  2. 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
  3. WHAT IS DAGGER.ANDROID? class MainActivity : AppCompatActivity() { lateinit var

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

    var instance: Hoge override fun onCreate(state: Bundle?) { AndroidInjection.inject(this) super.onCreate(state) ... } ... }
  5. 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" }
  6. REPLACE WITH DAGGER.ANDROID AppComponent.kt @Singleton @Component(modules = arrayOf( AppModule::class ))

    interface AppComponent { fun inject(app: App) fun activityComponent(): ActivityComponent }
  7. 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) }
  8. REPLACE WITH DAGGER.ANDROID ActivityComponent.kt @Subcomponent(modules = arrayOf( ... )) interface

    ActivityComponent : AndroidInjector<MainActivity> { @Subcomponent.Builder abstract class Builder : AndroidInjector.Builder<MainActivity>() }
  9. 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͢Δ
  10. 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) }
  11. 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) }
  12. 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
  13. REPLACE WITH DAGGER.ANDROID App.kt class App : DaggerApplication() { override

    fun applicationInjector() = DaggerAppComponent.builder() .application(this) .build() override fun onCreate() { super.onCreate() ... } ... }
  14. 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) ... } ... }
  15. 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) ... } ... }
  16. MORE SIMPLE ActivityComponent.kt @Subcomponent(modules = ...) interface ActivityComponent : AndroidInjector<MainActivity>

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

    { @Subcomponent.Builder abstract class Builder : AndroidInjector.Builder<MainActivity>() } Component࡞Βͳͯ͘ྑ͍
  18. 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> }
  19. 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΋ॻ͔ͳ͍͍ͯ͘
  20. MORE SIMPLE ActivityBuilder.kt @Module internal abstract class ActivityBuilder { @ContributesAndroidInjector(modules

    = arrayOf(...)) internal abstract fun contributeMainActivityInjector(): MainActivity } ͜Ε͚ͩͰSubcomponent͕Ͱ͖Δ