Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Replace with dagger.android
Search
Yuji Koketsu
October 25, 2017
Programming
0
2.3k
Replace with dagger.android
Yuji Koketsu
October 25, 2017
Tweet
Share
More Decks by Yuji Koketsu
See All by Yuji Koketsu
How to contribute android-gpuimage
kettsun0123
0
520
AutoSizingTextView
kettsun0123
0
180
Android Instant Apps or Progressive Web Apps
kettsun0123
1
400
Other Decks in Programming
See All in Programming
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
120
Better Code Design in PHP
afilina
PRO
0
130
subpath importsで始めるモック生活
10tera
0
330
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
260
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
210
Figma Dev Modeで変わる!Flutterの開発体験
watanave
0
150
rails new flags - `rails new` のフラグから Rails を構成するコンポーネントの変遷をザックリ眺める
snaka
0
1.8k
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.8k
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
120
CSC509 Lecture 12
javiergs
PRO
0
160
Featured
See All Featured
Site-Speed That Sticks
csswizardry
0
37
Optimizing for Happiness
mojombo
376
70k
How to train your dragon (web standard)
notwaldorf
88
5.7k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Producing Creativity
orderedlist
PRO
341
39k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
The Pragmatic Product Professional
lauravandoore
31
6.3k
Navigating Team Friction
lara
183
14k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
380
Transcript
REPLACE WITH DAGGER.ANDROID
HELLO ABOUT ME Yuji Koketsu @kettsun0123 ▸ Android Developer ▸
CyberAgent, Inc ▸ ݩ AbemaTV >> ৽نϝσΟΞ
REPLACE WITH DAGGER.ANDROID What is dagger.android?
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
WHAT IS DAGGER.ANDROID? class MainActivity : AppCompatActivity() { lateinit var
instance: Hoge override fun onCreate(state: Bundle?) { super.onCreate(state) ... } ... }
WHAT IS DAGGER.ANDROID? class MainActivity : AppCompatActivity() { @Inject lateinit
var instance: Hoge override fun onCreate(state: Bundle?) { AndroidInjection.inject(this) super.onCreate(state) ... } ... }
REPLACE WITH DAGGER.ANDROID dagger.androidͰஔ͖͑Δ
REPLACE WITH DAGGER.ANDROID App MainActivity AppModule class Hoge
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" }
REPLACE WITH DAGGER.ANDROID AppComponent.kt @Singleton @Component(modules = arrayOf( AppModule::class ))
interface AppComponent { fun inject(app: App) fun activityComponent(): ActivityComponent }
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) }
REPLACE WITH DAGGER.ANDROID ActivityComponent.kt @Subcomponent(modules = arrayOf( ... )) interface
ActivityComponent { fun inject(activity: MainActivity) }
REPLACE WITH DAGGER.ANDROID ActivityComponent.kt @Subcomponent(modules = arrayOf( ... )) interface
ActivityComponent : AndroidInjector<MainActivity> { @Subcomponent.Builder abstract class Builder : AndroidInjector.Builder<MainActivity>() }
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͢Δ
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) }
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) }
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
REPLACE WITH DAGGER.ANDROID App.kt class App : DaggerApplication() { override
fun applicationInjector() = DaggerAppComponent.builder() .application(this) .build() override fun onCreate() { super.onCreate() ... } ... }
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) ... } ... }
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) ... } ... }
MORE SIMPLE ͬͱγϯϓϧʹॻ͚·͢
MORE SIMPLE @ContributesAndroidInjector Dagger v2.11 ~
MORE SIMPLE ActivityComponent.kt @Subcomponent(modules = ...) interface ActivityComponent : AndroidInjector<MainActivity>
{ @Subcomponent.Builder abstract class Builder : AndroidInjector.Builder<MainActivity>() }
MORE SIMPLE ActivityComponent.kt @Subcomponent(modules = ...) interface ActivityComponent : AndroidInjector<MainActivity>
{ @Subcomponent.Builder abstract class Builder : AndroidInjector.Builder<MainActivity>() } Component࡞Βͳͯ͘ྑ͍
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> }
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ॻ͔ͳ͍͍ͯ͘
MORE SIMPLE ActivityBuilder.kt @Module internal abstract class ActivityBuilder { @ContributesAndroidInjector(modules
= arrayOf(...)) internal abstract fun contributeMainActivityInjector(): MainActivity } ͜Ε͚ͩͰSubcomponent͕Ͱ͖Δ
MORE SIMPLE @ContributesAndroidInjectorͰγϯϓϧʹ
FIN Thanks:)