Search code examples
androidkotlinmvvmdaggerdagger-hilt

Cannot create instance of MainViewModel with Hilt


I'm testing hilt with a simple project, what I want to achieve is to generate an instance of my MainViewModel with Hilt this is what I have done so far

MainActivity

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
 ...
}

MainFragment

@AndroidEntryPoint
class MainFragment : Fragment(),MainAdapter.OnTragoClickListener {

    private val viewModel by activityViewModels<MainViewModel>()

...
}

MainViewModel

class MainViewModel @ViewModelInject constructor(private val repo:Repo):ViewModel(){
...
}

RepoImpl

class RepoImpl @Inject constructor(private val dataSource: DataSource): Repo {
...
}

DataSourceImpl

class DataSourceImpl @Inject constructor(private val tragosDao: TragosDao): DataSource{
...
}

Now , this is the architecture the app follows, here Repo and DataSource are simple interfaces that I use.

So after this I generate all that hilt requires to generate the instances

BaseApplication

@HiltAndroidApp
class BaseApplication: Application() {
}

AppModule

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

    @Singleton
    @Provides
    fun provideRoomInstance(
        @ApplicationContext context: Context
    ) = Room.databaseBuilder(
            context,
            AppDatabase::class.java,
            "tabla_tragos")
            .build()

    @Singleton
    @Provides
    fun provideTragosDao(db: AppDatabase) = db.tragoDao()

}

The module above provides tragoDao() so I can access it in my DataSourceImpl, since I need an unique instance of this database I use @Singleton on its provide

Then I just create another module that will let hilt know about the implementations of the interfaces above

@Module
@InstallIn(ActivityRetainedComponent::class)
abstract class ActivityModule {

    @Binds
    abstract fun bindDataSource(dataSource:DataSourceImpl): DataSource

    @Binds
    abstract fun bindRepo(repo: RepoImpl): Repo

}

Since I need an instance of the MainViewModel , I scope this module with ActivityRetainedComponent

After compiling the app I get this error

java.lang.RuntimeException: Cannot create an instance of class com.g.tragosapp.ui.viewmodel.MainViewModel

Dependencies

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'

dependencies {
  implementation fileTree(dir: "libs", include: ["*.jar"])
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  implementation 'androidx.core:core-ktx:1.3.0'
  implementation 'androidx.appcompat:appcompat:1.1.0'
  implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

  //Navigation Components
  implementation "androidx.navigation:navigation-fragment-ktx:2.3.0"
  implementation "androidx.navigation:navigation-ui-ktx:2.3.0"
  implementation 'androidx.legacy:legacy-support-v4:1.0.0'

  //ViewModel y LiveData
  implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

  // KTX - Viewmodel Y Livedata
  implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
  implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.0-alpha05"

  implementation "androidx.fragment:fragment-ktx:1.2.5"
  implementation "androidx.activity:activity-ktx:1.1.0"

  //utils
  implementation 'com.github.bumptech.glide:glide:4.11.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

  //Corutinas
  implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3"

  //Retrofit
  implementation 'com.squareup.retrofit2:retrofit:2.6.0'
  implementation 'com.google.code.gson:gson:2.8.5'
  implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
  implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'

  implementation 'com.github.chrisbanes:PhotoView:2.3.0'

  //Room
  implementation 'androidx.room:room-ktx:2.2.5'
  implementation "androidx.room:room-runtime:2.2.5"
  kapt "androidx.room:room-compiler:2.2.5"

  //Hilt
  implementation "com.google.dagger:hilt-android:2.28-alpha"
  kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
  implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02'


  testImplementation 'junit:junit:4.12'
  androidTestImplementation 'androidx.test.ext:junit:1.1.1'
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

I have also added

  implementation "androidx.fragment:fragment-ktx:1.2.5"
  implementation "androidx.activity:activity-ktx:1.1.0"
  implementation "androidx.core:core:1.3.1"

which has not made any difference


Solution

  • class RepoImpl
    

    Should be

    @Singleton class RepoImpl
    

    And same for DataSourceImpl

    Then change @InstallIn(ActivityRetainedComponent::class) to @InstallIn(SingletonComponent::class) (used to be ApplicationComponent)

    And also make sure to have all these deps (at the time of writing):

    buildscript {
        ext {
            dagger_version = '2.41'
        }
    
    dependencies {
        classpath "com.google.dagger:hilt-android-gradle-plugin:$dagger_version"
    }
    

    and

    apply plugin: 'dagger.hilt.android.plugin'
    apply plugin: 'kotlin-kapt'
    
    implementation "com.google.dagger:dagger:$dagger_version"
    kapt "com.google.dagger:dagger-compiler:$dagger_version"
    implementation "com.google.dagger:hilt-android:$dagger_version"
    kapt "com.google.dagger:hilt-android-compiler:$dagger_version"
    kaptTest "com.google.dagger:hilt-android-compiler:$dagger_version"
    kaptAndroidTest "com.google.dagger:hilt-android-compiler:$dagger_version"
    kapt 'androidx.hilt:hilt-compiler:1.0.0'