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

Tash

01/28/2022, 2:11 AM
Seeing a
has no zero argument constructor
runtime error when using
androidx.lifecycle.viewmodel.compose.viewModel()
🧵
Happens ONLY when the
ViewModel
is in a separate gradle module
The activity:
Copy code
// In Module A
@AndroidEntryPoint
class FooActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            // Breaks when FooViewModel is in a different gradle module
            val fooViewModel = viewModel<FooViewModel>()
            ScreenContent(state = fooViewModel.state)
        }
    }
}
The viewmodel:
Copy code
// In Module B
@HiltViewModel
class FooViewModel @Inject constructor(
    private val getFoo: GetFoo
) : ViewModel() { ... }
Something to do with my setup or could it be a bug?
Hmm just updated dagger/hilt to 2.40.5, seems to have fixed it 🤔
y

Yves Kalume

01/28/2022, 10:45 AM
Try
hiltViewmodel()
instead of
viewModel()
but first you must add the hilt-compose-navigation lib to your gradle
t

Tash

01/28/2022, 6:39 PM
Had the same result with
hiltViewModel()
… the only thing that fixed it was bumping dagger version 😬
186 Views