1

I'm facing a little issue after i tried to implement Dagger 2 in Koltin , i almost tried everything i saw in other topic on Stack over flow and other articles but nothing worked out for me , this is the error i'm facing , if any one can kindly help or guide me to solve it , thank you

  • This is the error generated
error: [Dagger/MissingBinding] java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,? extends javax.inject.Provider<androidx.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method.
public abstract interface MainComponent extends dagger.android.AndroidInjector<com.example.foodapp.di.BaseApplication> {
                ^
      java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,? extends javax.inject.Provider<androidx.lifecycle.ViewModel>> is injected at
          com.example.foodapp.di.DiComponents.ViewModelProviderFactory(creators)
      com.example.foodapp.di.DiComponents.ViewModelProviderFactory is injected at
          com.example.foodapp.MainActivity.provider
      com.example.foodapp.MainActivity is injected at

  • This is my AuthViewModelModule where i map my viewmodel class
@Module
abstract class AuthViewModelsModule {
    @Binds
    @IntoMap
    @ViewModelKey(MainViewModel::class)
    abstract fun bindAuthViewModel(viewModel: MainViewModel): ViewModel?
}
  • This is my ViewModelKey
@MustBeDocumented
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
internal annotation class ViewModelKey(val value: KClass<out ViewModel>)
  • This is my ViewModelProvierFactory
lass ViewModelProviderFactory @Inject constructor(private val creators: Map<Class<out ViewModel>, Provider<ViewModel>>) :
    ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        var creator: Provider<out ViewModel>? = creators[modelClass]
        if (creator == null) { // if the viewmodel has not been created

            // loop through the allowable keys (aka allowed classes with the @ViewModelKey)
            for ((key, value) in creators) {

                // if it's allowed, set the Provider<ViewModel>
                if (modelClass.isAssignableFrom(key)) {
                    creator = value
                    break
                }
            }
        }

        // if this is not one of the allowed keys, throw exception
        requireNotNull(creator) { "unknown model class $modelClass" }

        // return the Provider
        return try {
            creator.get() as T
        } catch (e: Exception) {
            throw RuntimeException(e)
        }
    }

    companion object {
        private const val TAG = "ViewModelProviderFactor"
    }

}

  • This is AppComponent class
@Component(modules = [AndroidSupportInjectionModule::class,AndroidBuildersModule::class, ViewModelFactoryModule::class, AppModule::class])
interface MainComponent : AndroidInjector<BaseApplication>{

    @Component.Builder
    interface toBuild
    {
        @BindsInstance
        fun Building(application: Application) : toBuild
        fun build() : MainComponent
    }
}
Taki
  • 3,290
  • 1
  • 16
  • 41
  • `private val creators: Map, @JvmSuppressWildcards Provider>` https://stackoverflow.com/questions/61598709/why-the-dagger-graph-works-works-in-java-but-in-kotlin-it-says-missing-provides/61599962#61599962 – IR42 May 14 '20 at 20:34
  • in my case , i don't have that config piece of code , should i one like that ? – Taki May 14 '20 at 20:39
  • I actually did that but still showing the same error – Taki May 14 '20 at 20:41
  • `fun bindAuthViewModel` should return `ViewModel` not `ViewModel?`, and `override fun create(...` – IR42 May 14 '20 at 20:42
  • Okey let me try to delete that and see if it works – Taki May 14 '20 at 20:43
  • i did delete it but the same error still showing , i also tried to convert some class to java like the ViewModelKey and Factory provider but nothing changed , tried to also downgrade kotlin plugin version , it didn't solve the problem , so i'm stuck and puzzled – Taki May 14 '20 at 20:45
  • try `Build -> Rebuild Project ` – IR42 May 14 '20 at 20:47
  • The same , actually if the rebuild finished successfully , it means that the error is solved , but either when i run the app or rebuild the project , it keeps showing the same error – Taki May 14 '20 at 20:48
  • you didn't add `AuthViewModelsModule` to `MainComponent` modules list – IR42 May 14 '20 at 21:08
  • Yeah that was the issue mate , thank you so much for helping , Help deeply appreciated – Taki May 14 '20 at 21:12

1 Answers1

2

It looks like you forgot to add AuthViewModelsModule.class to modules in the AppComponent class

Anes Abismail
  • 305
  • 1
  • 2
  • 9