0

My project worked as a long time, it use single Activity, Dagger2. We are setting up Dagger2 and ViewModel like this post.

Today, I use Hilt for my project, but it in a specific dynamic feature module. In main module (:app), I put annotation @AndroidEntryPoint for MainActivity and project can't build with log

[Dagger/MissingBinding] androidx.lifecycle.ViewModelProvider.Factory cannot be provided without an @Provides-annotated method.

image-1

There's something conflicting here.

Please help me. Thanks.

Update:

ViewModelFactory.kt

class ViewModelFactory
@Inject
constructor(private val creators: @JvmSuppressWildcards Map<Class<out ViewModel>, Provider<ViewModel>>) :
        ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        val creator = creators[modelClass]
                ?: creators.asIterable().firstOrNull {
                    modelClass.isAssignableFrom(it.key)
                }?.value
                ?: throw IllegalArgumentException("Unknown model class: $modelClass")

        return try {
            @Suppress("UNCHECKED_CAST")
            creator.get() as T
        } catch (e: Exception) {
            throw RuntimeException(e)
        }
    }

}
PhongBM
  • 799
  • 10
  • 23

1 Answers1

0

If you using Hilt in your app, I think you should remove ViewModelModule and use annotation @HiltViewModel and instance as normal using ViewModelProvider or the by viewModels() KTX extension.

Check this https://dagger.dev/hilt/view-model

  • I want only use Hilt in a specific dynamic feature module, don't touch to app module, because my project has many file, I can't refactoring all. – PhongBM May 27 '21 at 04:44