How to properly use Hilt with ViewModel using @HiltViewModel?
Asked Answered
C

4

8

I have a problem with Hilt and ViewModel. I'm getting RunTimeException:

MainViewModel> has no zero argument constructor

I spent hours to find what is wrong...

Here are sources from my code:

AppModule:

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

    @Singleton
    @Provides
    fun provideCurrencyApi(): CurrencyApi = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
        .create(CurrencyApi::class.java)

    @Singleton
    @Provides
    fun provideMainRepository(api: CurrencyApi): MainRepository = DefaultMainRepository(api)

    @Singleton
    @Provides
    fun provideDispatchers(): DispatcherProvider = object : DispatcherProvider {
        override val main: CoroutineDispatcher
            get() = Dispatchers.Main
        override val io: CoroutineDispatcher
            get() = Dispatchers.IO
        override val default: CoroutineDispatcher
            get() = Dispatchers.Default
        override val unconfined: CoroutineDispatcher
            get() = Dispatchers.Unconfined
    }
}

MainActivity:

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    private val viewModel: MainViewModel by viewModels()

MainViewModel:

@HiltViewModel
class MainViewModel @Inject constructor(
    private val repository: MainRepository,
    private val dispatchers: DispatcherProvider
) : ViewModel() {

MainRepository

class DefaultMainRepository @Inject constructor(
    private val api: CurrencyApi
) : MainRepository {

Hee are Gradle dependencies:

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'

    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.okhttp3:okhttp:4.9.0'

    implementation "androidx.activity:activity-ktx:1.4.0"

    implementation 'com.google.dagger:hilt-android:2.40.5'
    kapt 'com.google.dagger:hilt-compiler:2.40.5'
    implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
    kapt'androidx.hilt:hilt-compiler:1.0.0'

    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.4.0'


    androidTestImplementation  'com.google.dagger:hilt-android-testing:2.40.5'
    kaptAndroidTest 'com.google.dagger:hilt-compiler:2.40.5'
    testImplementation 'com.google.dagger:hilt-android-testing:2.40.5'
    kaptTest 'com.google.dagger:hilt-compiler:2.40.5'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

Application:

@HiltAndroidApp
class CurrencyApplication: Application()

Manifest:

    <application
        android:name=".CurrencyApplication"

Im trying to find solution everywhere like here: possible reason

I will be grateful for any help

Coretta answered 7/2, 2022 at 18:52 Comment(4)
What do your build gradles look like?Disestablish
Have you taken a look at the hilt view model helper site? I think you are missing an annotation.Officialese
Which annotation ?Coretta
Hi sir! I have the same configuration...still having the issue.... did you solve it?Gaudreau
P
0

first of all, check anotationProcessor for hilt to be able generate correct binding, so go to app.gradle and check these dependency existence:

dependencies {
  
    implementation 'com.google.dagger:hilt-android:2.30-alpha'
    kapt 'com.google.dagger:hilt-android-compiler:2.30-alpha'

 
    implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02'
    kapt'androidx.hilt:hilt-compiler:1.0.0-alpha02'

...
}
Peonage answered 7/2, 2022 at 21:1 Comment(1)
Thank You for answer, Yes I have such dependencies. I added my gradle dependencies source to my questionCoretta
O
0

You have not mentioned here so i think you forgot to add @HiltAndroidApp in your app.

@HiltAndroidApp
class MyOwnApplication : Application() {

}

and then in your manifest file add android:name=".MyOwnApplication" inside your application tag.

Outlook answered 8/2, 2022 at 10:40 Comment(3)
I have already done that... Sorry that I didn't mention about that. I added those informations to my questionCoretta
How to inject this into a ViewModel?Coinstantaneous
I don't know why anyone would downvote this, I've been using Hilt for a few weeks now in some projects and I happen to forget to add this to my Application. Thanks for reminding me :)Keyhole
B
0

You are using 'kapt hilt-compiler' twice.

I am not sure that is a problem, but I am using this and it works:

... compose_version = '1.3.0-beta02' 
... hilt_version = '2.42'


// Hilt
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
kapt "androidx.hilt:hilt-compiler:1.0.0"
implementation "androidx.hilt:hilt-navigation-compose:1.0.0"
Balcer answered 28/9, 2022 at 19:1 Comment(0)
Z
0

I was had same problem , solved it throw add this annotation @AndroidEntryPoint in the fragment and main activity https://developer.android.com/training/dependency-injection/hilt-jetpack

https://i.sstatic.net/hQzI6.png

Ziagos answered 7/4, 2023 at 4:46 Comment(1)
Please read Why should I not upload images of code/data/errors? Instead, format code as a code block. The easiest way to do this is to paste the code as text directly into your question, then select it and click the code block button.Syphon

© 2022 - 2024 — McMap. All rights reserved.