티스토리 뷰
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 어노테이션 종류
https://developer.android.com/training/dependency-injection/hilt-android?hl=ko#component-hierarchy
https://hyperconnect.github.io/2020/07/28/android-dagger-hilt.html
'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
링크
TAG
- View
- XML
- 안드로이드 #코틀린 #Android #Kotlin
- MVVM
- 코틀린
- handler
- fragment
- activity
- JSONObject
- ArrayList
- ViewModel
- 혀가 길지 않은 개발자
- ViewPager2
- java
- Architecture Pattern
- coroutine
- JSONArray
- 안드로이드
- 자바
- CoordinatorLayout
- Kotlin
- Design Pattern
- Intent
- James Kim
- Livedata
- Android
- DataBinding
- recyclerview
- Vue.js #Vue.js + javascript
- TabLayout
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함