티스토리 뷰

Android/Kotlin

[Kotlin] Hilt

혀가 길지 않은 개발자 2021. 5. 23. 21:26

Hilt


프로젝트 build.gradle 에 dependency 추가

buildscript {
    ...
    dependencies {
        ...
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'
    }
}

 

 

 

app 단에 build.gradle에 dependency 추가

...
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'

android {
    ...
}

dependencies {
    implementation "com.google.dagger:hilt-android:2.28-alpha"
    kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
}

자바 1.8 버전 아니면 추가

android {
  ...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

Application 단에 @HiltAndroidApp 추가

package com.example.sunflowerexample

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class MainApplication : Application()

매니페스트에 MainApplication 추가

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sunflowerexample">

    <application
        android:name=".MainApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.SunflowerExample">
        <activity android:name=".GardenActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 


Activity에 @AndroidEntryPoint 추가

package com.example.sunflowerexample

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil.setContentView
import com.example.sunflowerexample.databinding.ActivityGardenBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class GardenActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView<ActivityGardenBinding>(this, R.layout.activity_garden)
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

참고

https://developer.android.com/training/dependency-injection/hilt-android?hl=ko 

 

Hilt를 사용한 종속 항목 삽입  |  Android 개발자  |  Android Developers

Hilt는 프로젝트에서 수동 종속 항목 삽입을 실행하는 상용구를 줄이는 Android용 종속 항목 삽입 라이브러리입니다. 수동 종속 항목 삽입을 실행하려면 모든 클래스와 종속 항목을 수동으로 구성

developer.android.com

Hilt 어노테이션 종류

https://developer.android.com/training/dependency-injection/hilt-android?hl=ko#component-hierarchy 

 

Hilt를 사용한 종속 항목 삽입  |  Android 개발자  |  Android Developers

Hilt는 프로젝트에서 수동 종속 항목 삽입을 실행하는 상용구를 줄이는 Android용 종속 항목 삽입 라이브러리입니다. 수동 종속 항목 삽입을 실행하려면 모든 클래스와 종속 항목을 수동으로 구성

developer.android.com

https://hyperconnect.github.io/2020/07/28/android-dagger-hilt.html

 

Dagger Hilt로 안드로이드 의존성 주입 시작하기

Dagger Hilt에 대해 알아보고 안드로이드 프로젝트에 적용하는 방법을 소개합니다.

hyperconnect.github.io

 

'Android > Kotlin' 카테고리의 다른 글

[Kotlin] CollapsingToolbarLayout  (0) 2021.05.24
[Kotlin] Navigation  (0) 2021.05.23
[Kotlin] Compose  (0) 2021.05.22
[Kotlin] SpannableStringBuilder  (0) 2021.05.22
[Kotlin] Service  (0) 2021.05.02
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함