https://kotlinlang.org logo
#compose
Title
# compose
o

oday

06/25/2022, 9:03 AM
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

Chris Fillmore

06/25/2022, 11:15 AM
Can you show the module?
b

Berkeli Alashov

06/25/2022, 11:31 AM
f

FunkyMuse

06/25/2022, 11:52 AM
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

FunkyMuse

06/25/2022, 3:22 PM
You need to enable aggregating task for hilt in your app module
o

oday

06/25/2022, 3:47 PM
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

Reda AOUTEM

06/26/2022, 11:06 AM
In your code, you’ve written
val viewModel : AuthenticationViewModel = viewModel()
instead of
= hiltViewModel()
. Could it be the mistake ?
o

oday

06/26/2022, 1:35 PM
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
31 Views