Sam Story

2024-08-07 Dagger,Hilt에 관한 공부 기록 본문

공부기록

2024-08-07 Dagger,Hilt에 관한 공부 기록

Sam H 2024. 8. 7. 19:59

오늘은 Dagger,Hilt에 대한 기술 세미나를 위해서 대거 힐트에 대해서 공부 했다.

 

의존성 주입에 대한 정의를 간단하게 이해하고 바로 예제를 만들어서 실전에 어떻게 사용할지

공부를 하려 했는데 

 

새로운 프로젝트를 만들고 코드를 작성 후 build를 해보니 jvm 문제가 자꾸 발생했다.

일단 기존프로젝트들의 경우 java 1.8 버전을 기준으로 진행해 왔었는데 프로젝트 문제인지

의존성 추가할때 라이브러리와 충돌이 나는건지 계속 에러가 발생했다.

 

대표적으로 발생한 에러는 

 

1. gradle toolChain 관련 에러

 

에러문구에는 현재 적용되어 있는건 8버전인데

사용하려하는 버전은 17버전이다. 라는 내용의 에러였다.

 

이를 해결하기 위해서 여러가지 내용들을 바꿔봤었는데

최종적으로 수정된 코드는 아래와 같다.

 

최종 수정된 build.gradle

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
}

android {
    namespace 'com.example.daggerexam'
    compileSdk 34

    defaultConfig {
        applicationId "com.example.daggerexam"
        minSdk 25
        targetSdk 34
        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_17
        targetCompatibility JavaVersion.VERSION_17

    }

    kotlinOptions {
        jvmTarget = '17'
    }

    java {
        toolchain {
            languageVersion = JavaLanguageVersion.of(17)
        }
    }

}

dependencies {
    implementation 'androidx.core:core-ktx:1.8.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.2.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'

    // Dagger
    implementation 'com.google.dagger:dagger:2.50'
    kapt 'com.google.dagger:dagger-compiler:2.50'
}

 

 

toolChain이 무엇인가 하고 좀 찾아보니 로컬 jdk환경이 다들 다르니

gradle script에서 자바버전을 확인하기 위해 사용된다 라는 내용이 있다고 한다.

 

시간이 된다면 좀 더 자세히 공부해 봐야겠다.

 

 

 

2. Hilt plugin 에러

* Exception is:
org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'dagger.hilt.android.plugin'] was not found in any of the following sources:

 

build.gradle에 Hilt 의존성 추가를 해줬는데 이런 에러가 발생했다.

 

해결 자체는 다른 사람의 블로그에 있는 코드를 추가해 줌으로써 간단하게 해결했다.

먼저 build.gradle의 경우는 Dagger 와 거의 흡사하지만 plugins에 hilt 플러그인이 추가된걸 볼 수 있다.

 

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt' // Kotlin Kapt 플러그인 적용
    id 'dagger.hilt.android.plugin'

}

android {
    namespace 'com.example.hiltexam'
    compileSdk 34

    defaultConfig {
        applicationId "com.example.hiltexam"
        minSdk 25
        targetSdk 34
        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_17
        targetCompatibility JavaVersion.VERSION_17

    }

    kotlinOptions {
        jvmTarget = '17'
    }

    java {
        toolchain {
            languageVersion = JavaLanguageVersion.of(17)
        }
    }

}

dependencies {

    implementation 'androidx.core:core-ktx:1.8.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.2.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'

    // Hilt
    implementation "com.google.dagger:hilt-android:2.50"
    kapt "com.google.dagger:hilt-android-compiler:2.50"
}

 

 

그리고 블로그에서 본 내용으로 해결한 방법은

settings.gradle에 코드를 추가해 주는것인데

 

추가해준 코드를 보게되면

pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
    
    // 문제 수정을위해 추가한 코드
    resolutionStrategy {
        eachPlugin {
            if( requested.id.id == 'dagger.hilt.android.plugin') {
                useModule("com.google.dagger:hilt-android-gradle-plugin:2.39.1")
            }
        }
    }
    
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "HiltExam"
include ':app'

 

 

 

이러한 문제들이 지난번 gradle 설정으로 인해 발생한건지 라이브러리 의존성 추가할 때 충돌이 나서 그런건지

좀 더 여러가지 방면으로 확인해 봐야 할 것 같다.