Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@ViewModelInject has deprecated #12

Open
kentcheung2000 opened this issue May 1, 2022 · 2 comments
Open

@ViewModelInject has deprecated #12

kentcheung2000 opened this issue May 1, 2022 · 2 comments

Comments

@kentcheung2000
Copy link

Hi,

@ViewModelInject has deprecated. I change to the following and I still got an error.

"java.lang.RuntimeException: Cannot create an instance of class com.example.room_test.ui.tasks.TasksViewModel"

Is there a way I can solve the problem? Many thanks.

KC

@HiltViewModel
class TasksViewModel @Inject constructor(
private val taskDao: TaskDao
) : ViewModel() {

val tasks = taskDao.getTasks().asLiveData()

}

@HowDidUGetOutOfBounds
Copy link

Yea, I've got similar issue. I've checked annotations everywhere and they are fine. idk what's wrong and keep investigating this issue

@HowDidUGetOutOfBounds
Copy link

Finally i got solution!
Due to some library changes we need upgrade some library versions, which are set in build.gradle on app and project levels

See this link with sample project, which shows how to inject VM with Hilt:
https://dev.to/mahendranv/android-basic-hilt-setup-with-viewmodel-fragment-32fd#fragment-setup

And here I'll also post my changes in project:

build.gradle:Project

       // App dependencies
        appCompatVersion = "1.2.0"
        constraintLayoutVersion = "2.0.4"
        coroutinesVersion = "1.3.9"
        dataStoreVersion = "1.0.0-alpha02"
        espressoVersion = "3.3.0"
        fragmentVersion = "1.3.6"
        gradleVersion = "4.1.0"
        hiltAndroidXVersion = "1.0.0-alpha03"
        hiltVersion = "2.38.1"
        junitVersion = "4.13.1"
        kotlinVersion = "1.5.21"
        ktxVersion = "1.3.2"
        lifecycleVersion = "2.3.1"
        materialVersion = "1.3.0-alpha03"
        navigationVersion = "2.3.1"
        roomVersion = "2.2.5"
        testExtJunitVersion = "1.1.2"

build.gradle:Module

 // Room
    implementation "androidx.room:room-runtime:$roomVersion"
    kapt "androidx.room:room-compiler:$roomVersion"
    implementation "androidx.room:room-ktx:$roomVersion"

 // Dagger Hilt
 implementation "com.google.dagger:hilt-android:$hiltVersion"
    kapt  "com.google.dagger:hilt-compiler:$hiltVersion"
    kapt  "androidx.hilt:hilt-compiler:$hiltAndroidXVersion"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hiltAndroidXVersion"

AppModule

package by.sergey.mynotesmvvm.di

import android.content.Context
import androidx.room.Room
import by.sergey.mynotesmvvm.data.TaskDao
import by.sergey.mynotesmvvm.data.TaskDatabase
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import javax.inject.Qualifier

import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
object AppModule {
   @Provides
   fun provideTaskDao(taskDatabase: TaskDatabase): TaskDao {
       return taskDatabase.taskDao()
   }

   @Provides
   @Singleton
   fun provideAppDatabase(
       @ApplicationContext appContext: Context,
       callback: TaskDatabase.Callback
   ): TaskDatabase {
       return Room.databaseBuilder(appContext, TaskDatabase::class.java, "TaskDatabase")
           .fallbackToDestructiveMigration()
           .addCallback(callback)
           .build()
   }

   @ApplicationScope
   @Provides
   @Singleton
   fun provideApplicationScope() = CoroutineScope(SupervisorJob())
}

@Retention(AnnotationRetention.RUNTIME)
@Qualifier
annotation class ApplicationScope

and TasksViewModel

@HiltViewModel
class TasksViewModel @Inject constructor(private val taskDao: TaskDao) : ViewModel() {

    fun job(){
        Log.d("TAG", "job:")
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants