[Android] Dagger2 - Hilt๋ก ๋ง์ด๊ทธ๋ ์ด์
#1 Hilt ๊ฐ์
Hilt๋ฅผ ์ฌ์ฉํ ์ข ์ ํญ๋ชฉ ์ฝ์ | Android Developers
์ด ํ์ด์ง๋ Cloud Translation API๋ฅผ ํตํด ๋ฒ์ญ๋์์ต๋๋ค. Hilt๋ฅผ ์ฌ์ฉํ ์ข ์ ํญ๋ชฉ ์ฝ์ ์ปฌ๋ ์ ์ ์ฌ์ฉํด ์ ๋ฆฌํ๊ธฐ ๋ด ํ๊ฒฝ์ค์ ์ ๊ธฐ์ค์ผ๋ก ์ฝํ ์ธ ๋ฅผ ์ ์ฅํ๊ณ ๋ถ๋ฅํ์ธ์. Hilt๋ ํ๋ก์ ํธ์์ ์ข ์
developer.android.com
Hilt๋ Dagger2์ ๋ณต์กํ ๋ถ๋ถ์ ๋ ์ฝ๊ณ ํจ์จ์ ์ผ๋ก ์ฌ์ฉํ ์ ์๊ฒ ๋ง๋ค์ด์ฃผ๋ ์ถ์ํ ๊ณ์ธต์ด๋ค. ๊ทธ๋ฐ ๋ฉด์์ Dagger2 - Hilt ๊ด๊ณ๋ SQLite - Room์ ๊ด๊ณ์๋ ๊ฐ๋ค. Hilt์ ์ฌ์ฉํ๋ฉด Dagger2๋ฅผ ์์ผ๋ก ์ฌ์ฉํ๋ ๊ฒ๋ณด๋ค ์ธ์ธํ ์ค์ ์ ๋ถ๊ฐ๋ฅํ๋ค. ํ์ง๋ง, ์์ฉ๊ตฌ ์ฝ๋๋ฅผ ๋ง์ด ์ค์ฌ์ค๋ค. ๋ง ๊ทธ๋๋ก ์ถ์ํ ๊ณ์ธต์ด๋ ๋ง์ด๋ค.
#2 Hilt ์ฌ์ฉํ๊ธฐ - ๊ธฐ์ด
#2-1 Hilt๋ฅผ ์ ์ฉํ ์ํ ์ฑ
[Android] Dagger2 - Activity์ Dependency ์ฃผ์
#1 ์ด์ ๊ธ [Android] Dagger2 - ๊ธฐ์ด#1 ์ด์ ๊ธ ์์กด์ฑ ์ฃผ์ (Dependency Injection)#1 ์์กด์ฑ ์ฃผ์ (Dependency Injection)์ด๋?#1-1 Dependent์ Dependencyfun main() { val car = Car() car.startCar() } class Car { private val engine = Engine
kenel.tistory.com
Dagger2๋ฅผ ์ด์ฉํด ํ๋ ์ฃผ์
์ ๊ตฌํํ ์ ๊ฒ์๊ธ์ ์์ฑ๋ ์ฑ์ ์ผ๋ถ ์์ ํด์, ํด๋น ์ฑ์ด Dagger2 ๋์ Hilt๋ฅผ ์ด์ฉํด ์์กด์ฑ ์ฃผ์
์ ํ๊ฒ ๋ง๋ค์ด๋ณธ๋ค.
์์ ์ ๊ฐํ ์ํ ์ฑ์ ์์กด์ฑ ๊ทธ๋ํ๋ค.
#2-2 build.gradle (ํ๋ก์ ํธ ์์ค)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
...
// Hilt
id("com.google.dagger.hilt.android") version "2.46" apply false
}
#2-3 build.gradle (๋ชจ๋ ์์ค)
plugins {
...
// Hilt
id("com.google.dagger.hilt.android")
}
android {
...
}
dependencies {
...
/*
// Dagger2
val daggerVersion = "2.51.1"
implementation("com.google.dagger:dagger:$daggerVersion")
kapt("com.google.dagger:dagger-compiler:$daggerVersion")
*/
// Hilt
val hiltVersion = "2.46"
implementation("com.google.dagger:hilt-android:$hiltVersion")
kapt("com.google.dagger:hilt-android-compiler:$hiltVersion")
}
๊ธฐ์กด Dagger2์ ๊ดํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์ฃผ์ ์ฒ๋ฆฌ๋ฅผ ํตํด ์ญ์ ํด๋ฒ๋ ธ๋ค. Hilt ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ด๋ฏธ ํฌํจ๋์ด์๊ธฐ ๋๋ฌธ์ด๋ค.
#2-4 ์ฌ์ฉ์ ์ ์ @Component ํด๋์ค (CarComponent.kt) ์ญ์
/* ์ญ์
import dagger.Component
@Component
interface CarComponent {
fun inject(mainActivity: MainActivity)
}
*/
Hilt๊ฐ Dagger2์ ๊ตฌ๋ณ๋๋ ๊ฐ์ฅ ํฐ ๋ถ๋ถ์ ๋ฐ๋ก @Component ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ํ๋ก๊ทธ๋๋จธ๊ฐ ๋ง๋ค๋๋ก ํ์ฉํ์ง ์๋๋ค๋ ์ ์ด๋ค. Hilt์์๋ ์ฌ๋ฌ ๊ฐ์ง Component ์ธ์คํด์ค๊ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋จ์์ ์ด๋ฏธ ์์ฑ๋ ์ฑ๋ก ํ๋ก๊ทธ๋๋จธ์๊ฒ ์ ๊ณต๋๋ค. ํ๋ก๊ทธ๋๋จธ๋ ๊ทธ ์ค์์ ํ์ํ ์ปดํฌ๋ํธ๋ฅผ ๊ณจ๋ผ์ ์ฌ์ฉํ๋ ๊ตฌ์กฐ๋ค (์ฐธ์กฐ: Hilt์ built-in๋ ์ปดํฌ๋ํธ์ ๋ชฉ๋ก). ๋ฐ๋ผ์ CarComponent.kt ํ์ผ์ ์๋๋ก์ด๋ ํ๋ก์ ํธ์์ ์ ๊ฑฐํ๋ค.
#2-5 Application ํด๋์ค์ @HiltAndroidApp ์ด๋ ธํ ์ด์ ๋ถ์ด๊ธฐ
// package com.example.migrationtohilt
import android.app.Application
import dagger.hilt.android.HiltAndroidApp
// AndroidManifest.xml์์ android:name=".MyApplication" ์ถ๊ฐํ๋ ๊ฒ ์์ง ๋ง๊ธฐ!
@HiltAndroidApp
class MyApplication : Application()
์ด @HiltAndroidApp ์ด๋
ธํ
์ด์
์ Hilt์ ์์์ ์ฝ๋ ์์ฑ์ ๊ฐ์(trigger)ํ๊ฒ ๋ง๋ ๋ค.
#2-6 AndroidManifest ์์
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:name=".MyApplication"
...>
<activity
...
</activity>
</application>
</manifest>
์๋๋ก์ด๋ ํ๋ก์ ํธ๊ฐ ๊ธฐ๋ณธ Application ํด๋์ค ๋์ ์ด Application ํด๋์ค๋ฅผ ์ฌ์ฉํ๋๋ก ๋ช
์ํ๋ค. AndroidManifest์ <application> ํ๊ทธ์ android:name ์์ฑ์ ๋ฐฉ๊ธ ๋ง๋ Application ํด๋์ค์ ์ด๋ฆ์ ๋ฃ์.
#2-7 ์๋๋ก์ด๋ ํด๋์ค์ @AndroidEntryPoint ์ด๋ ธํ ์ด์ ๋ถ์ด๊ธฐ
// package com.example.migrationtohilt
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject
lateinit var car: Car
override fun onCreate(savedState: Bundle?) {
super.onCreate(savedState)
setContentView(R.layout.activity_main)
/*
val crankshaft = Crankshaft()
val cylinder = Cylinder()
val piston = Piston(crankshaft, cylinder)
val engine = Engine(piston)
val airbag = Airbag()
val battery = Battery()
val car = Car(engine, airbag, battery)
*/
// DaggerCarComponent.create().inject(this)
car.startCar()
}
}
์์กด์ฑ์ ์ฃผ์
๋ฐ์ ์๋๋ก์ด๋ ํด๋์ค์, ์ด ํ๋ก์ ํธ์ ๊ฒฝ์ฐ์๋ MainActivity์ @AndroidEntryPoint ์ด๋
ธํ
์ด์
์ ๋ถ์ด๋ค. ์ด ์ด๋
ธํ
์ด์
์ Hilt๋ก๋ถํฐ ์์กด์ฑ์ ์ฃผ์
๋ฐ์ ํด๋์ค๋ฅผ ๋ช
์ํ๋ ์ญํ ์ ์ํํ๋ค. ๋, ์ด ์ด๋
ธํ
์ด์
์ ์ฌ๊ท์ ์ผ๋ก ๋ถ์ฌ์ผํ๋ค. ์๋ฅผ ๋ค์ด, ์ด๋ค Fragment์ @AndroidEntryPoint๋ก ์์กด์ฑ์ ์ฃผ์
ํ๋ ค๋ฉด, ํด๋น Fragment๋ฅผ ์ฌ์ฉํ๋ ๋ชจ๋ Activity์๋ @AndroidEntryPoint ์ด๋
ธํ
์ด์
์ ๋ถ์ฐฉ์ด ์๊ตฌ๋๋ค. DaggerCarComponent.create().inject(this)๋ ์ญ์ ํ๋ค. ์ด ๋ถ๋ถ์ Hilt์์ ์์์ ์ผ๋ก ์งํํ๊ธฐ ๋๋ฌธ์ด๋ค. ์ฆ, @Inject๊ฐ ๋ถ์ ํ๋(์ฝํ๋ฆฐ์ด๋๊น ๋ ์ ํํ ๋งํ๋ฉด ํ๋กํผํฐ)์ Hilt๊ฐ ์์์ ์ธ์คํด์ค๋ฅผ ํ ๋นํ๋ค.
#2-8 ์๋ ํ์ธ (๋ก๊ทธ ๋ฉ์์ง)
Crankshaft is ready
Cylinder is ready
Piston is ready
Engine is ready
Airbag is ready
Battery is ready
Car is ready
#3 Hilt ์ฌ์ฉํ๊ธฐ - @Module๊ณผ @Provides
#3-1 ๊ฐ์
[Android] Dagger2 - @Provides
#1 ์ด์ ๊ธ [Android] Dagger2 - ๊ธฐ์ด#1 ์ด์ ๊ธ ์์กด์ฑ ์ฃผ์ (Dependency Injection)#1 ์์กด์ฑ ์ฃผ์ (Dependency Injection)์ด๋?#1-1 Dependent์ Dependencyfun main() { val car = Car() car.startCar() } class Car { private val engine = Engine
kenel.tistory.com
์ ๊ฒ์๊ธ์ฒ๋ผ, Hilt์์ @Module ๋ฐ @Provides๋ฅผ ์ฌ์ฉํด๋ณธ๋ค. ์ ๊ฒ์๊ธ์์ ๋์ค๋ฏ, @Provides๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๋ ์์กด์ฑ์ธ ํด๋์ค๋ฅผ ๊ฑด๋ค ์ ์๋ ๊ฒฝ์ฐ๋ค. Airbag ๋ฐ Battery๊ฐ Stringํ ์์กด์ฑ์ ์๊ตฌํ๊ฒ ๋ง๋ค์ด๋ณธ๋ค. ๊ทธ๋ฅ ์ ๊ฒ์๊ธ์ ๋ด์ฉ์ Hilt๋ก ๋๊ฐ์ด ์ฌํํ๋ค๊ณ ๋ณด๋ฉด ๋๋ค.
#3-2 Airbag ํด๋์ค ์์
// package com.example.migrationtohilt
import android.util.Log
import javax.inject.Inject
import javax.inject.Named
class Airbag @Inject constructor(@Named("Airbag") private val manufacturer: String) {
fun startAirbag() {
Log.i("interfacer_han", "${this::class.simpleName} is ready")
Log.i("interfacer_han", "${this::class.simpleName} made by ${manufacturer}")
}
}
#3-3 Battery ํด๋์ค ์์
// package com.example.migrationtohilt
import android.util.Log
import javax.inject.Inject
import javax.inject.Named
class Battery @Inject constructor(@Named("Battery") private val manufacturer: String) {
fun startBattery() {
Log.i("interfacer_han", "${this::class.simpleName} is ready")
Log.i("interfacer_han", "${this::class.simpleName} made by ${manufacturer}")
}
}
#3-4 Module ์์ฑ ๋ฐ @InstallIn ์ด๋ ธํ ์ด์ ์ ํตํ ์ปดํฌ๋ํธ ์ง์
// package com.example.migrationtohilt
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Named
@Module
@InstallIn(SingletonComponent::class)
class MyModule {
@Provides
@Named("Airbag")
fun providesAirbagManufacturer(): String {
return "KENEL"
}
@Provides
@Named("Battery")
fun providesBatteryManufacturer(): String {
return "TISTORY"
}
}
์ฝ๋๋ Dagger์์์ ๊ฑฐ์ ๊ฐ์ผ๋, @InstallIn์ด๋ผ๋ ์ด๋
ธํ
์ด์
์ ์ ๋ฌด๋ผ๋ ์ฐจ์ด์ ์ด ์กด์ฌํ๋ค. #2-4์์ ๋งํ๋ฏ, Hilt๋ ์ปดํฌ๋ํธ๋ฅผ '๋ง๋๋' ๊ฒ์ด ์๋๋ผ, '์ ํํ๋' ๊ฒ์ด๋ฏ๋ก @Component ์ด๋
ธํ
์ด์
์ด ์๋ค. ์ด ๋ ์๋์ @Component์ ๋์๋๋ ๊ฒ์ @InstallIn๋ผ๊ณ ํ ์ ์๋๋ฐ, @InstallIn์ ํด๋น Module์ด ์ด๋ค (Hilt์ built-in๋) ์ปดํฌ๋ํธ์ ์ค์น(Install)๋ ์ง ์ง์ ํ๋ ์ด๋
ธํ
์ด์
์ด๊ธฐ ๋๋ฌธ์ด๋ค. Dagger2์์๋ @Component ์ด๋
ธํ
์ด์
์ ์ด๋ค ๋ชจ๋์ ์ธ ์ง ๋ช
์ํ๋๋ฐ, Hilt์์๋ ๋ฐ๋๋ก @Module์์ ์ด๋ค Component์ ์ค์น๋ ์ง ๋ช
์ํ๊ณ ์๋ค๋ ์ ์ด ๋๋น๋๋ค.
(2025-02-01 ์ถ๊ฐ) @Module์ class๊ฐ ์๋๋ผ object๋ก ์ ์ธํ๋ ๊ฒ์ด ์ข๋ค. Hilt ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ @Module์ ๋จ์ผ ๊ฐ์ฒด(์ฑ๊ธํค)๋ก ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ด๋ค. ์ฆ, ์ฌ๋ฌ๋ฒ ์ธ์คํด์คํ ๋ ๊ฒ์ ์์ ํ๋ ํค์๋์ธ class๋ฅผ ์ธ ์ด์ ๊ฐ ์๋ค. ๋ฌผ๋ก ์์ ์ฝ๋์ฒ๋ผ, object ์๋ฆฌ์ ๋์ class๋ฅผ ๋ฌ๋ ๋ฑํ ์๋ฌ๊ฐ ๋ฐ์ํ์ง ์๋๋ค.
#3-5 ์ ๋ฐ์ดํธ๋ ์์กด์ฑ ๊ทธ๋ํ
Airbag ๋ฐ Battery ํด๋์ค์์ Stringํ ์์กด์ฑ์ ์ถ๊ฐํ๊ณ , Module๋ก ํด๋น Stringํ ์์กด์ฑ์ @Providesํจ์ผ๋ก์จ ์
๋ฐ์ดํธ๋ ์์กด์ฑ ๊ทธ๋ํ๋ค.
#3-6 ์๋ ํ์ธ (๋ก๊ทธ ๋ฉ์์ง)
Crankshaft is ready
Cylinder is ready
Piston is ready
Engine is ready
Airbag is ready
Airbag made by KENEL
Battery is ready
Battery made by TISTORY
Car is ready
#4 Hilt ์ฌ์ฉํ๊ธฐ - ๋งค๊ฐ๋ณ์ ๋์ ํ ๋น
#4-1 ๊ฐ์ (@AssistedInject์ @AssistedFactory์ ์ฌ์ฉ ๋ฐฐ๊ฒฝ)
์์ ๋งํ๋ฏ, Hilt๊ฐ Dagger2์ ๊ตฌ๋ณ๋๋ ๊ฐ์ฅ ํฐ ๋ถ๋ถ์ ๋ฐ๋ก @Component ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ํ๋ก๊ทธ๋๋จธ๊ฐ ๋ง๋ค์ง ์์ผ๋ฉฐ, Component ์ธ์คํด์ค๊ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋จ์์ ์ด๋ฏธ ์์ฑ๋ ์ฑ๋ก ํ๋ก๊ทธ๋๋จธ์๊ฒ ์ ๊ณต๋๋ค. ๋ฐ๋ผ์ ์ด์ ๊ฒ์๊ธ์์์ฒ๋ผ Componenet.Builder๋ฅผ ํตํด @Module ํด๋์ค ๋ฐ @Provides ๋ฉ์๋์ ๋์ ๋งค๊ฐ๋ณ์๋ฅผ ์ ๋ฌํ๋ ๊ฒ์ด ๋ถ๊ฐ๋ฅํ๋ค.
๊ทธ๋์ Hilt์์๋ ๋์ ๋งค๊ฐ๋ณ์๋ฅผ ์ ๋ฌํ๊ธฐ ์ํ ์๋๋ก, ๋์ @AssistedInject ๋ฐ @AssistedFactory ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ๋ค. @AssistedFactory ์ด๋
ธํ
์ด์
์ ์ด๋ฆ์์ ๋ณด๋ฏ, Factory ํจํด์ ๊ตฌํํด์ Component.Builder์์ ํ๋ ๊ฒ๊ณผ ๋น์ทํ ๋งฅ๋ฝ์ผ๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ ๊ฒ์ด๋ค.
#4-2 @AssistedInject ๋ฐ @AssistedFactory ์ด๋ ธํ ์ด์ ๋ถ์ด๊ธฐ
// package com.example.migrationtohilt
import android.util.Log
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
class Car @AssistedInject constructor(
@Assisted private val engine: Engine,
private val airbag: Airbag,
private val battery: Battery
) {
fun startCar() {
engine.startEngine()
airbag.startAirbag()
battery.startBattery()
Log.i("interfacer_han", "${this::class.simpleName} is ready")
}
@AssistedFactory
fun interface Factory {
fun create(engine: Engine): Car
}
}
ํผ์น๋ชปํ ์ฌ์ ์ผ๋ก, Car ํด๋์ค์ ์์กด์ฑ ์ค ํ๋์ธ Engine ํด๋์ค๋ฅผ ๋ฐ๋์ ๋ฐํ์ ์ค์ ๋์ ์ผ๋ก ํ ๋นํด์ผ๋๋ ์ํฉ์ด ์๊ฒผ๋ค๊ณ ๊ฐ์ ํ๋ค. ์ด๋ ๊ฒ ์์กด์ฑ๋ค ์ค ํ๋๋ผ๋ ๋์ ํ ๋น์ด ์๊ตฌ๋๋ฉด ์์ฑ์์ ๋ถ์ @Inject ์ด๋
ธํ
์ด์
์ ํต์งธ๋ก @AssistedInject๋ก ๋ฐ๊ฟ์ผ ํ๋ค. ์ฌ๊ธฐ์, ๋์ ํ ๋น์ด ์๊ตฌ๋๋ ์์กด์ฑ์๋ @Assisted ์ด๋
ธํ
์ด์
์ ์ถ๊ฐ๋ก ๋ถ์ธ๋ค. ๊ทธ๋ฆฌ๊ณ ํด๋์ค ๋ด๋ถ์ Hilt๊ฐ Factory ํจํด ์ฝ๋๋ฅผ ์์ฑํ๋๋ก @AsssistedFactory ์ด๋
ธํ
์ด์
์ด ๋ถ์ SAM ํ๋๋ฅผ ์ถ๊ฐํ๋ค. interface ํค์๋ ์ผ์ชฝ์ fun์ ์ญ์ ํ๊ณ ํ๋ฒํ interface(SAM์ ํ๋ณด)๋ก ๋ฌ๋ ํฐ ์๊ด์ ์๋ค. ์ด๋ ์ชฝ์ด๋ ํ๋ก์ ํธ ๋น๋๋ ์ ๋๋ค.
#4-3 ์ ๋ฐ์ดํธ๋ ์์กด์ฑ ๊ทธ๋ํ
#4-2์ ์ฝ๋ ์์ ์ผ๋ก ์ธํด ์
๋ฐ์ดํธ๋ ์์กด์ฑ ๊ทธ๋ํ๋ค. @AssistedInject๋ Hilt๊ฐ ๊ทธ๋ ค๋ด๋ ์์กด์ฑ ๊ทธ๋ํ๋ฅผ ๋ถ๋ฆฌ์ํจ๋ค. ์ฆ, Car์ Engine์ ์๋ก ๋ค๋ฅธ 2๊ฐ์ ๊ทธ๋ํ์ ์ํ๊ฒ ๋๋ ๊ฒ์ด๋ค. ์ด ๋์๋๋ฅผ ๊ทธ๋ฆฌ๋, @Assisted์ ์๋ฏธ๊ฐ ์ ๋๋ก ๋ณด์ธ๋ค. ๋ฐ๋ก ํ๋ก๊ทธ๋๋จธ๊ฐ ๋์์ฃผ์ด์ผ(Assist)ํ๋ ์์กด์ฑ ์ฃผ์
์ด๋ผ๋ ์๋ฏธ ๋ง์ด๋ค.
#4-4 MainActivity ์์
// package com.example.migrationtohilt
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
lateinit var car: Car
@Inject
lateinit var carFactory: Car.Factory
@Inject
lateinit var engine: Engine
override fun onCreate(savedState: Bundle?) {
super.onCreate(savedState)
setContentView(R.layout.activity_main)
car = carFactory.create(engine) // ๋ฐํ์ ๋ Engine ๊ฐ์ฒด ์ฃผ์
car.startCar()
}
}
Car ํด๋์ค ๋์ Car.Factory ํด๋์ค๋ฅผ Hilt๋ก๋ถํฐ ์ฃผ์
๋ฐ๋๋ค. ์ด ํด๋์ค๋ ๋ฐฉ๊ธ Car ์์ ์ ์ํ๋ interface๋ฅผ Hilt๊ฐ ์์์ ํด๋์ค๋ก์ ๊ตฌํํด๋์ ๊ฒ์ด๋ค. ์ด ํด๋์ค๋ฅผ ํตํด ๋งค๊ฐ๋ณ์์ ๋ฐํ์ ์ค ๋์ ํ ๋น์ด ๊ฐ๋ฅํ๋ค.
#4-5 ์๋ ํ์ธ
Crankshaft is ready
Cylinder is ready
Piston is ready
Engine is ready
Airbag is ready
Airbag made by KENEL
Battery is ready
Battery made by TISTORY
Car is ready
#3์ ์์ ์ฌํญ๋ ๊ฐ์ด ๋
น์ฌ๋๊ธฐ ๋๋ฌธ์, "Airbag made by KENEL" ๋ฐ "Battery made by TISTORY"๋ผ๋ ๋ก๊ทธ ๋ฉ์์ง๋ ๋ณด์ธ๋ค.
#5 Hilt ์ฌ์ฉํ๊ธฐ - ์ธํฐํ์ด์ค ๊ตฌํ์ฒด ์ฃผ์
#5-1 ๊ฐ์
[Android] Dagger2 - ์ธํฐํ์ด์ค ๊ตฌํ์ฒด ์ฃผ์ (@Binds)
#1 ์ด์ ๊ธ [Android] Dagger2 - ๊ธฐ์ด#1 ์ด์ ๊ธ ์์กด์ฑ ์ฃผ์ (Dependency Injection)#1 ์์กด์ฑ ์ฃผ์ (Dependency Injection)์ด๋?#1-1 Dependent์ Dependencyfun main() { val car = Car() car.startCar() } class Car { private val engine = Engine
kenel.tistory.com
์ ๊ฒ์๊ธ์ฒ๋ผ Hilt์์๋ @Provides ๋๋ @Binds ์ด๋
ธํ
์ด์
์ ํตํด ์ธํฐํ์ด์ค ๊ตฌํ์ฒด๋ฅผ ์ฃผ์
ํ ์ ์๋ค. ๋ฌ๋ผ์ง๋ ๊ฒ Component ์ฒ๋ฆฌ๋ฅผ ์ํ @InstallIn ์ด๋
ธํ
์ด์
์ธ์๋ ์๊ธฐ ๋๋ฌธ์, ์ค๋ช
์์ด ์ฝ๋ ์์ฃผ๋ก ๋น ๋ฅด๊ฒ ๋๊ธด๋ค. ์ด ๊ฒ์๊ธ์์๋ Piston ํด๋์ค๋ฅผ ์ธํฐํ์ด์ค๋ก ์ ํํ๊ณ ํด๋น ์ธํฐํ์ด์ค์ ๊ตฌํ์ฒด๋ฅผ ์ฃผ์
ํ๋ ์ฝ๋๋ฅผ ์์ฑํด๋ณธ๋ค.
#5-2 Piston ํด๋์ค๋ฅผ ์ธํฐํ์ด์ค๋ก ์ ํ
// package com.example.migrationtohilt
interface Piston {
fun startPiston()
}
#5-3 Piston ์ธํฐํ์ด์ค์ ๊ตฌํ์ฒด TitaniumPiston ํด๋์ค ์์ฑ
// package com.example.migrationtohilt
import android.util.Log
import javax.inject.Inject
class TitaniumPiston @Inject constructor(
private val crankshaft: Crankshaft,
private val cylinder: Cylinder
) : Piston {
override fun startPiston() {
crankshaft.startCrankshaft()
cylinder.startCylinder()
Log.i("interfacer_han", "${this::class.simpleName} is ready")
}
}
#5-4 Piston์ ๊ตฌํ์ฒด๋ฅผ @Providesํ๊ฑฐ๋ @Bindsํ๊ธฐ ์ํ PistonModule
// package com.example.migrationtohilt
import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
/*
// @Provides๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ
@Module
@InstallIn(SingletonComponent::class)
class PistonModule {
@Provides
fun providesTitaniumPiston(titaniumPiston: TitaniumPiston) : Piston {
return titaniumPiston
}
}
*/
// @Binds๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ
@Module
@InstallIn(SingletonComponent::class)
abstract class PistonModule {
@Binds
abstract fun providesTitaniumPiston(titaniumPiston: TitaniumPiston): Piston
}
์ผ๋ฐ ํด๋์ค PistonModule๊ณผ @Provides์ ์กฐํฉ์ ์ฌ์ฉํ๊ฑฐ๋, ์ถ์ ํด๋์ค PistonModule๊ณผ @Binds์ ์กฐํฉ์ ์ฌ์ฉํ๋ค. ์ธํฐํ์ด์ค ๊ตฌํ์ฒด์ ์ฃผ์
์ ํ์์ ๋ฐฉ์์ด ์ผ๋ฐ์ ์ด๋ค.
#5-5 ์ ๋ฐ์ดํธ๋ ์์กด์ฑ ๊ทธ๋ํ
#5-6 ์๋ ํ์ธ (๋ก๊ทธ ๋ฉ์์ง)
Crankshaft is ready
Cylinder is ready
TitaniumPiston is ready
Engine is ready
Airbag is ready
Airbag made by KENEL
Battery is ready
Battery made by TISTORY
Car is ready
#3์ ์์ ์ฌํญ๋ ๊ฐ์ด ๋
น์ฌ๋๊ธฐ ๋๋ฌธ์, "Airbag made by KENEL" ๋ฐ "Battery made by TISTORY"๋ผ๋ ๋ก๊ทธ ๋ฉ์์ง๋ ๋ณด์ธ๋ค.
#6 ์์ฝ
Hilt๋ฅผ ์ฌ์ฉํด์ ์ป๋ ์ด์ ์, ์ถ์ํ ๊ณ์ธต์ ์ด์ฉํด์ ์ป๋ ์ด์ ๊ณผ ๋์ผํ๋ค.
#7 ์์ฑ๋ ์ฑ
#7-1 ๊นํ๋ธ ๋งํฌ
android-practice/dagger2/MigrationToHilt at master · Kanmanemone/android-practice
Contribute to Kanmanemone/android-practice development by creating an account on GitHub.
github.com
#2 ~ #5์ ๋ชจ๋ ์์ ์ฌํญ์ ์ ๋ถ ์ ์ฉํ๋ค.
#7-2 ์์กด์ฑ ๊ทธ๋ํ
#2 ~ #5 ์ ๋ชจ๋ ์์ ์ฌํญ์ด ์ ๋ถ ์ ์ฉ๋ ์์กด์ฑ ๊ทธ๋ํ๋ค.