Solvedarchitecture components samples [Question] On GithubBrowserSample, how are ViewModel dependencies injected?

Hi,
Thanks for the very useful examples! They were really helpful!

I have two (probably basic) questions regarding Dagger and injection in ViewModels.
Sometimes we want to inject some dependencies in our class constructor. To do that, we just annotate our class constructor with @Inject and the dependencies passed to the constructor will be provided.

In the Architecture Components ViewModel, if we want to pass parameters to a ViewModel constructor, we have to use a Factory that knows how that ViewModel is instantiated. So I assume just annotating the ViewModel with @Inject wont't work, since it is the Factory that is providing the ViewModel instance, not Dagger.

I saw that you do exactly that and use GithubViewModelFactory.java to provide instances of ViewModels.

However, I don't understand two things in this class:
1 - How (T) creator.get() knows how to return a ViewModel instance with its dependencies injected on it?
2 - GithubViewModelFactory.java receives a Map<Class<? extends ViewModel>, Provider<ViewModel>> creators on its constructor. Where is this Map coming from?

Thanks! :)

18 Answers

✔️Accepted Answer

But it does not work for me.I have defined ViewModelKey like below:

@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@MapKey
@interface ViewModelKey {
    Class<? extends ViewModel> value();
}

and ViewModelModule:

@Module
abstract class ViewModelModule {
    @Binds
    @IntoMap
    @ViewModelKey(SearchViewModel.class)
    abstract SearchViewModel bindSearchViewModel(SearchViewModel searchViewModel);
}

and AppComponent has include the module:

@Singleton
@Component(modules = {
        ViewModelModule.class,
        AppModule.class,
        AndroidSupportInjectionModule.class,
        ApplicationModule.class,
        ActivityBindingModule.class
})
public interface AppComponent extends AndroidInjector<DaggerApplication> {
    void inject(ViewerApp viewerApp);

    @Override
    void inject(DaggerApplication instance);

    @Component.Builder
    interface Builder {

        @BindsInstance
        AppComponent.Builder application(Application application);

        AppComponent build();
    }
}

But when build the project, i got this in gradle console:

[dagger.android.AndroidInjector.inject(T)] java.util.Map<java.lang.Class<? extends android.arch.lifecycle.ViewModel>,javax.inject.Provider<android.arch.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method.

Is there someone i miss? @florina-muntenescu

Other Answers:

Leaving a note in case anyone else has the same issue I did when following this sample using kotlin. If you get this error (same as #141 (comment))

java.util.Map<java.lang.Class<? extends android.arch.lifecycle.ViewModel>,? extends javax.inject.Provider<android.arch.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method.

You'll need to add a @JvmSuppressWildcards annotation to the creator map's value type param:

@Singleton
class ViewModelFactory
@Inject constructor(
        private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
) : ViewModelProvider.Factory {
    ...
}

The answer to your questions is in the ViewModelModule file.
The magic is done by Dagger via the @Binds and @IntoMap annotations. A Map<K, Provider<V>> is generated via the @Intomap annotation and that Map is the constructor param for GithubViewModelFactory

I had this problem

java.util.Map<java.lang.Class<? extends android.arch.lifecycle.ViewModel>,? extends javax.inject.Provider<android.arch.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method.

in my case , i was updated my Kotlin version to 1.3.30 !
i have just changed it to 1.3.21 and problem has been fixed.

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.21"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.21"

in my case , i was updated my Kotlin version to 1.3.30 !
i have just changed it to 1.3.21 and problem has been fixed.

Your case is another issue though from https://youtrack.jetbrains.com/issue/KT-30979