codinginflow / MVVMTodo

187 stars 124 forks source link

@ViewModelInject has deprecated #12

Open kentcheung2000 opened 2 years ago

kentcheung2000 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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:")
    }
}