본문 바로가기
Android/개념 및 정보

[Android] Hilt 의존성 주입(DI) gradle 버전, 각 어노테이션 설명

by Taehyung Kim, dev 2020. 12. 11.
728x90

gradle.build(project)

buildscript {
    ext.kotlin_version = '1.3.72'
    ext.hilt_version = '2.28-alpha'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
    }
}

gradle.build(module)

.
.
.
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
.
.
.
dependencies {
    ...
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
}

 

  • @HiltAndroidApp
    • 종속성 주입을 사용할 수 있는 application 의 basic class 를 포함하여 Hilt 의 코드 생성을 트리거한다.
    • application container 는 앱의 상위 container 이므로 다른 container 가 제공하는 종속성에 접근할 수 있다.

 

  • @AndroidEntryPoint
    • 위 어노테이션을 설정한 클래스는 Android class lifecycle 을 따르는 종속 container 를 생성한다.
    • @Inject 를 사용하기 위해서 클래스에 설정해야한다.

 

  • @Inject
    • 위 어노테이션을 통해 DI 를 할 수 있다.
    • filed injection 이라고 한다.
    • DI 된 field 는 private 을 가질 수 없다.
    • @Inject 만 진행한다고 해서 DI 가 진행된 것이 아니다.
      주입하고자 하는 것을 Hilt 에게 알려야한다.

 

  • @SingleTon
    • 항상 동일한 인스턴스를 제공할 때 사용하는 어노테이션
    • 만약 항상 동일한 인스턴스를 제공하는 activity container 를 원한다면 @ActivityScoped 어노테이션을 지정할 수 있다.

 

  • @Module, @InstallIn
    • @InstallIn 은 Hilt 의 Component 를 직접 지정하여 바인딩 할 수 있는 Container 를 알려줍니다.
      Hilt 의 구성요소는 아래에 있습니다.
    • @Module 은 Module 이라고 알려줍니다.
    • Hilt 에 바인딩을 추가하거나 다른 유형의 인스턴스를 제공하는 방법을 알려주는데 사용됩니다.
    • Interface 혹은 생성자가 주입할 수 없는 유형의 바인딩을 포함합니다.

 

  • @Binds
    • Interface 타입을 가지고 있는 변수에 DI 를 하기위해 사용한다.
    • 즉, Hilt 에게 implementation 한 Interface @Binds 어노테이션을 이용하여 알려줄 수 있다.
    • @Binds 는 abstract 함수에 지정해야 한다. Return 타입은 implementaion 한 interface.
    • @Binds 와 @Provides 어노테이션을 같은 클래스에 명시할 수 없다.
    • type 이 지정인 경우 Scope 어노테이션을 가져야 합니다.

 

  • @Qualifier
    • multiple bindings 으로 같은 유형을 implementation 한 경우 해당 어노테이션으로 모호함을 식별하는데에 사용한다.

 

  • @EntryPoint
    • Hilt 에서 지원하지 않는 클래스에 종속성을 주입합니다.

 

 

 

Hilt component

Hilt component Injector for
ApplicationComponent Application
ActivityRetainedComponent ViewModel
ActivityComponent Activity
FragmentComponent Fragment
ViewComponent View
ViewWithFragmentComponent View annotated with @WithFragmentBindings
ServiceComponent Service

 

 

Default Binding Componet

Android Component Defaut Bindings
ApplicationComponent Application
ActivityRetainedComponent Application
ActivityComponent Application, Activity
FragmentComponent ApplicationActivity, Fragment
ViewComponent ApplicationActivity, View
ViewWithFragmentComponent ApplicationActivity, Fragment, View
ServiceComponent Application, Service

 

 

Component hierarchy

 

 

 

Component scopes

Android class Generated component Scope
Application ApplicationComponent @SingleTon
View Model ActivityRetainedComponent @ActivityRetainedScope
Activity ActivityComponent @ActivityScoped
Fragment FragmentComponent @FragmentScoped
View ViewComponent @ViewScoped
View annotated with @WithFragmentBindings ViewWithFragmentComponent @ViewScoped
Service ServiceComponent @ServiceScoped

 

728x90

댓글