21

In my project, compose viewModel() method is giving error "has no zero argument constructor" when I am using hilt.

@Composable
    fun HomeScreen(homeViewModel: HomeViewModel = viewModel()) {
    ...
    }
    
    @HiltViewModel
    class HomeViewModel @Inject constructor(private val repository: Repository) :
        ViewModel() {
    ...
    }
@Module
@InstallIn(ViewModelComponent::class)
abstract class DataModule {
    @Binds
    abstract fun bindRepository(
        fakePuppyRepository: RepositoryImpl
    ): Repository
}

these are my dependencies

implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0'
    implementation 'androidx.activity:activity-compose:1.3.0-alpha03'
    implementation "androidx.navigation:navigation-compose:1.0.0-alpha08"
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.0'
    implementation 'androidx.compose.runtime:runtime-livedata:1.0.0-beta01'
    implementation "com.google.dagger:hilt-android:$hilt_version"
    implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
    kapt "com.google.dagger:hilt-compiler:$hilt_version"
    kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha03'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
Vivart
  • 14,900
  • 6
  • 36
  • 74
  • 2
    I also found this. But using `by viewModels()` in an activity works fine. Guessing it's a bug in one of the alpha libs. – enyciaa Mar 02 '21 at 23:06
  • Are you using this composable within NavHost? I am also running I to this problem and the Hilt integration page is outdated recommending HiltViewModelFactory – jeubank12 Mar 10 '21 at 23:35

6 Answers6

15

Based on your dependencies, I suspect you are using NavController/NavHost.

Add this dependency: androidx.hilt:hilt-navigation-compose:1.0.0-alpha01

Now in your composable which defines NavController, call HomeScreen as follows:

import androidx.compose.runtime.Composable
import androidx.hilt.navigation.compose.hiltNavGraphViewModel
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController

...

@Composable
fun Main() {
  val navController = rememberNavController()
  NavHost(navController, startDestination = "home"){
    composable("home") {
      val model: HomeViewModel = hiltNavGraphViewModel(it)
      HomeScreen(model)
    }
  }
}

...

hiltNavGraphViewModel(...) can also be replaced with viewModel(HiltViewModelFactory(LocalContext.current, backStackEntry)) (from https://github.com/google/dagger/issues/2166#issuecomment-769162910)

jeubank12
  • 891
  • 9
  • 17
  • 2
    I removed `androidx.navigation:navigation-compose` from the project based on this answer and I cannot believe that was the problem the whole time ‍♂️ – Victor Ude Apr 16 '21 at 18:02
  • In my case it was enough to add: val exampleViewModel = hiltViewModel() ExampleScreen(exampleViewModel) https://developer.android.com/jetpack/compose/libraries#hilt-navigation – bene25 Apr 19 '22 at 16:07
  • I imagine that may be true now. This answer was very specific to androidx.navigation:navigation-compose:1.0.0-alpha08 and androidx.hilt:hilt-navigation-compose:1.0.0-alpha01. @Dương Minh answer may be more accurate now -- the key part is requiring the hilt-navigation-compose library – jeubank12 Apr 20 '22 at 15:32
7

The hiltNavGraphViewModel() extension has been deprecated. You can use hiltViewModel() instead if you use Navigation and Hilt.

First, add this dependency:

implementation 'androidx.hilt:hilt-navigation-compose:1.0.0-alpha03'

And now you can use hiltViewModel() together with Navigation.

@Composable
fun MyApp() {
    NavHost(navController, startDestination = startRoute) {
        composable("example") { backStackEntry ->
            // Creates a ViewModel from the current BackStackEntry
            // Available in the androidx.hilt:hilt-navigation-compose artifact
            val exampleViewModel = hiltViewModel<ExampleViewModel>()
            ExampleScreen(exampleViewModel)
        }
        /* ... */
    }
}

More information: Hilt and Navigation

Dương Minh
  • 2,062
  • 1
  • 9
  • 21
3

Happened to me when I set my ViewModel inside a package called new.

Let's say my base package is com.example.appname, I created a new pakage called new and I had com.example.appname.new, every other ViewModel factories were created successfully by Hilt with the exception the one inside of package new.

  1. Rename the package with name new, it looks like it is reserved.
  2. Move your ViewModel to another location.
Akhha8
  • 418
  • 3
  • 10
1

Make sure your Activity/Fragments which enclose the composables have @AndroidEntryPoint and you have the latest version of hilt dependencies.

Sponge Bob
  • 821
  • 7
  • 3
  • 1
    This is a valid response as long as you don't use compose navigation and just want a simple activity-scoped viewmodel injection (as asked by OP). – Michał Klimczak Nov 24 '21 at 07:05
1

I had the same error in Hilt version 2.44, even though I had followed all the steps correctly.

@Composable
fun MainView(viewModel : MainViewModel = viewModel()) {
   ...
}

After checking the steps, finally found the problem:

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    MyTheme {
        MainView()
    }
}

If you stop previewing the Composable function that has the ViewModel, the error will be fixed.

I hope this strange problem will be solved soon!

HamidReza RTM
  • 79
  • 1
  • 7
-1

I solved my error after renaming my package name ...ui.screens.main.new which contains my class MyCustomViewModel inside the new: package. before renaming the new package to addnew my problem was solved.

abdo Salm
  • 1,678
  • 4
  • 12
  • 22