폴더구조는 아래와 같습니다.

grdle에 라이브러리 넣어주고
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plugins { | |
id 'com.android.application' | |
id 'kotlin-android' | |
id 'kotlin-kapt' | |
id 'dagger.hilt.android.plugin' | |
id 'kotlin-android-extensions' | |
} | |
android { | |
compileSdkVersion 30 | |
buildToolsVersion "30.0.3" | |
defaultConfig { | |
applicationId "com.bokchi.recyclerviewmvvmgithubretrofit" | |
minSdkVersion 16 | |
targetSdkVersion 30 | |
versionCode 1 | |
versionName "1.0" | |
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | |
} | |
} | |
compileOptions { | |
sourceCompatibility JavaVersion.VERSION_1_8 | |
targetCompatibility JavaVersion.VERSION_1_8 | |
} | |
kotlinOptions { | |
jvmTarget = '1.8' | |
} | |
} | |
dependencies { | |
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | |
implementation 'androidx.core:core-ktx:1.3.2' | |
implementation 'androidx.appcompat:appcompat:1.2.0' | |
implementation 'com.google.android.material:material:1.3.0' | |
implementation 'androidx.constraintlayout:constraintlayout:2.0.4' | |
implementation 'com.google.dagger:hilt-android:2.33-beta' | |
implementation 'com.squareup.retrofit2:retrofit:2.9.0' | |
implementation 'com.squareup.retrofit2:converter-gson:2.9.0' | |
implementation 'com.github.bumptech.glide:glide:4.12.0' | |
kapt 'com.google.dagger:hilt-android-compiler:2.33-beta' | |
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' | |
testImplementation 'junit:junit:4.+' | |
androidTestImplementation 'androidx.test.ext:junit:1.1.2' | |
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' | |
} |
이 부분에 에러가 나는데, kotlin_version이 없으면 에러가 납니다.

Menifest에 넣어주고

AppModule 만들어주고


Retrofit 연결하고 api 호출부분 만들어줌 inject로 의졵성을 주입해줬습니다.


나머지는 뭐 다 당연한 recyclerView입니다.




여기까지 해보면 드는 의문이 아니 그런데 Hilt가 없어도 그대로 동작하는거 아냐? 라는 생각이 듭니다.
도대체 왜쓰는거야? 를 알아보기 위해서 Hilt부분을 지워봤습니다.
똑같이 Hilt부분을 제거하니
Cannot create an instance of class
라고 나옵니다.
Hilt 의존성 주입이 실패했다고 나오는데, 어디에서 어떻게 잘못되었는지 살펴볼까요?
에러를 찾아보니 ViewModel에서 repository를 정의했을 때 문제가 생깁니다.

이거 없이도 가능할까? 해서 해보니
이걸 Hilt없이 할려고 하니, Api를 호출할 때 마다 RetrofitInstace만들어줘야 하고 설정을 이것저것 해줘야 하네요
결론은 좀 더 Hilt를 쓰면 코드가 간편해집니다.
- 참조
https://www.youtube.com/watch?v=8EoGVJK0sj0
'Android(Kotlin)' 카테고리의 다른 글
fragment dialog (0) | 2021.08.31 |
---|---|
Android muti module (0) | 2021.08.29 |
Google Android Hilt + Room Ex (0) | 2021.08.28 |
paging Room Hilt Simple EX (0) | 2021.08.28 |
Android Interface abstract class 차이 (0) | 2021.08.27 |