so I’m injecting a couple of dependencies into my ...
# compose
o
so I’m injecting a couple of dependencies into my ViewModel like so
Copy code
@HiltViewModel
class AuthenticationViewModel @Inject constructor(
    private val determineAuthStatus: DetermineAuthStatus,
    private val populateUserPrefs: PopulateUserPrefs
) : ViewModel() {
and I want to use the viewmodel in a composable like so
Copy code
val viewModel : AuthenticationViewModel = hiltViewModel()
but I get that the viewmodel does not have any zero-argument constructors, I’ve already created the module for these 2 deps to be provided this composable that i’m using the viewmodel in, is started inside an Activity which I’ve set @AndroidEntryPoint on, what am I missing ?
c
Can you show the module?
b
f
Did you add @HiltAndroidApp in your Application?
I don’t think I need a module even
the problematic file there is AuthenticationViewModel
so the MainActivity calls Authentication composable Authentication composable creates new instance of AuthenticationViewModel AuthenticationViewModel injects populateUserPrefs and determineAuthStatus AuthenticationViewModel has @HiltViewModel and MainActivity has @AndroidEntryPoint, Application class has @HiltAndroidApp, populateUserPrefs and determineAuthStatus both have @Inject constructor so that Hilt could “see” them
what if it has something to do with MainActivity being in another module than the rest of the files like Authentication and AuthenticationViewModel?
so if I initialize the viewmodel in the same file where @AndroidEntryPoint is defined, using
by viewModels()
it works fine but if I initialize it inside the composable using
= viewModel()
it does the no zero-argument constructor error
I want to initialize it inside the composable, I dont want to initialize it inside the activity and then pass the viewmodel to the composable, that’s just dumb
f
You need to enable aggregating task for hilt in your app module
o
hmm what’s that
Copy code
kapt {
 correctErrorTypes true
}
?
ah this
Copy code
hilt {
    enableAggregatingTask = true
}
that did nothing
there is something fundamentally wrong with what’s happening
HALP!
despair
r
In your code, you’ve written
val viewModel : AuthenticationViewModel = viewModel()
instead of
= hiltViewModel()
. Could it be the mistake ?
o
Tried that already
Turned out to be an issue of multi module, main activity that has AndroidEntryPoint is sitting in another package than where I'm calling the composable
Opted for single package instead of this headache