Issues with Dagger Hilt and KSP in Android Kotlin Project: Cannot Resolve Symbol ‘Inject’

6.1k views Asked by At

I’m working on an Android project with Kotlin and using Dagger Hilt for dependency injection. Recently, I’ve attempted to migrate from kapt to KSP (Kotlin Symbol Processing) for code generation, as kapt is now in “maintenance mode”. However, I’ve run into an issue: Dagger Hilt annotations such as @Inject, @Binds, and @Module are not being detected by KSP, and I’m getting a “Cannot resolve symbol” error.

I was testing the connection to Supabase using the “Framework Quickstarts: Use Supabase with Android Kotlin” documentation, where you can find the code. My build.gradle.kts(module):

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android") version "1.9.20"
    id ("com.google.relay") version "0.3.10"
    kotlin("plugin.serialization") version "1.9.0"
    id ("com.google.devtools.ksp") version "1.9.20-1.0.14"
}

android {
    namespace = "xxxxxxxxxxx"
    compileSdk = 34

    defaultConfig {
        applicationId = "xxxxxxxxxxxx"
        minSdk = 24
        targetSdk = 33
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            isMinifyEnabled = 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("androidx.core:core-ktx:1.12.0")
    implementation("androidx.appcompat:appcompat:1.6.1")
    implementation("com.google.android.material:material:1.10.0")
    implementation(platform("io.github.jan-tennert.supabase:bom:1.4.6-dev-1"))
    implementation("io.github.jan-tennert.supabase:postgrest-kt")
    implementation("io.github.jan-tennert.supabase:realtime-kt")
    implementation("io.github.jan-tennert.supabase:storage-kt")
    implementation("io.github.jan-tennert.supabase:functions-kt")
    implementation("io.github.jan-tennert.supabase:gotrue-kt")
    implementation("io.github.jan-tennert.supabase:compose-auth")
    implementation("io.github.jan-tennert.supabase:compose-auth-ui")
    implementation("io.ktor:ktor-client-cio:2.3.5")
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
    ksp("com.google.dagger:dagger-compiler:2.48.1") // Dagger compiler
    ksp("com.google.dagger:hilt-compiler:2.48.1")   // Hilt compiler
    ksp("androidx.room:room-compiler:2.6.0")
    ksp("androidx.hilt:hilt-compiler:1.1.0")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}


My build.gradle.kts(proyect):

plugins {
    id("com.android.application") version "8.1.2" apply false
    id("org.jetbrains.kotlin.android") version "1.9.20" apply false
    id("com.google.devtools.ksp") version "1.9.20-1.0.14" apply false
}

I’ve tried cleaning and rebuilding my project, verifying my dependency versions, and reviewing my Dagger Hilt and KSP setup. I’ve also reviewed the official KSP documentation and Google documentation as well, but I haven’t been able to resolve the issue. I expected that after migrating to KSP, Dagger Hilt annotations would be correctly detected and the code would work as expected.

1

There are 1 answers

0
Alan Escamilla On BEST ANSWER

I finally solve it, my mistake was not to write

implementation 'com.google.dagger:hilt-android:2.48.1'

at the dependencies from the build.gradle.kts (module). I also made some changes in the build.gradle files

now it completely works.

i leave my build.gradle.kts(module):

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("com.google.dagger.hilt.android")
id ("com.google.devtools.ksp")
id ("com.google.relay") version "0.3.10"
kotlin("plugin.serialization") version "1.9.0"

}

android {
namespace = "com.xxxxxxx.xxxx"
compileSdk = 34

defaultConfig {
    applicationId = "com.xxxxx.xxxxx"
    minSdk = 24
    targetSdk = 33
    versionCode = 1
    versionName = "1.0"

    testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    vectorDrawables {
        useSupportLibrary = true
    }
}

buildTypes {
    release {
        isMinifyEnabled = 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"
}
buildFeatures {
    compose = true
}
composeOptions {
    kotlinCompilerExtensionVersion = "1.5.0"
}
packaging {
    resources {
        excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {

implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
implementation("androidx.activity:activity-compose:1.8.0")
implementation(platform("androidx.compose:compose-bom:2023.10.01"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("com.google.dagger:hilt-android:2.48.1")
ksp("androidx.room:room-compiler:2.6.0")
ksp("com.google.dagger:dagger-compiler:2.48.1")
ksp("com.google.dagger:hilt-android-compiler:2.48.1")
implementation(platform("io.github.jan-tennert.supabase:bom:1.4.6-dev-1"))
implementation("io.github.jan-tennert.supabase:postgrest-kt")
implementation("io.github.jan-tennert.supabase:realtime-kt")
implementation("io.github.jan-tennert.supabase:storage-kt")
implementation("io.github.jan-tennert.supabase:functions-kt")
implementation("io.github.jan-tennert.supabase:gotrue-kt")
implementation("io.github.jan-tennert.supabase:compose-auth")
implementation("io.github.jan-tennert.supabase:compose-auth-ui")
implementation("io.ktor:ktor-client-cio:2.3.5")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation(platform("androidx.compose:compose- 
bom:2023.10.01"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
}

and my build.gradle.kts (proyect)

// Top-level build file where you can add configuration options common to 
all sub-projects/modules.
plugins {
id("com.android.application") version "8.1.2" apply false
id("org.jetbrains.kotlin.android") version "1.9.20" apply false
id("com.google.dagger.hilt.android") version "2.48.1" apply false
id("com.google.devtools.ksp") version "1.9.20-1.0.14" apply false
}