Search code examples
kotlindagger-2android-jetpackandroid-jetpack-composedagger-hilt

Hilt : @DefineComponent dagger.hilt.components.SingletonComponent is missing a parent declaration


I am setting up a project using Jetpack Compose + Hilt + Coroutines. I haven't used any fragment. It is a single activity(ComponentActivity) based project. I have used the latest versions : Kotlin : 1.4.31 & Hilt : 2.33-beta

This is the logcat error snapshot : Logcat snapshot

I understand that this question has been asked earlier however the issue was the usage of a deprecated Hilt library where "ApplicationComponent" has been deprecated inside AppModule. Please have a look at the setup :

AppModule :

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

ApplicationClass :

@HiltAndroidApp
class App: Application()

MainActivity :

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        home()
    }
  }
}

@Composable
fun home(model: UserViewModel = viewModel()) {
val items = model.fetchUserList.observeAsState(initial = listOf<UserDomain>())
val test = items.value
Log.d("test_data",test.toString())
}

ViewModel :

@HiltViewModel
class UserViewModel @Inject constructor (private val mainRepository: MainRepository) : ViewModel() {

val fetchUserList = liveData {
    emit(Resource.loading(data = null))
    emit(mainRepository.getEmployee())
  }
}

Hilt Dependency :

// Hilt dependency injection
implementation "com.google.dagger:hilt-android:2.33-beta"
kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
// ViewModel
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
// When using Kotlin.
kapt 'androidx.hilt:hilt-compiler:1.0.0-beta01'

Solution

  • Update your hilt compiler dependency:

    kapt "com.google.dagger:hilt-android-compiler:2.33-beta"