์ด ๊ธ์์ ๋ฐฐ์ธ์
- ๋ค๋ฅธ DI์์ ์ฐจ์ด์
- Hilt์ ๊ดํ ์ ๋ฐ์ ์ธ ์ง์
- Hilt๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
- Hilt์์ ์์ฃผ ์ฌ์ฉํ๋ Annotation
DI -> Dependency Inject ์์กด์ฑ ์ฃผ์ ์ด๋ผ๊ณ ํ๋ค.
์ด์ ์ Koin์ ๊ดํ ๋ธ๋ก๊ทธ๋ฅผ ์์ฑ ํ์ ๋๋ ๊ณต๋ถ๋ฅผ ํ์์ง๋ง Hilt๋ฅผ ์ข ๋ ๋ง์ด ์์๋ณด๋ ค ๋ค์๊ธ ๊ธ์ ์ด๋ค.
์ด์ ์ ์ผ๋ Koin์ ๊ดํ ๋ธ๋ก๊ทธ
์์กด์ฑ ์ฃผ์ ์ Koin๐ช ํ ๋ข์ - 1
์์กด์ฑ ์ฃผ์ ์ด๋ผ.. ๋๋ฌด๋๋ ์ต์ํ์ง ์์ ๋ฌธ์ฅ์ด๋ค. ๋จ์๊ฒ ์์กดํ์ง๋ง๊ณ ์ฃผ์ฒด์ ์ผ๋ก ์ด์ ๋ผ๋ ๋ง์ ์ฌ์ฉํ ๋๋ง ์ผ๋ ๋จ์ด๋ฅผ ๊ฐ๋ฐํ ๋ ์ฌ์ฉํ๋ ์ด์ํ๊ณ ๋จ๋ฒ์ ์ดํด๊ฐ ๋์ง ์๋๋ค. ์์กด
taekwon-seungwon.tistory.com
์์กด์ฑ ์ฃผ์ ์ Koin๐ช ๋ ๋ข์ - 2
์ด๋ฒ์ ์ค์ ๋ก ํ๋ก๊ทธ๋จ์ ์์กด์ฑ ์ฃผ์ (DI)๋ฅผ ์ ์ฉ์์ผ๋ณด๋ฉด์ ์ฝ์ง์ ์ฐ์์์ ๋๊ผ๋ ์ ๊ณผ ํ์ํ ๋ถ๋ถ์ ์์ฑํ ์์ ์ ๋๋ค. 1. Fragment๋ผ๋ฆฌ ViewModel์ ๊ณต์ ํ๊ณ ์ถ์ ๋! class JoinEntrepreneurFragment:
taekwon-seungwon.tistory.com
๋ค๋ฅธ DI์์ ์ฐจ์ด์
์ด์ ์๋ ์์ฑํ๋ Koin๊ณผ Hilt, Dagger์ ์ฐจ์ด์ ๋ํด ์ ๊น ๋ณด์ฌ๋๋ฆฌ์๋ฉด
Hilt(Jetpack) | Dagger2 | Koin | |
Java | O | O | X |
Kotlin | O | O | O |
๋ฌ๋์ปค๋ธ | ๋ฎ์ | ๋์ | ๋ฎ์ |
์๋ฌ๊ฒ์ถ ์์ | Compile Time | Compile Time | Runtime |
์ค์ ํ๋ก์ ํธ ๊ฒ์ฆ |
๋ฌด๋ ค 2.44 ๋ฒ์ ๊น์ง ์ถ์ |
๋ง์ ์ฑ์์ ๊ฒ์ฆ๋จ | ๋ง์ ์ฑ์์ ๊ฒ์ฆ๋จ |
์ด๋ฐ ์ฐจ์ด๋ฅผ ๋ณด์์๋ค.
ํ์ง๋ง ์ด์ ์ ๋ฌ๋์ปค๋ธ๋ ๋ฎ๊ณ ์ด์๋๊ณ ์๋ ์๋น์ค์ ์ถ๊ฐํด์ผํ๋ ๋ถ๋ถ ๋๋ฌธ์ ๊ฒ์ฆ๋ Koin์ ์ฌ์ฉํ์์ง๋ง
Hilt๊ฐ ์ ์ ๊ณ์ํด์ ์ ๋ฐ์ดํธ๋ ๋๊ณ ๊ฐ์ฅ ๋ง์ ๋๋ ๊ฒ์ Complie ๋จ๊ณ์์ ์๋ฌ๋ฅผ ๊ฒ์ถํด์ฃผ์๊ธฐ ๋๋ฌธ์ ์ฑ ์์ ์ฑ์์๋ ํจ์ฌ ์ข์ ์ ์ ๋ณด์ฌ์ฃผ๊ธฐ ๋๋ฌธ์ ๋์ ๋ฐ ์ฌ์ฉํ๊ณ ์๋ค.
Hilt์ ๊ดํ ์ ๋ฐ์ ์ธ ์ง์
Hilt์ ๋ป์ ์ง์ญํ๋ฉด
๋ช ์ฌ
1.(์นผ) ์๋ฃจ, (๊ณก๊ดญ์ด์) ์๋ฃจ, (๊ถ์ดยท๋จ๋ ๋ฑ์) ์์ก์ด(haft)
๋ก ๋์ด์๋ค. ๊ธฐ๋ฅ์ ์ดํดํ๊ณ ๋ป์ ๋ค์๋ณด๋ฉด ์ Hilt๋ผ๋ ๋จ์ด๋ฅผ ์ฌ์ฉํ๋์ง ์ฝ๊ฒ ์ ์ ์๋ค.
๋จผ์ DI, Hilt๋ฅผ ์ฌ์ฉ์ ์ด์ ์ ์๋ google developer์์ ์ค๋ช ํด์ค ๊ฒ์
- ํด๋์ค ์ฌ์ฌ์ฉ ๊ฐ๋ฅ ๋ฐ ์ข ์ ํญ๋ชฉ ๋ถ๋ฆฌ: ์ข ์ ํญ๋ชฉ ๊ตฌํ์ ์ฝ๊ฒ ๊ต์ฒดํ ์ ์์ต๋๋ค. ์ปจํธ๋กค ๋ฐ์ ์ผ๋ก ์ธํด ์ฝ๋ ์ฌ์ฌ์ฉ์ด ๊ฐ์ ๋์์ผ๋ฉฐ ํด๋์ค๊ฐ ๋ ์ด์ ์ข ์ ํญ๋ชฉ ์์ฑ ๋ฐฉ๋ฒ์ ์ ์ดํ์ง ์์ง๋ง ๋์ ๋ชจ๋ ๊ตฌ์ฑ์์ ์๋ํฉ๋๋ค.
- ๋ฆฌํฉํฐ๋ง ํธ์์ฑ: ์ข ์ ํญ๋ชฉ์ API ๋ ธ์ถ ์์ญ์ ๊ฒ์ฆ ๊ฐ๋ฅํ ์์๊ฐ ๋๋ฏ๋ก ๊ตฌํ ์ธ๋ถ์ ๋ณด๋ก ์จ๊ฒจ์ง์ง ์๊ณ ๊ฐ์ฒด ์์ฑ ์๊ฐ ๋๋ ์ปดํ์ผ ์๊ฐ์ ํ์ธํ ์ ์์ต๋๋ค.
- ํ ์คํธ ํธ์์ฑ: ํด๋์ค๋ ์ข ์ ํญ๋ชฉ์ ๊ด๋ฆฌํ์ง ์์ผ๋ฏ๋ก ํ ์คํธ ์ ๋ค์ํ ๊ตฌํ์ ์ ๋ฌํ์ฌ ๋ค์ํ ๋ชจ๋ ์ฌ๋ก๋ฅผ ํ ์คํธํ ์ ์์ต๋๋ค.
์ผ๋ก ์ค๋ช ํ๋ค.
Android์ ์ข ์ ํญ๋ชฉ ์ฝ์ | Android ๊ฐ๋ฐ์ | Android Developers
Android์ ์ข ์ ํญ๋ชฉ ์ฝ์ ์ปฌ๋ ์ ์ ์ฌ์ฉํด ์ ๋ฆฌํ๊ธฐ ๋ด ํ๊ฒฝ์ค์ ์ ๊ธฐ์ค์ผ๋ก ์ฝํ ์ธ ๋ฅผ ์ ์ฅํ๊ณ ๋ถ๋ฅํ์ธ์. ์ข ์ ํญ๋ชฉ ์ฝ์ (DI)์ ํ๋ก๊ทธ๋๋ฐ์ ๋๋ฆฌ ์ฌ์ฉ๋๋ ๊ธฐ๋ฒ์ผ๋ก, Android ๊ฐ๋ฐ์ ์ ํฉํฉ๋
developer.android.com
Hilt๋ Dagger๊ฐ ์ ๊ณตํ๋ ์ปดํ์ผ ์๊ฐ ์ ํ์ฑ, ๋ฐํ์ ์ฑ๋ฅ, ํ์ฅ์ฑ ๋ฐ Android ์คํ๋์ค ์ง์์ ์ด์ ์ ๋๋ฆฌ๊ธฐ ์ํด ์ธ๊ธฐ ์๋ DI ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ Dagger๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋น๋๋์์ต๋๋ค.
Hilt๋ฅผ ์ฌ์ฉํ๋ ๊ฐ๋จํ ๋ฐฉ๋ฒ
1. PlugIn ๋ฐ Gradle ์ถ๊ฐ
build.gradle ํ์ผ
plugins {
...
id("com.google.dagger.hilt.android") version "2.44" apply false
}
app/build.gradle
plugins {
...
kotlin("kapt")
id 'dagger.hilt.android.plugin'
}
android {
...
}
dependencies {
implementation("com.google.dagger:hilt-android:2.44")
kapt("com.google.dagger:hilt-android-compiler:2.44")
}
// ์์ฑ๋ ์ฝ๋์ ๋ํ ์ฐธ์กฐ ํ์ฉ
kapt {
correctErrorTypes = true
}
2. Application Class ํ์ผ ์์ฑ
Hilt๋ฅผ ์ฌ์ฉํ๋ ๋ชจ๋ ์ฑ์ @HiltAndroidApp์ผ๋ก ์ฃผ์์ด ์ง์ ๋ Application ํด๋์ค๋ฅผ ํฌํจํด์ผ ํฉ๋๋ค.
@HiltAndroidApp์ ์ ํ๋ฆฌ์ผ์ด์ ์์ค ์ข ์ ํญ๋ชฉ ์ปจํ ์ด๋ ์ญํ ์ ํ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ธฐ๋ณธ ํด๋์ค๋ฅผ ๋น๋กฏํ์ฌ Hilt์ ์ฝ๋ ์์ฑ์ ํธ๋ฆฌ๊ฑฐํฉ๋๋ค.
@HiltAndroidApp
class ExampleApplication : Application() { ... }
์์ฑ๋ ์ด Hilt ๊ตฌ์ฑ์์๋ Application ๊ฐ์ฒด์ ์๋ช ์ฃผ๊ธฐ์ ์ฐ๊ฒฐ๋๋ฉฐ ์ด์ ๊ด๋ จํ ์ข ์ ํญ๋ชฉ์ ์ ๊ณตํฉ๋๋ค. ๋ํ ์ด๋ ์ฑ์ ์์ ๊ตฌ์ฑ์์์ด๋ฏ๋ก ๋ค๋ฅธ ๊ตฌ์ฑ์์๋ ์ด ์์ ๊ตฌ์ฑ์์์์ ์ ๊ณตํ๋ ์ข ์ ํญ๋ชฉ์ ์ก์ธ์คํ ์ ์์ต๋๋ค.
3. Android ํด๋์ค์ ์ข ์ ํญ๋ชฉ ์ฝ์
Application ํด๋์ค์ Hilt๋ฅผ ์ค์ ํ๊ณ ์ ํ๋ฆฌ์ผ์ด์ ์์ค ๊ตฌ์ฑ์์๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ๋๋ฉด Hilt๋ @AndroidEntryPoint ์ฃผ์์ด ์๋ ๋ค๋ฅธ Android ํด๋์ค์ ์ข ์ ํญ๋ชฉ์ ์ ๊ณตํ ์ ์์ต๋๋ค.
@AndroidEntryPoint
class ExampleActivity : AppCompatActivity() { ... }
Hilt๋ ํ์ฌ ๋ค์ Android ํด๋์ค๋ฅผ ์ง์ํฉ๋๋ค.
- Application(@HiltAndroidApp์ ์ฌ์ฉํ์ฌ)
- ViewModel(@HiltViewModel์ ์ฌ์ฉํ์ฌ)
- Activity
- Fragment
- View
- Service
- BroadcastReceiver
4. Class ์์์ ํญ๋ชฉ ์ฌ์ฉํ๊ธฐ
@Qualifier // ๋ป : ์์์ด
@Retention(AnnotationRetention.RUNTIME)
annotation class DebugState
@Module
@InstallIn(SingletonComponent::class)
class SomeModule {
@Provides // ๋ป : ์ ๊ณตํ๋ค.
@Singleton
@DebugState
fun getDebugState(): String {
return if (BuildConfig.DEBUG) DEBUG_STATE else RELEASE_STATE
}
...
}
@AndroidEntryPoint // ๋ป : ์๋๋ก์ด๋ ์ง์
์
class ExampleActivity : AppCompatActivity() {
@Inject // ๋ป : ์ฃผ์
ํ๋ค
@DebugState
lateinit var debugState: String // ์์์
๋๋ค.
...
}
@AndroidEntryPoint๋ ํ๋ก์ ํธ์ ๊ฐ Android ํด๋์ค์ ๊ดํ ๊ฐ๋ณ Hilt ๊ตฌ์ฑ์์๋ฅผ ์์ฑํฉ๋๋ค. ์ด๋ฌํ ๊ตฌ์ฑ์์๋ ๊ตฌ์ฑ์์ ๊ณ์ธต ๊ตฌ์กฐ์ ์ค๋ช ๋ ๋๋ก ํด๋นํ๋ ๊ฐ ์์ ํด๋์ค์์ ์ข ์ ํญ๋ชฉ์ ๋ฐ์ ์ ์์ต๋๋ค.
๊ตฌ์ฑ์์์์ ์ข ์ ํญ๋ชฉ์ ๊ฐ์ ธ์ค๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด @Inject ์ฃผ์์ ์ฌ์ฉํ์ฌ ํ๋ ์ฝ์ ์ ์คํํฉ๋๋ค.
import androidx.activity.viewModels
@AndroidEntryPoint // ๋ป : ์๋๋ก์ด๋ ์ง์
์
class ExampleActivity : AppCompatActivity() {
private val viewModel: ExampleViewModel by viewModels()
...
}
---------------------------------------------------------------
@HiltViewModel // ViewModel์ @HiltViewModel ์ด๋
ธํ
์ด์
์ ์จ์ผํ๋ค.
class ExampleViewModel @Inject constructor(
@DebugState private val debugState: String
) : ViewModel() {
...
}
์์ฒ๋ผ ViewModel์์ ๋ฐ๋ก ์ฌ์ฉํ ๋ ์ฌ์ฉํ ์ ์๋ค.
Activity์์ ๋ณด๋ฉด ViewModel์ ์์ฑํ ๋ ํ๋ผ๋ฏธํฐ๋ฅผ ์ง์ ์ฃผ์ ํด์ฃผ์ง ์์๋ constructor์ ๋ฃ์ด์ ์ฌ์ฉํ ์ ์๋ค.
์ด ๊ธ์ 2023๋ 7์ 9์ผ์ ์์ฑ๋์๊ธฐ์ ๋ฒ์ ํ์ธ ๋ฐ ์ ์ฉ์ ์๋ ๋งํฌ๋ฅผ ํ์ธ ํ ๋์ ํ์๊ธฐ ๋ฐ๋๋๋ค
Hilt๋ฅผ ์ฌ์ฉํ ์ข ์ ํญ๋ชฉ ์ฝ์ | Android ๊ฐ๋ฐ์ | Android Developers
Hilt๋ฅผ ์ฌ์ฉํ ์ข ์ ํญ๋ชฉ ์ฝ์ ์ปฌ๋ ์ ์ ์ฌ์ฉํด ์ ๋ฆฌํ๊ธฐ ๋ด ํ๊ฒฝ์ค์ ์ ๊ธฐ์ค์ผ๋ก ์ฝํ ์ธ ๋ฅผ ์ ์ฅํ๊ณ ๋ถ๋ฅํ์ธ์. Hilt๋ ํ๋ก์ ํธ์์ ์ข ์ ํญ๋ชฉ ์๋ ์ฝ์ ์ ์คํํ๋ ์์ฉ๊ตฌ๋ฅผ ์ค์ด๋ Android์ฉ
developer.android.com
Hilt์์ ์์ฃผ ์ฌ์ฉํ๋ Annotation
- @HiltAndroidApp : ํํธ ์ฝ๋ ์์ฑ์ ์์ํฉ๋๋ค. ์์ฉ ํ๋ก๊ทธ๋จ ํด๋์ค์ ์ฃผ์์ ๋ฌ์์ผ ํฉ๋๋ค.
@HiltAndroidApp
class MyApplication : Application() { ... }
- @AndroidEntryPoint : DI ์ปจํ ์ด๋์ ์ฃผ์์ด ๋ฌ๋ฆฐ Android ํด๋์ค์ DI ์ปจํ ์ด๋๋ฅผ ์ถ๊ฐํฉ๋๋ค. ์ด๋ ๊ฒ ํ๋ ค๋ฉด Hilt์ Gradle ํ๋ฌ๊ทธ์ธ์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
@AndroidEntryPoint
class MyActivity : AppCompatActivity() { ... }
- @Inject :
1. Constructor Injection(์์ฑ์ ์ฃผ์ ). ์ธ์คํด์ค๋ฅผ ์ ๊ณตํ๋ ๋ฐ ์ฌ์ฉํ ์์ฑ์์ ์ ํ์ ํฌํจ๋ ์ข ์์ฑ์ ์ง์ ํฉ๋๋ค.
class AnalyticsAdapter @Inject constructor(
private val service: AnalyticsService
) { ... }
2. Field Injection(ํ์ฅ ์ฃผ์ ). @AndroidEntryPoint ์ฃผ์์ด ๋ฌ๋ฆฐ ํด๋์ค์ ํ๋๋ฅผ ์ฑ์๋๋ค. ํ๋๋ private ์ผ ์ ์์ต๋๋ค.
@AndroidEntryPoint
class MyActivity : AppCompatActivity() {
@Inject lateinit var adapter: AnalyticsAdapter
...
}
- @HiltViewModel : Hilt์๊ฒ Architecture Component View Model์ ์ธ์คํด์ค๋ฅผ ์ ๊ณตํ๋ ๋ฐฉ๋ฒ์ ์๋ ค์ค๋๋ค.
class MyViewModel @Inject constructor(
private val adapter: AnalyticsAdapter,
private val state: SavedStateHandle
): ViewModel() { ... }
- @Module : ์์ฑ์๋ฅผ ์ฃผ์ ํ ์ ์๋ ํ์์ ๋ฐ์ธ๋ฉ์ ์ถ๊ฐํ ์ ์๋ ํด๋์ค์ ๋๋ค.
- @Installn : Hilt ์์ฑ DI ์ปจํ ์ด๋(์ฝ๋์ Singleton Component) ๋ชจ๋ ๋ฐ์ธ๋ฉ์ ์ฌ์ฉํ ์ ์์ด์ผ ํจ์ ๋ํ๋ ๋๋ค.
@InstallIn(SingletonComponent::class)
@Module
class AnalyticsModule { ... }
- @Provides : ์์ฑ์๋ฅผ ์ฃผ์ ํ ์ ์๋ ํ์์ ๋ํ ๋ฐ์ธ๋ฉ์ ์ถ๊ฐํฉ๋๋ค:
- ๋ฐํ ํ์์ ๋ฐ์ธ๋ฉ ํ์์
๋๋ค.
- ๋งค๊ฐ ๋ณ์๋ ์ข
์์ฑ์
๋๋ค.
- ์ธ์คํด์ค๊ฐ ํ์ํ ๋๋ง๋ค
ํจ์ ๋ณธ๋ฌธ์ ํ์ ๋ฒ์๊ฐ ์ง์ ๋์ง ์์ ๊ฒฝ์ฐ ์คํ๋ฉ๋๋ค.
@InstallIn(SingletonComponent::class)
@Module
class AnalyticsModule {
@Provides
fun providesAnalyticsService(
converterFactory: GsonConverterFactory
): AnalyticsService {
return Retrofit.Builder()
.baseUrl("https://example.com")
.addConverterFactory(converterFactory)
.build()
.create(AnalyticsService::class.java)
}
}
- @Binds : ์ธํฐํ์ด์ค ์ ํ์ ๋ฐ์ธ๋ฉํ๊ธฐ ์ํ ๋จ์ถํ:
- Method๋ @Module์ ์์ด์ผ ํฉ๋๋ค.
- @Binds์ Annotation์ด ๋ฌ๋ฆฐ Method๋ abstract class์ด์ฌ์ผ ํฉ๋๋ค.
- ๋ฐํ ํ์์ ๋ฐ์ธ๋ฉ ํ์์
๋๋ค.
- ๋งค๊ฐ ๋ณ์๋ ๊ตฌํ ์ ํ์
๋๋ค.
@InstallIn(SingletonComponent::class)
@Module
abstract class AnalyticsModule {
@Binds
abstract fun bindsAnalyticsService(
analyticsServiceImpl: AnalyticsServiceImpl
): AnalyticsService
}
- Scope Annotations: @Singleton @ActivityScoped ๋ฑ๋ฑ...
์ปจํ
์ด๋์ ๊ฐ์ฒด ๋ฒ์๋ฅผ ์ง์ ํฉ๋๋ค.
ํด๋น ์ ํ์ ์ข
์์ฑ์ผ๋ก ์ฌ์ฉํ๊ฑฐ๋ ํ๋ ์ฃผ์
์ ์ํด ๋๋ ๊ณ์ธต ๊ตฌ์กฐ ์๋์ ์ปจํ
์ด๋์์ ํ์ํ ๊ฒฝ์ฐ ์ปจํ
์ด๋์์ ๋์ผํ ์ ํ ์ธ์คํด์ค๋ฅผ ์ ๊ณตํฉ๋๋ค.
@Singleton
class AnalyticsAdapter @Inject constructor(
private val service: AnalyticsService
) { ... }
- @ApplicationContext @ActivityContext : ๋ฏธ๋ฆฌ ์ ์๋ ๋ฐ์ธ๋ฉ์ ํ์ ์(Qualifiers Annotation)
ํด๋น ์ปจํ ์ด๋์์ ์ข ์์ฑ์ผ๋ก ์ฌ์ฉํ ์ ์๋ ๋ฏธ๋ฆฌ ์ ์๋ ๋ฐ์ธ๋ฉ์ ๋๋ค.
- @Singleton : ์ปดํฌ๋ํธ ์ธ์คํด์ค๊ฐ ์กด์ฌํ ๊ฒฝ์ฐ ํด๋น ์ธ์คํด์ค๊ฐ ํ๋๋ฟ์์ ๋ํ๋ด๋ ์ด๋ ธํ ์ด์ . ApplicationScope(๋ฒ์)๋ฅผ ๊ฐ๋๋ค
@Singleton
class AnalyticsAdapter @Inject constructor(
@ApplicationContext val context: Context
private val service: AnalyticsService
) { ... }
- @Entry Point : Hilt์์ ์ง์ ์ง์ํ์ง ์๊ฑฐ๋ Hilt๋ฅผ ์ฌ์ฉํ ์ ์๋ ํด๋์ค์ ์ข
์์ฑ์ ๊ฐ์ ธ์ต๋๋ค.
@EntryPoint๋ก ์ฃผ์์ด ๋ฌ๋ฆฐ ์ธํฐํ์ด์ค์๋ ํด๋น ์ข
์์ฑ์ ๊ฐ์ ธ์จ Hilt ๋ฏธ๋ฆฌ ์ ์๋ ๊ตฌ์ฑ ์์๋ฅผ ์ ๋ฌํ๋ @InstallIn์ผ๋ก ์ฃผ์์ ๋ฌ์์ผ ํฉ๋๋ค.
DI ์ปจํ
์ด๋(@InstallIn ์ฃผ์์ ๊ฐ๊ณผ ์ผ์น)์ ํญ๋ชฉ์ด ์๋ ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ์ ๋ฌํ๋ EntryPointAccessor์์ ์ ์ ํ ์ ์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ธ๋ฉ์ ์ก์ธ์คํฉ๋๋ค
class MyContentProvider(): ContentProvider {
@InstallIn(SingletonComponent::class)
@EntryPoint
interface MyContentProviderEntryPoint {
fun analyticsService(): AnalyticsService
}
override fun query(...): Cursor {
val appContext = context?.applicationContext ?: throw IllegalStateException()
val entryPoint = EntryPointAccessors.fromApplication(
appContext,
MyContentProviderEntryPoint::class.java
)
val analyticsService = entryPoint.analyticsService()
...
}
}
- @Qualifier : ๋์ผํ ์ ํ(ํ์ )์ด ์ฌ๋ฌ๊ฐ์ผ ๋ ๊ตฌ๋ถํ๊ธฐ ์ํด ์ด๋ฆ์ ์ง์ ํด์ค.
- @Retention : ๋ป -> ๋ณด์ , ์ ์ง ๋ผ๋ ๋ป์ ๊ฐ์ง๊ณ ์๋๋ฐ ์ด ์ด๋ ธํ ์ด์ ์ด ๋ฌ๋ฆฐ ํ์ ์ ์ด๋ ธํ ์ด์ ์ด ์ ์ง๋๋ ๊ธฐ๊ฐ์ ๋ํ๋ธ๋ค. ๊ธฐ๋ณธ๊ฐ์ AnnotationRetention.Runtime์ด๋ค
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class DebugState
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class UtilDebugState
public enum class AnnotationRetention {
/** Annotation isn't stored in binary output */
SOURCE,
/** Annotation is stored in binary output, but invisible for reflection */
BINARY,
/** Annotation is stored in binary output and visible for reflection (default retention) */
RUNTIME
}

๋.
์ฐธ์กฐ.
Hilt๋ฅผ ์ฌ์ฉํ ์ข ์ ํญ๋ชฉ ์ฝ์ | Android ๊ฐ๋ฐ์ | Android Developers
Hilt๋ฅผ ์ฌ์ฉํ ์ข ์ ํญ๋ชฉ ์ฝ์ ์ปฌ๋ ์ ์ ์ฌ์ฉํด ์ ๋ฆฌํ๊ธฐ ๋ด ํ๊ฒฝ์ค์ ์ ๊ธฐ์ค์ผ๋ก ์ฝํ ์ธ ๋ฅผ ์ ์ฅํ๊ณ ๋ถ๋ฅํ์ธ์. Hilt๋ ํ๋ก์ ํธ์์ ์ข ์ ํญ๋ชฉ ์๋ ์ฝ์ ์ ์คํํ๋ ์์ฉ๊ตฌ๋ฅผ ์ค์ด๋ Android์ฉ
developer.android.com
[Android] Hilt์ ๋ํ ๊ณ ์ฐฐ
๊ธ ์ ๋ชฉ์ '๊ณ ์ฐฐ'์ด๋ผ๋ ๋จ์ด๋ฅผ ์จ์ ๊ฑฐ์ฐฝํ๊ฒ ๋ณด์ด๊ฒ ์ง๋ง ์ฌ์ค Hilt๊ฐ ๋ฌด์์ธ์ง, ์ฌ์ฉ๋๋ ๋ช๋ช ์ด๋ ธํ ์ด์ ๊ณผ Hilt๊ฐ ์ด๋ค ๋ฐฉ์์ผ๋ก ์๋ํ๋์ง์ ๋ํด ๊ฐ๋จํ๊ฒ ๊ธฐ๋กํ ๋ฟ์ธ ํฌ์คํ ์ด๋ค. ์๋ "H
onlyfor-me-blog.tistory.com
'Development > Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์๋๋ก์ด๋์์ ์ฝ๋ฃจํด / Coroutine ์จ๋ด ์๋ค - 1 (0) | 2023.07.24 |
---|---|
Thread์ ๊ดํ์ฌ ๋ง ์ ์ด๋ณด๊ธฐ (0) | 2023.04.11 |
๐ ์ฌ์ฉ์๋ค์ด ์ฑ์ ์ ์ ๋ชป ์ฐ๊ฒ ํ ์ ์๋? - Firebase Remote Config (0) | 2022.09.18 |
Databinding ์์ ์ด๋ป๊ฒ ๋ฐ์ดํฐ๋ค์ ๊ฐ๊ณตํ ๊น? ๐ฝ (1) | 2022.09.18 |
๐ฆ Databinding์ ์ฐ๋ฉด์ - 2 (0) | 2022.09.08 |
์ด ๊ธ์์ ๋ฐฐ์ธ์
- ๋ค๋ฅธ DI์์ ์ฐจ์ด์
- Hilt์ ๊ดํ ์ ๋ฐ์ ์ธ ์ง์
- Hilt๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
- Hilt์์ ์์ฃผ ์ฌ์ฉํ๋ Annotation
DI -> Dependency Inject ์์กด์ฑ ์ฃผ์ ์ด๋ผ๊ณ ํ๋ค.
์ด์ ์ Koin์ ๊ดํ ๋ธ๋ก๊ทธ๋ฅผ ์์ฑ ํ์ ๋๋ ๊ณต๋ถ๋ฅผ ํ์์ง๋ง Hilt๋ฅผ ์ข ๋ ๋ง์ด ์์๋ณด๋ ค ๋ค์๊ธ ๊ธ์ ์ด๋ค.
์ด์ ์ ์ผ๋ Koin์ ๊ดํ ๋ธ๋ก๊ทธ
์์กด์ฑ ์ฃผ์ ์ Koin๐ช ํ ๋ข์ - 1
์์กด์ฑ ์ฃผ์ ์ด๋ผ.. ๋๋ฌด๋๋ ์ต์ํ์ง ์์ ๋ฌธ์ฅ์ด๋ค. ๋จ์๊ฒ ์์กดํ์ง๋ง๊ณ ์ฃผ์ฒด์ ์ผ๋ก ์ด์ ๋ผ๋ ๋ง์ ์ฌ์ฉํ ๋๋ง ์ผ๋ ๋จ์ด๋ฅผ ๊ฐ๋ฐํ ๋ ์ฌ์ฉํ๋ ์ด์ํ๊ณ ๋จ๋ฒ์ ์ดํด๊ฐ ๋์ง ์๋๋ค. ์์กด
taekwon-seungwon.tistory.com
์์กด์ฑ ์ฃผ์ ์ Koin๐ช ๋ ๋ข์ - 2
์ด๋ฒ์ ์ค์ ๋ก ํ๋ก๊ทธ๋จ์ ์์กด์ฑ ์ฃผ์ (DI)๋ฅผ ์ ์ฉ์์ผ๋ณด๋ฉด์ ์ฝ์ง์ ์ฐ์์์ ๋๊ผ๋ ์ ๊ณผ ํ์ํ ๋ถ๋ถ์ ์์ฑํ ์์ ์ ๋๋ค. 1. Fragment๋ผ๋ฆฌ ViewModel์ ๊ณต์ ํ๊ณ ์ถ์ ๋! class JoinEntrepreneurFragment:
taekwon-seungwon.tistory.com
๋ค๋ฅธ DI์์ ์ฐจ์ด์
์ด์ ์๋ ์์ฑํ๋ Koin๊ณผ Hilt, Dagger์ ์ฐจ์ด์ ๋ํด ์ ๊น ๋ณด์ฌ๋๋ฆฌ์๋ฉด
Hilt(Jetpack) | Dagger2 | Koin | |
Java | O | O | X |
Kotlin | O | O | O |
๋ฌ๋์ปค๋ธ | ๋ฎ์ | ๋์ | ๋ฎ์ |
์๋ฌ๊ฒ์ถ ์์ | Compile Time | Compile Time | Runtime |
์ค์ ํ๋ก์ ํธ ๊ฒ์ฆ |
๋ฌด๋ ค 2.44 ๋ฒ์ ๊น์ง ์ถ์ |
๋ง์ ์ฑ์์ ๊ฒ์ฆ๋จ | ๋ง์ ์ฑ์์ ๊ฒ์ฆ๋จ |
์ด๋ฐ ์ฐจ์ด๋ฅผ ๋ณด์์๋ค.
ํ์ง๋ง ์ด์ ์ ๋ฌ๋์ปค๋ธ๋ ๋ฎ๊ณ ์ด์๋๊ณ ์๋ ์๋น์ค์ ์ถ๊ฐํด์ผํ๋ ๋ถ๋ถ ๋๋ฌธ์ ๊ฒ์ฆ๋ Koin์ ์ฌ์ฉํ์์ง๋ง
Hilt๊ฐ ์ ์ ๊ณ์ํด์ ์ ๋ฐ์ดํธ๋ ๋๊ณ ๊ฐ์ฅ ๋ง์ ๋๋ ๊ฒ์ Complie ๋จ๊ณ์์ ์๋ฌ๋ฅผ ๊ฒ์ถํด์ฃผ์๊ธฐ ๋๋ฌธ์ ์ฑ ์์ ์ฑ์์๋ ํจ์ฌ ์ข์ ์ ์ ๋ณด์ฌ์ฃผ๊ธฐ ๋๋ฌธ์ ๋์ ๋ฐ ์ฌ์ฉํ๊ณ ์๋ค.
Hilt์ ๊ดํ ์ ๋ฐ์ ์ธ ์ง์
Hilt์ ๋ป์ ์ง์ญํ๋ฉด
๋ช ์ฌ
1.(์นผ) ์๋ฃจ, (๊ณก๊ดญ์ด์) ์๋ฃจ, (๊ถ์ดยท๋จ๋ ๋ฑ์) ์์ก์ด(haft)
๋ก ๋์ด์๋ค. ๊ธฐ๋ฅ์ ์ดํดํ๊ณ ๋ป์ ๋ค์๋ณด๋ฉด ์ Hilt๋ผ๋ ๋จ์ด๋ฅผ ์ฌ์ฉํ๋์ง ์ฝ๊ฒ ์ ์ ์๋ค.
๋จผ์ DI, Hilt๋ฅผ ์ฌ์ฉ์ ์ด์ ์ ์๋ google developer์์ ์ค๋ช ํด์ค ๊ฒ์
- ํด๋์ค ์ฌ์ฌ์ฉ ๊ฐ๋ฅ ๋ฐ ์ข ์ ํญ๋ชฉ ๋ถ๋ฆฌ: ์ข ์ ํญ๋ชฉ ๊ตฌํ์ ์ฝ๊ฒ ๊ต์ฒดํ ์ ์์ต๋๋ค. ์ปจํธ๋กค ๋ฐ์ ์ผ๋ก ์ธํด ์ฝ๋ ์ฌ์ฌ์ฉ์ด ๊ฐ์ ๋์์ผ๋ฉฐ ํด๋์ค๊ฐ ๋ ์ด์ ์ข ์ ํญ๋ชฉ ์์ฑ ๋ฐฉ๋ฒ์ ์ ์ดํ์ง ์์ง๋ง ๋์ ๋ชจ๋ ๊ตฌ์ฑ์์ ์๋ํฉ๋๋ค.
- ๋ฆฌํฉํฐ๋ง ํธ์์ฑ: ์ข ์ ํญ๋ชฉ์ API ๋ ธ์ถ ์์ญ์ ๊ฒ์ฆ ๊ฐ๋ฅํ ์์๊ฐ ๋๋ฏ๋ก ๊ตฌํ ์ธ๋ถ์ ๋ณด๋ก ์จ๊ฒจ์ง์ง ์๊ณ ๊ฐ์ฒด ์์ฑ ์๊ฐ ๋๋ ์ปดํ์ผ ์๊ฐ์ ํ์ธํ ์ ์์ต๋๋ค.
- ํ ์คํธ ํธ์์ฑ: ํด๋์ค๋ ์ข ์ ํญ๋ชฉ์ ๊ด๋ฆฌํ์ง ์์ผ๋ฏ๋ก ํ ์คํธ ์ ๋ค์ํ ๊ตฌํ์ ์ ๋ฌํ์ฌ ๋ค์ํ ๋ชจ๋ ์ฌ๋ก๋ฅผ ํ ์คํธํ ์ ์์ต๋๋ค.
์ผ๋ก ์ค๋ช ํ๋ค.
Android์ ์ข ์ ํญ๋ชฉ ์ฝ์ | Android ๊ฐ๋ฐ์ | Android Developers
Android์ ์ข ์ ํญ๋ชฉ ์ฝ์ ์ปฌ๋ ์ ์ ์ฌ์ฉํด ์ ๋ฆฌํ๊ธฐ ๋ด ํ๊ฒฝ์ค์ ์ ๊ธฐ์ค์ผ๋ก ์ฝํ ์ธ ๋ฅผ ์ ์ฅํ๊ณ ๋ถ๋ฅํ์ธ์. ์ข ์ ํญ๋ชฉ ์ฝ์ (DI)์ ํ๋ก๊ทธ๋๋ฐ์ ๋๋ฆฌ ์ฌ์ฉ๋๋ ๊ธฐ๋ฒ์ผ๋ก, Android ๊ฐ๋ฐ์ ์ ํฉํฉ๋
developer.android.com
Hilt๋ Dagger๊ฐ ์ ๊ณตํ๋ ์ปดํ์ผ ์๊ฐ ์ ํ์ฑ, ๋ฐํ์ ์ฑ๋ฅ, ํ์ฅ์ฑ ๋ฐ Android ์คํ๋์ค ์ง์์ ์ด์ ์ ๋๋ฆฌ๊ธฐ ์ํด ์ธ๊ธฐ ์๋ DI ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ Dagger๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋น๋๋์์ต๋๋ค.
Hilt๋ฅผ ์ฌ์ฉํ๋ ๊ฐ๋จํ ๋ฐฉ๋ฒ
1. PlugIn ๋ฐ Gradle ์ถ๊ฐ
build.gradle ํ์ผ
plugins {
...
id("com.google.dagger.hilt.android") version "2.44" apply false
}
app/build.gradle
plugins {
...
kotlin("kapt")
id 'dagger.hilt.android.plugin'
}
android {
...
}
dependencies {
implementation("com.google.dagger:hilt-android:2.44")
kapt("com.google.dagger:hilt-android-compiler:2.44")
}
// ์์ฑ๋ ์ฝ๋์ ๋ํ ์ฐธ์กฐ ํ์ฉ
kapt {
correctErrorTypes = true
}
2. Application Class ํ์ผ ์์ฑ
Hilt๋ฅผ ์ฌ์ฉํ๋ ๋ชจ๋ ์ฑ์ @HiltAndroidApp์ผ๋ก ์ฃผ์์ด ์ง์ ๋ Application ํด๋์ค๋ฅผ ํฌํจํด์ผ ํฉ๋๋ค.
@HiltAndroidApp์ ์ ํ๋ฆฌ์ผ์ด์ ์์ค ์ข ์ ํญ๋ชฉ ์ปจํ ์ด๋ ์ญํ ์ ํ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ธฐ๋ณธ ํด๋์ค๋ฅผ ๋น๋กฏํ์ฌ Hilt์ ์ฝ๋ ์์ฑ์ ํธ๋ฆฌ๊ฑฐํฉ๋๋ค.
@HiltAndroidApp
class ExampleApplication : Application() { ... }
์์ฑ๋ ์ด Hilt ๊ตฌ์ฑ์์๋ Application ๊ฐ์ฒด์ ์๋ช ์ฃผ๊ธฐ์ ์ฐ๊ฒฐ๋๋ฉฐ ์ด์ ๊ด๋ จํ ์ข ์ ํญ๋ชฉ์ ์ ๊ณตํฉ๋๋ค. ๋ํ ์ด๋ ์ฑ์ ์์ ๊ตฌ์ฑ์์์ด๋ฏ๋ก ๋ค๋ฅธ ๊ตฌ์ฑ์์๋ ์ด ์์ ๊ตฌ์ฑ์์์์ ์ ๊ณตํ๋ ์ข ์ ํญ๋ชฉ์ ์ก์ธ์คํ ์ ์์ต๋๋ค.
3. Android ํด๋์ค์ ์ข ์ ํญ๋ชฉ ์ฝ์
Application ํด๋์ค์ Hilt๋ฅผ ์ค์ ํ๊ณ ์ ํ๋ฆฌ์ผ์ด์ ์์ค ๊ตฌ์ฑ์์๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ๋๋ฉด Hilt๋ @AndroidEntryPoint ์ฃผ์์ด ์๋ ๋ค๋ฅธ Android ํด๋์ค์ ์ข ์ ํญ๋ชฉ์ ์ ๊ณตํ ์ ์์ต๋๋ค.
@AndroidEntryPoint
class ExampleActivity : AppCompatActivity() { ... }
Hilt๋ ํ์ฌ ๋ค์ Android ํด๋์ค๋ฅผ ์ง์ํฉ๋๋ค.
- Application(@HiltAndroidApp์ ์ฌ์ฉํ์ฌ)
- ViewModel(@HiltViewModel์ ์ฌ์ฉํ์ฌ)
- Activity
- Fragment
- View
- Service
- BroadcastReceiver
4. Class ์์์ ํญ๋ชฉ ์ฌ์ฉํ๊ธฐ
@Qualifier // ๋ป : ์์์ด
@Retention(AnnotationRetention.RUNTIME)
annotation class DebugState
@Module
@InstallIn(SingletonComponent::class)
class SomeModule {
@Provides // ๋ป : ์ ๊ณตํ๋ค.
@Singleton
@DebugState
fun getDebugState(): String {
return if (BuildConfig.DEBUG) DEBUG_STATE else RELEASE_STATE
}
...
}
@AndroidEntryPoint // ๋ป : ์๋๋ก์ด๋ ์ง์
์
class ExampleActivity : AppCompatActivity() {
@Inject // ๋ป : ์ฃผ์
ํ๋ค
@DebugState
lateinit var debugState: String // ์์์
๋๋ค.
...
}
@AndroidEntryPoint๋ ํ๋ก์ ํธ์ ๊ฐ Android ํด๋์ค์ ๊ดํ ๊ฐ๋ณ Hilt ๊ตฌ์ฑ์์๋ฅผ ์์ฑํฉ๋๋ค. ์ด๋ฌํ ๊ตฌ์ฑ์์๋ ๊ตฌ์ฑ์์ ๊ณ์ธต ๊ตฌ์กฐ์ ์ค๋ช ๋ ๋๋ก ํด๋นํ๋ ๊ฐ ์์ ํด๋์ค์์ ์ข ์ ํญ๋ชฉ์ ๋ฐ์ ์ ์์ต๋๋ค.
๊ตฌ์ฑ์์์์ ์ข ์ ํญ๋ชฉ์ ๊ฐ์ ธ์ค๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด @Inject ์ฃผ์์ ์ฌ์ฉํ์ฌ ํ๋ ์ฝ์ ์ ์คํํฉ๋๋ค.
import androidx.activity.viewModels
@AndroidEntryPoint // ๋ป : ์๋๋ก์ด๋ ์ง์
์
class ExampleActivity : AppCompatActivity() {
private val viewModel: ExampleViewModel by viewModels()
...
}
---------------------------------------------------------------
@HiltViewModel // ViewModel์ @HiltViewModel ์ด๋
ธํ
์ด์
์ ์จ์ผํ๋ค.
class ExampleViewModel @Inject constructor(
@DebugState private val debugState: String
) : ViewModel() {
...
}
์์ฒ๋ผ ViewModel์์ ๋ฐ๋ก ์ฌ์ฉํ ๋ ์ฌ์ฉํ ์ ์๋ค.
Activity์์ ๋ณด๋ฉด ViewModel์ ์์ฑํ ๋ ํ๋ผ๋ฏธํฐ๋ฅผ ์ง์ ์ฃผ์ ํด์ฃผ์ง ์์๋ constructor์ ๋ฃ์ด์ ์ฌ์ฉํ ์ ์๋ค.
์ด ๊ธ์ 2023๋ 7์ 9์ผ์ ์์ฑ๋์๊ธฐ์ ๋ฒ์ ํ์ธ ๋ฐ ์ ์ฉ์ ์๋ ๋งํฌ๋ฅผ ํ์ธ ํ ๋์ ํ์๊ธฐ ๋ฐ๋๋๋ค
Hilt๋ฅผ ์ฌ์ฉํ ์ข ์ ํญ๋ชฉ ์ฝ์ | Android ๊ฐ๋ฐ์ | Android Developers
Hilt๋ฅผ ์ฌ์ฉํ ์ข ์ ํญ๋ชฉ ์ฝ์ ์ปฌ๋ ์ ์ ์ฌ์ฉํด ์ ๋ฆฌํ๊ธฐ ๋ด ํ๊ฒฝ์ค์ ์ ๊ธฐ์ค์ผ๋ก ์ฝํ ์ธ ๋ฅผ ์ ์ฅํ๊ณ ๋ถ๋ฅํ์ธ์. Hilt๋ ํ๋ก์ ํธ์์ ์ข ์ ํญ๋ชฉ ์๋ ์ฝ์ ์ ์คํํ๋ ์์ฉ๊ตฌ๋ฅผ ์ค์ด๋ Android์ฉ
developer.android.com
Hilt์์ ์์ฃผ ์ฌ์ฉํ๋ Annotation
- @HiltAndroidApp : ํํธ ์ฝ๋ ์์ฑ์ ์์ํฉ๋๋ค. ์์ฉ ํ๋ก๊ทธ๋จ ํด๋์ค์ ์ฃผ์์ ๋ฌ์์ผ ํฉ๋๋ค.
@HiltAndroidApp
class MyApplication : Application() { ... }
- @AndroidEntryPoint : DI ์ปจํ ์ด๋์ ์ฃผ์์ด ๋ฌ๋ฆฐ Android ํด๋์ค์ DI ์ปจํ ์ด๋๋ฅผ ์ถ๊ฐํฉ๋๋ค. ์ด๋ ๊ฒ ํ๋ ค๋ฉด Hilt์ Gradle ํ๋ฌ๊ทธ์ธ์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
@AndroidEntryPoint
class MyActivity : AppCompatActivity() { ... }
- @Inject :
1. Constructor Injection(์์ฑ์ ์ฃผ์ ). ์ธ์คํด์ค๋ฅผ ์ ๊ณตํ๋ ๋ฐ ์ฌ์ฉํ ์์ฑ์์ ์ ํ์ ํฌํจ๋ ์ข ์์ฑ์ ์ง์ ํฉ๋๋ค.
class AnalyticsAdapter @Inject constructor(
private val service: AnalyticsService
) { ... }
2. Field Injection(ํ์ฅ ์ฃผ์ ). @AndroidEntryPoint ์ฃผ์์ด ๋ฌ๋ฆฐ ํด๋์ค์ ํ๋๋ฅผ ์ฑ์๋๋ค. ํ๋๋ private ์ผ ์ ์์ต๋๋ค.
@AndroidEntryPoint
class MyActivity : AppCompatActivity() {
@Inject lateinit var adapter: AnalyticsAdapter
...
}
- @HiltViewModel : Hilt์๊ฒ Architecture Component View Model์ ์ธ์คํด์ค๋ฅผ ์ ๊ณตํ๋ ๋ฐฉ๋ฒ์ ์๋ ค์ค๋๋ค.
class MyViewModel @Inject constructor(
private val adapter: AnalyticsAdapter,
private val state: SavedStateHandle
): ViewModel() { ... }
- @Module : ์์ฑ์๋ฅผ ์ฃผ์ ํ ์ ์๋ ํ์์ ๋ฐ์ธ๋ฉ์ ์ถ๊ฐํ ์ ์๋ ํด๋์ค์ ๋๋ค.
- @Installn : Hilt ์์ฑ DI ์ปจํ ์ด๋(์ฝ๋์ Singleton Component) ๋ชจ๋ ๋ฐ์ธ๋ฉ์ ์ฌ์ฉํ ์ ์์ด์ผ ํจ์ ๋ํ๋ ๋๋ค.
@InstallIn(SingletonComponent::class)
@Module
class AnalyticsModule { ... }
- @Provides : ์์ฑ์๋ฅผ ์ฃผ์ ํ ์ ์๋ ํ์์ ๋ํ ๋ฐ์ธ๋ฉ์ ์ถ๊ฐํฉ๋๋ค:
- ๋ฐํ ํ์์ ๋ฐ์ธ๋ฉ ํ์์
๋๋ค.
- ๋งค๊ฐ ๋ณ์๋ ์ข
์์ฑ์
๋๋ค.
- ์ธ์คํด์ค๊ฐ ํ์ํ ๋๋ง๋ค
ํจ์ ๋ณธ๋ฌธ์ ํ์ ๋ฒ์๊ฐ ์ง์ ๋์ง ์์ ๊ฒฝ์ฐ ์คํ๋ฉ๋๋ค.
@InstallIn(SingletonComponent::class)
@Module
class AnalyticsModule {
@Provides
fun providesAnalyticsService(
converterFactory: GsonConverterFactory
): AnalyticsService {
return Retrofit.Builder()
.baseUrl("https://example.com")
.addConverterFactory(converterFactory)
.build()
.create(AnalyticsService::class.java)
}
}
- @Binds : ์ธํฐํ์ด์ค ์ ํ์ ๋ฐ์ธ๋ฉํ๊ธฐ ์ํ ๋จ์ถํ:
- Method๋ @Module์ ์์ด์ผ ํฉ๋๋ค.
- @Binds์ Annotation์ด ๋ฌ๋ฆฐ Method๋ abstract class์ด์ฌ์ผ ํฉ๋๋ค.
- ๋ฐํ ํ์์ ๋ฐ์ธ๋ฉ ํ์์
๋๋ค.
- ๋งค๊ฐ ๋ณ์๋ ๊ตฌํ ์ ํ์
๋๋ค.
@InstallIn(SingletonComponent::class)
@Module
abstract class AnalyticsModule {
@Binds
abstract fun bindsAnalyticsService(
analyticsServiceImpl: AnalyticsServiceImpl
): AnalyticsService
}
- Scope Annotations: @Singleton @ActivityScoped ๋ฑ๋ฑ...
์ปจํ
์ด๋์ ๊ฐ์ฒด ๋ฒ์๋ฅผ ์ง์ ํฉ๋๋ค.
ํด๋น ์ ํ์ ์ข
์์ฑ์ผ๋ก ์ฌ์ฉํ๊ฑฐ๋ ํ๋ ์ฃผ์
์ ์ํด ๋๋ ๊ณ์ธต ๊ตฌ์กฐ ์๋์ ์ปจํ
์ด๋์์ ํ์ํ ๊ฒฝ์ฐ ์ปจํ
์ด๋์์ ๋์ผํ ์ ํ ์ธ์คํด์ค๋ฅผ ์ ๊ณตํฉ๋๋ค.
@Singleton
class AnalyticsAdapter @Inject constructor(
private val service: AnalyticsService
) { ... }
- @ApplicationContext @ActivityContext : ๋ฏธ๋ฆฌ ์ ์๋ ๋ฐ์ธ๋ฉ์ ํ์ ์(Qualifiers Annotation)
ํด๋น ์ปจํ ์ด๋์์ ์ข ์์ฑ์ผ๋ก ์ฌ์ฉํ ์ ์๋ ๋ฏธ๋ฆฌ ์ ์๋ ๋ฐ์ธ๋ฉ์ ๋๋ค.
- @Singleton : ์ปดํฌ๋ํธ ์ธ์คํด์ค๊ฐ ์กด์ฌํ ๊ฒฝ์ฐ ํด๋น ์ธ์คํด์ค๊ฐ ํ๋๋ฟ์์ ๋ํ๋ด๋ ์ด๋ ธํ ์ด์ . ApplicationScope(๋ฒ์)๋ฅผ ๊ฐ๋๋ค
@Singleton
class AnalyticsAdapter @Inject constructor(
@ApplicationContext val context: Context
private val service: AnalyticsService
) { ... }
- @Entry Point : Hilt์์ ์ง์ ์ง์ํ์ง ์๊ฑฐ๋ Hilt๋ฅผ ์ฌ์ฉํ ์ ์๋ ํด๋์ค์ ์ข
์์ฑ์ ๊ฐ์ ธ์ต๋๋ค.
@EntryPoint๋ก ์ฃผ์์ด ๋ฌ๋ฆฐ ์ธํฐํ์ด์ค์๋ ํด๋น ์ข
์์ฑ์ ๊ฐ์ ธ์จ Hilt ๋ฏธ๋ฆฌ ์ ์๋ ๊ตฌ์ฑ ์์๋ฅผ ์ ๋ฌํ๋ @InstallIn์ผ๋ก ์ฃผ์์ ๋ฌ์์ผ ํฉ๋๋ค.
DI ์ปจํ
์ด๋(@InstallIn ์ฃผ์์ ๊ฐ๊ณผ ์ผ์น)์ ํญ๋ชฉ์ด ์๋ ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ์ ๋ฌํ๋ EntryPointAccessor์์ ์ ์ ํ ์ ์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ธ๋ฉ์ ์ก์ธ์คํฉ๋๋ค
class MyContentProvider(): ContentProvider {
@InstallIn(SingletonComponent::class)
@EntryPoint
interface MyContentProviderEntryPoint {
fun analyticsService(): AnalyticsService
}
override fun query(...): Cursor {
val appContext = context?.applicationContext ?: throw IllegalStateException()
val entryPoint = EntryPointAccessors.fromApplication(
appContext,
MyContentProviderEntryPoint::class.java
)
val analyticsService = entryPoint.analyticsService()
...
}
}
- @Qualifier : ๋์ผํ ์ ํ(ํ์ )์ด ์ฌ๋ฌ๊ฐ์ผ ๋ ๊ตฌ๋ถํ๊ธฐ ์ํด ์ด๋ฆ์ ์ง์ ํด์ค.
- @Retention : ๋ป -> ๋ณด์ , ์ ์ง ๋ผ๋ ๋ป์ ๊ฐ์ง๊ณ ์๋๋ฐ ์ด ์ด๋ ธํ ์ด์ ์ด ๋ฌ๋ฆฐ ํ์ ์ ์ด๋ ธํ ์ด์ ์ด ์ ์ง๋๋ ๊ธฐ๊ฐ์ ๋ํ๋ธ๋ค. ๊ธฐ๋ณธ๊ฐ์ AnnotationRetention.Runtime์ด๋ค
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class DebugState
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class UtilDebugState
public enum class AnnotationRetention {
/** Annotation isn't stored in binary output */
SOURCE,
/** Annotation is stored in binary output, but invisible for reflection */
BINARY,
/** Annotation is stored in binary output and visible for reflection (default retention) */
RUNTIME
}

๋.
์ฐธ์กฐ.
Hilt๋ฅผ ์ฌ์ฉํ ์ข ์ ํญ๋ชฉ ์ฝ์ | Android ๊ฐ๋ฐ์ | Android Developers
Hilt๋ฅผ ์ฌ์ฉํ ์ข ์ ํญ๋ชฉ ์ฝ์ ์ปฌ๋ ์ ์ ์ฌ์ฉํด ์ ๋ฆฌํ๊ธฐ ๋ด ํ๊ฒฝ์ค์ ์ ๊ธฐ์ค์ผ๋ก ์ฝํ ์ธ ๋ฅผ ์ ์ฅํ๊ณ ๋ถ๋ฅํ์ธ์. Hilt๋ ํ๋ก์ ํธ์์ ์ข ์ ํญ๋ชฉ ์๋ ์ฝ์ ์ ์คํํ๋ ์์ฉ๊ตฌ๋ฅผ ์ค์ด๋ Android์ฉ
developer.android.com
[Android] Hilt์ ๋ํ ๊ณ ์ฐฐ
๊ธ ์ ๋ชฉ์ '๊ณ ์ฐฐ'์ด๋ผ๋ ๋จ์ด๋ฅผ ์จ์ ๊ฑฐ์ฐฝํ๊ฒ ๋ณด์ด๊ฒ ์ง๋ง ์ฌ์ค Hilt๊ฐ ๋ฌด์์ธ์ง, ์ฌ์ฉ๋๋ ๋ช๋ช ์ด๋ ธํ ์ด์ ๊ณผ Hilt๊ฐ ์ด๋ค ๋ฐฉ์์ผ๋ก ์๋ํ๋์ง์ ๋ํด ๊ฐ๋จํ๊ฒ ๊ธฐ๋กํ ๋ฟ์ธ ํฌ์คํ ์ด๋ค. ์๋ "H
onlyfor-me-blog.tistory.com
'Development > Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์๋๋ก์ด๋์์ ์ฝ๋ฃจํด / Coroutine ์จ๋ด ์๋ค - 1 (0) | 2023.07.24 |
---|---|
Thread์ ๊ดํ์ฌ ๋ง ์ ์ด๋ณด๊ธฐ (0) | 2023.04.11 |
๐ ์ฌ์ฉ์๋ค์ด ์ฑ์ ์ ์ ๋ชป ์ฐ๊ฒ ํ ์ ์๋? - Firebase Remote Config (0) | 2022.09.18 |
Databinding ์์ ์ด๋ป๊ฒ ๋ฐ์ดํฐ๋ค์ ๊ฐ๊ณตํ ๊น? ๐ฝ (1) | 2022.09.18 |
๐ฆ Databinding์ ์ฐ๋ฉด์ - 2 (0) | 2022.09.08 |