why am i getting nosuchmethod exception when i inject an object to my viewmodel that i have clearly provided in my dagger hilt module

98 views Asked by At

any help will be greatly appreciated as I have been on this for days now.

this is the error message;

Caused by: java.lang.NoSuchMethodException: com.example.funditech.presentation.navigationDrawer.NavigationDrawerViewModel. [] at java.lang.Class.getConstructor0(Class.java:2332) at java.lang.Class.getDeclaredConstructor(Class.java:2170) at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create

this are my dependency;

`plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id("org.jetbrains.kotlin.plugin.serialization") version "1.9.0"
    kotlin("kapt")
    id("com.google.dagger.hilt.android") version "2.48.1."

}`

implementation ("com. google. dagger: hilt-android:2.48") kapt("com. google. dagger: hilt-android-compiler:2.48")

this is what is in my classpath;`

plugins {
    id("com.android.application") version "8.1.2" apply false.
    id("org.jetbrains.kotlin.android") version "1.9.0" apply false.
}
buildscript {

    dependencies {

        classpath ("com.google.dagger: hilt-android-gradle-plugin:2.48.1")
        classpath ("com.google.gms: google-services:4.4.0")
        classpath ("org.jetbrains.kotlin:kotlin-gradle-plugin")


    }
    repositories {
        mavenCentral()
    }
}`

this is the class Iam trying to inject.

package com.example.funditech.preferenceManager

import android.content.Context
import android.content.SharedPreferences
class FunditechPreferenceManager(val context: Context)  {


    private val sharedPreferences: SharedPreferences =
        context.getSharedPreferences("funditechUser", Context.MODE_PRIVATE)


    override fun saveString(key: String, value: String) {
        sharedPreferences.edit().putString(key, value).apply()
    }

    override fun getString(key: String, defaultValue: String): String {
        return sharedPreferences.getString(key, defaultValue) ?: defaultValue
    }

}

This is my hilt module object.

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

    @Provides
    @Singleton
    fun shared Preference(@ApplicationContext context: Context): FunditechPreferenceManager {
        return FunditechPreferenceManager(context)
    }
}

this is the view Model;

@HiltViewModel
class NavigationDrawerViewModel @Inject constructor(
  private Val funditechPreferenceManager: FunditechPreferenceManager
) : ViewModel() {
}

I have tried to play around with the different versions of dagger no result. I have also tried the ksp plugin, same error.

Iam also using the latest gradle version that is 8.4.

0

There are 0 answers