Search code examples
androidkotlindependency-injectionandroid-viewmodeldagger-hilt

Hilt Dependency Injection Issue: Cannot Find Symbol 'PlayerViewModel_HiltModules_BindsModule_Binds_LazyMapKey‘


I encountered the following compile-time error while using Hilt and Ksp for dependency injection:

Cannot find symbol
import xyz.linglitel.lmusic.viewmodel.PlayerViewModel_HiltModules_BindsModule_Binds_LazyMapKey;
Symbol: class PlayerViewModel_HiltModules_BindsModule_Binds_LazyMapKey
Location: package xyz.linglitel.lmusic.viewmodel

The error suggests that the PlayerViewModel_HiltModules_BindsModule_Binds_LazyMapKey class cannot be found. I have checked my code and confirmed that the annotations and dependency injection configurations in the PlayerViewModel and AppModule files are correct, but the issue persists.

My code

PlayerViewModel Class:

@HiltViewModel
class PlayerViewModel @Inject constructor(
    private val repository: MusicRepository,
    sessionToken: SessionToken,
    @ApplicationContext private val context: Context
) : ViewModel() {...some code}

AppModule Class:

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    @Provides
    fun provideSessionToken(@ApplicationContext context: Context): SessionToken {
        return SessionToken(context, ComponentName(context, MediaPlaybackService::class.java))
    }

 
}

MusicRepository:

package xyz.linglitel.lmusic.repository

import android.content.Context
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject

class MusicRepository @Inject constructor(
    @ApplicationContext private val context: Context
) {
}

MediaPlaybackService:

import androidx.media3.session.MediaLibraryService
import androidx.media3.session.MediaSession

class MediaPlaybackService : MediaLibraryService() {
    override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaLibrarySession? {
        TODO("Not yet implemented")
    }
}

BaseApplication:

@HiltAndroidApp
class BaseApplication: Application() {
}

build.gradle.kts Project:

plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.kotlin.android) apply false
    id("com.google.devtools.ksp") version "2.1.0-1.0.29" apply false
    id ("com.google.dagger.hilt.android") version "2.55" apply false
    alias(libs.plugins.compose.compiler) apply false
}

build.gradle.kts app:

import org.gradle.kotlin.dsl.testImplementation

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.android)
    id("com.google.devtools.ksp")
    id("com.google.dagger.hilt.android")
    alias(libs.plugins.compose.compiler)
}

android {
    namespace = "xyz.linglitel.lmusic"
    compileSdk = 35

    buildFeatures {
        compose = true
    }

    defaultConfig {
        applicationId = "xyz.linglitel.lmusic"
        minSdk = 31
        targetSdk = 35
        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_11
        targetCompatibility = JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = "11"
    }
    ksp{
        arg("room.schemaLocation", "$projectDir/schemas")
    }
}

dependencies {

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.appcompat)
    implementation(libs.material)
    implementation(libs.androidx.activity)
    implementation(libs.androidx.constraintlayout)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    val room_version = "2.6.1"
    implementation(libs.androidx.room.runtime)
    ksp("androidx.room:room-compiler:$room_version")
    implementation(libs.hilt.android)
    ksp(libs.hilt.android.compiler)
    implementation(libs.androidx.media3.ui)
    implementation(libs.androidx.media3.exoplayer)
    annotationProcessor("androidx.room:room-compiler:$room_version")
    implementation(libs.androidx.room.ktx)
    implementation(libs.androidx.media3.session)
    implementation(libs.androidx.media3.extractor)
    implementation(libs.androidx.media3.cast)
    implementation(libs.androidx.media3.exoplayer.workmanager)
    implementation(libs.androidx.media3.transformer)
    implementation (libs.kotlinx.coroutines.android)
    implementation(libs.androidx.room.guava)
    implementation(libs.androidx.hilt.navigation.compose)
    testImplementation(libs.androidx.room.testing)
    implementation(libs.androidx.room.paging)

    val composeBom = platform("androidx.compose:compose-bom:2025.01.01")
    implementation(composeBom)
    androidTestImplementation(composeBom)
    implementation(libs.androidx.material3)
    implementation(libs.androidx.material.icons.core)
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.kotlinx.coroutines.android)
    debugImplementation(libs.androidx.ui.tooling)
    implementation(libs.androidx.adaptive)
    implementation(libs.androidx.activity.compose)
    implementation (libs.androidx.hilt.navigation.compose)
    implementation(libs.androidx.runtime.livedata)

}

toml:

[versions]
activityCompose = "1.10.0"
agp = "8.7.3"
hiltAndroid = "2.54"
hiltAndroidCompiler = "2.51.1"
hiltCompiler = "1.2.0"
hiltNavigationCompose = "1.2.0"
kotlin = "2.1.0"
coreKtx = "1.15.0"
junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
appcompat = "1.7.0"
kotlinxCoroutinesAndroid = "1.10.1"
material = "1.12.0"
media3Extractor = "x.y.z"
media3Ui = "1.5.1"
roomRuntime = "2.6.1"
activity = "1.10.0"
constraintlayout = "2.2.0"

[libraries]
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "activityCompose" }
androidx-adaptive = { module = "androidx.compose.material3.adaptive:adaptive" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
androidx-hilt-navigation-compose = { module = "androidx.hilt:hilt-navigation-compose", version.ref = "hiltNavigationCompose" }
androidx-material-icons-core = { module = "androidx.compose.material:material-icons-core" }
androidx-material3 = { module = "androidx.compose.material3:material3" }
androidx-media3-cast = { module = "androidx.media3:media3-cast" }
androidx-media3-exoplayer = { module = "androidx.media3:media3-exoplayer", version.ref = "media3Ui" }
androidx-media3-exoplayer-workmanager = { module = "androidx.media3:media3-exoplayer-workmanager" }
androidx-media3-extractor = { module = "androidx.media3:media3-extractor", version.ref = "media3Extractor" }
androidx-media3-session = { module = "androidx.media3:media3-session" }
androidx-media3-transformer = { module = "androidx.media3:media3-transformer" }
androidx-media3-ui = { module = "androidx.media3:media3-ui", version.ref = "media3Ui" }
androidx-room-guava = { module = "androidx.room:room-guava", version.ref = "roomRuntime" }
androidx-room-ktx = { module = "androidx.room:room-ktx", version.ref = "roomRuntime" }
androidx-room-paging = { module = "androidx.room:room-paging", version.ref = "roomRuntime" }
androidx-room-runtime = { module = "androidx.room:room-runtime", version.ref = "roomRuntime" }
androidx-room-testing = { module = "androidx.room:room-testing", version.ref = "roomRuntime" }
androidx-runtime-livedata = { module = "androidx.compose.runtime:runtime-livedata" }
androidx-ui = { module = "androidx.compose.ui:ui" }
androidx-ui-tooling = { module = "androidx.compose.ui:ui-tooling" }
androidx-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" }
hilt-android-compiler = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hiltAndroidCompiler" }
hilt-android = { module = "com.google.dagger:hilt-android", 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-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinxCoroutinesAndroid" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
androidx-activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }


Cleaned and rebuilt the project, but the issue persists.
I have searched this question on Internet, but there is noway.
this is all my code,I have removed unuseful code
i am sorry to that i do not know which code is useful or unuseful,sorry


Solution

  • You are mixing three different Hilt versions:

    • 2.51.1 for the dependency com.google.dagger:hilt-android-compiler
    • 2.54 for the dependency com.google.dagger:hilt-android
    • 2.55 for the plugin com.google.dagger.hilt.android

    They all need to use the same version.

    The version catalog could help here, but you do not use it properly. You can fix it like this:

    • In the version catalog, replace the following versions with hilt = "2.55":

      hiltAndroid = "2.54"
      hiltAndroidCompiler = "2.51.1"
      hiltCompiler = "1.2.0"
      
    • In the version catalog, change the following libraries to use this new version hilt instead:

      hilt-android-compiler
      hilt-android
      
    • Move the Hilt plugin definition to the version catalog. Add this to the [plugins] section of the version catalog:

      hilt-android = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
      

      You can now use it in your gradle files to respectively replace

      id ("com.google.dagger.hilt.android") version "2.55" apply false`
      id("com.google.dagger.hilt.android")
      

      with

      alias(libs.plugins.hilt.android) apply false
      alias(libs.plugins.hilt.android)
      

    That's it, after a Gradle sync it should work now.

    (Note: hiltNavigationCompose = "1.2.0" uses a separate versioning and should stay as it is.)


    You have some other issues with the version catalog, though. From what I can tell you should at least do the following:

    • Remove the non-existent version media3Extractor = "x.y.z" and replace its usage with media3Ui (which you should probably rename to media3 since it applies to a lot more than just the UI artifact).

    • Move the KSP plugin definition to the version catalog, the same way as described above for the Hilt plugin. Since its first version component needs to be the same as your Kotlin version, you should keep the two versions next to each other. This way, when you update Kotlin, you can directly update KSP as well.

    • You have two Room dependencies in your gradle file that do not use the version catalog. Instead they use the separate version definition val room_version = "2.6.1". This is dangerous as updating Room will require you to remember to change this version and the version roomRuntime from the version catalog (which you should rename to room since it also applies to more artifacts than just the runtime).

      Move the two dependencies to the version catalog and use them the same way as you do for the other Room dependencies.