Search code examples
androiddagger-hilt

How do you correctly add dependencies for Dagger Hilt in Android Studio Iguana?


I tried to add the dependencies for Dagger Hilt, but I can't seem to add them correctly. Everywhere I've looked, I only found instructions for adding them with the old dependency system.

I tried adding them using the old method, but it doesn't seem to work. I am beginner on developing android apps and will really help if someone know how to do that :)


Solution

  • This is how I add (hilt) dependencies in Android Studio Iguana/Jellyfish using version catalogs:

    In your app level build.gradle.kts plugin section:

    plugins {
        alias(libs.plugins.androidApplication)
        alias(libs.plugins.jetbrainsKotlinAndroid)
        alias(libs.plugins.jetbrainsKotlinKsp)
        alias(libs.plugins.hiltPlugin)
    }
    

    dependencies section:

    dependencies {
        implementation(libs.androidx.core.ktx)
        implementation(libs.androidx.appcompat)
        implementation(libs.androidx.lifecycle.runtime.ktx)
        implementation(libs.androidx.activity.compose)
    
        //Hilt
        implementation (libs.hilt.android)
        ksp(libs.dagger.compiler)
        ksp(libs.hilt.compiler)
    
        testImplementation(libs.junit)
        androidTestImplementation(libs.androidx.junit)
        androidTestImplementation(libs.androidx.espresso.core)
        androidTestImplementation(libs.androidx.ui.test.junit4)
        debugImplementation(libs.androidx.ui.tooling)
        debugImplementation(libs.androidx.ui.test.manifest)
    }
    

    In your project level build.gradle.kts:

    plugins {
        alias(libs.plugins.androidApplication) apply false
        alias(libs.plugins.jetbrainsKotlinAndroid) apply false
        alias(libs.plugins.jetbrainsKotlinKsp) apply false
        alias(libs.plugins.hiltPlugin) apply false
    }
    

    In your libs.versions.toml file:

    [versions]
    agp = "8.3.2"
    appcompat = "1.6.1"
    hiltAndroid = "2.51"
    kotlin = "1.9.0"
    kotlinKsp = "1.9.0-1.0.12"
    coreKtx = "1.13.1"
    junit = "4.13.2"
    junitVersion = "1.1.5"
    espressoCore = "3.5.1"
    lifecycleRuntimeKtx = "2.8.0"
    activityCompose = "1.9.0"
    
    [libraries]
    androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
    androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
    
    dagger-compiler = { module = "com.google.dagger:dagger-compiler", version.ref = "hiltAndroid" }
    hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hiltAndroid" }
    hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "hiltAndroid" }
    
    junit = { group = "junit", name = "junit", version.ref = "junit" }
    androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
    androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
    androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
    androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
    
    [plugins]
    androidApplication = { id = "com.android.application", version.ref = "agp" }
    jetbrainsKotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
    jetbrainsKotlinKsp = { id = "com.google.devtools.ksp", version.ref = "kotlinKsp" }
    hiltPlugin = { id = "com.google.dagger.hilt.android", version.ref = "hiltAndroid" }
    

    Ref: Gradle Build Setup

    Hope that helps!