Stillswarm
Stillswarm

Reputation: 79

Problem setting up Hilt in Android Project

I am building an app which uses Room Database and Dagger-Hilt. When I try to run the app, I get the following error:

error: cannot find symbol
import com.example.domeupdatesreplica.ui.updates.UpdatesViewModel_HiltModules_KeyModule_Provide_LazyMapKey;
                                                ^
  symbol:   class UpdatesViewModel_HiltModules_KeyModule_Provide_LazyMapKey
  location: package com.example.domeupdatesreplica.ui.updates

I get the same error for all other viewmodels in my project. For reference, this is the one of the viewmodels:

@HiltViewModel
class UpdatesViewModel @Inject constructor(
    private val postRepository: PostRepository,
    private val userRepository: UserRepository
) :
    ViewModel() {

    // stuff
}

And this is the app module where all implementations are defined:

@Module
@InstallIn(SingletonComponent::class)
object AppModule {
    @Provides
    @Singleton
    fun provideDatabase(@ApplicationContext context: Context): DomeDatabase {
        return Room.databaseBuilder(
            context = context,
            name = "dome_database",
            klass = DomeDatabase::class.java
        )
            .fallbackToDestructiveMigrationFrom()
            .build()
    }

    @Provides
    @Singleton
    fun provideUserDao(database: DomeDatabase) = database.userDao()

    @Provides
    @Singleton
    fun providePostDao(database: DomeDatabase) = database.postDao()

    @Provides
    @Singleton
    fun provideCommentDao(database: DomeDatabase) = database.commentDao()

    @Provides
    @Singleton
    fun provideUserRepository(userDao: UserDao) = UserRepository(userDao)

    @Provides
    @Singleton
    fun providePostRepository(postDao: PostDao) = PostRepository(postDao)

    @Provides
    @Singleton
    fun provideCommentRepository(commentDao: CommentDao) = CommentRepository(commentDao)
}

Dependencies(app: build.gradle):

    val hiltVersion = "2.52"
    implementation("com.google.dagger:hilt-android:$hiltVersion")
    ksp("com.google.dagger:hilt-compiler:$hiltVersion")

Can someone please help me figure out what's going wrong? Thank you!

Upvotes: -1

Views: 84

Answers (1)

Pratik Fagadiya
Pratik Fagadiya

Reputation: 1472

Use kapt instead of ksp for Hilt's annotation processor because kapt is more mature and reliable with Hilt, especially for generating classes like UpdatesViewModel_HiltModules_KeyModule_Provide_LazyMapKey, which may not be properly generated or recognized when using ksp.

val hiltVersion = "2.52"

plugins {
    id("com.google.dagger.hilt.android")
    kotlin("kapt")
}

dependencies {
    implementation("com.google.dagger:hilt-android:$hiltVersion")
    kapt("com.google.dagger:hilt-compiler:$hiltVersion")
}

kapt {
    correctErrorTypes = true
}

Upvotes: -1

Related Questions