Hilt 사용 이유 → 2025.08.12 - [안드로이드] - [Android] Hilt- Hilt를 사용하는 이유 (1)
Hilt 프로젝트 세팅, 기본 흐름 → 2025.08.15 - [안드로이드] - [Android] Hilt- 프로젝트 세팅, Hilt 기본 흐름 (2)
Hilt의 Component, Scope와 Component의 계층 구조에 대해 알아보겠다.
Component
Hilt의 Component는 의존성을 어디에 주입할지 결정하는 역할을 한다.
Component 종류는 다음과 같다.
| Hilt Component | 의존성 주입 대상 | 생성 시기 | 소멸 시기 |
| SingletonComponent | Application | Application#onCreate() | Application 소멸됨 |
| ActivityRetainedComponent | N/A | Activity#onCreate() | Activity#onDestroy() |
| ViewModelComponent | ViewModel | ViewModel 생성됨 | ViewModel 소멸됨 |
| ActivityComponent | Activity | Activity#onCreate() | Activity#onDestroy() |
| FragmentComponent | Fragment | Fragment#onAttach() | Fragment#onDestroy() |
| ViewComponent | View | View#super() | View 소멸됨 |
| ViewWithFragmentComponent | @WithFragmentBindings 주석이 지정된 View | View#super() | View 소멸됨 |
| ServiceComponent | Service | Service#onCreate() | Service#onDestroy() |
해당 Component들은 Hilt 모듈의 @InstallIn 주석에서 활용된다.
SingletonComponent가 지정된 모듈은
Application 범위에서 제공되는 의존성 제공 함수를 "Application 생명주기 동안" 사용할 수 있다.
Component Scope
의존성을 사용하고자 생성자 또는 필드를 삽입할 때 제공되는 인스턴스는 기본적으로 범위가 제공되지 않는다.
따라서 의존성을 주입할 때마다 새로운 클래스 인스턴스가 생성되어 제공된다.
하지만 Component에 명시적으로 범위를 지정하면, 해당 범위 생명주기 동안 동일한 인스턴스를 제공할 수 있다.
| Component | Scope | 클래스 |
| SingletonComponent | @Singleton | Application |
| ActivityRetainedComponent | @ActivityRetainedScoped | Activity |
| ViewModelComponent | @ViewModelScoped | ViewModel |
| ActivityComponent | @ActivityScoped | Activity |
| FragmentComponent | @FragmentScoped | Fragment |
| ViewComponent | @ViewScoped | View |
| ViewWithFragmentComponent | @ViewScoped | @WithFragmentBindings 주석이 지정된 View |
| ServiceComponent | @ServiceScoped | Service |
다음 예시를 살펴보자.
@ActivityScoped
class AnalyticsAdapter @Inject constructor(
private val service: AnalyticsService
) { ... }
AnalyticsAdapter는 현재 @ActivityScoped 어노테이션을 가지고 있으므로,
Activity의 생명주기 동안 동일한 AnalyticsAdapter 인스턴스를 제공한다.
따라서 Activity에서 생성자 삽입 또는 필드 삽입으로 AnalyticsAdapter 인스턴스를 사용할 수 있다.
Scope 어노테이션 말고도,
Module에 특정 Component를 InstallIn 하면 Component의 Scope에 맞게 인스턴스를 사용할 수 있다.
Component 계층 구조
❓ @ActivityScoped가 지정된 클래스는 Activity 범위에서만 사용해야 할까?
❗️답은 ❌ 이다.
@ActivityScoped는 ActivityComponent에서 제공되며, 하위 컴포넌트에서도 의존성을 주입받아 사용할 수 있다.
Component 계층 구조는 다음과 같다.

Component 계층 구조에 따라 상위 Scope는 의존성을 제공하고, 하위 Component는 의존성을 주입받는다.
따라서 ActivityComponent는 @Singleton, @ActivityRetainedScoped, @ActivityScoped 세 가지 범위의 의존성을 모두 주입받아 쓸 수 있다.
Component의 범위에 따라 주입받아 사용할 수 있는 의존성 객체가 달라진다.
아울러 객체(인스턴스)는 생명 주기동안 일관된 상태로 관리됨을 잊지 말자!
:)
출처
https://medium.com/androiddevelopers/scoping-in-android-and-hilt-c2e5222317c0
https://developer.android.com/training/dependency-injection/hilt-android?hl=ko#kts
'Android' 카테고리의 다른 글
| [Android] Hilt- 프로젝트 세팅, Hilt 기본 흐름 (2) (5) | 2025.08.15 |
|---|---|
| [Android] Hilt- Hilt를 사용하는 이유 (1) (1) | 2025.08.12 |
| [Android] 클린 아키텍처(Clean Architecture) (2) | 2025.08.11 |
| [Android] Work Manager(+Job Scheduler) (2) | 2025.08.09 |
| [Android] 안드로이드 권장 아키텍처 (4) | 2025.07.31 |