I have a ViewModel class just like the one defined in the Connecting ViewModel and repository section of Architecture guide. When I run my app I get a runtime exception. Does anyone know how to get around this? Should I not be injecting the ViewModel? Is there a way to tell the ViewModelProvider
to use Dagger to create the model?
public class DispatchActivityModel extends ViewModel {
private final API api;
@Inject
public DispatchActivityModel(API api) {
this.api = api;
}
}
Caused by: java.lang.InstantiationException: java.lang.Class has no zero argument constructor at java.lang.Class.newInstance(Native Method) at android.arch.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:143) at android.arch.lifecycle.ViewModelProviders$DefaultFactory.create(ViewModelProviders.java:143) at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:128) at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:96) at com.example.base.BaseActivity.onCreate(BaseActivity.java:65) at com.example.dispatch.DispatchActivity.onCreate(DispatchActivity.java:53) at android.app.Activity.performCreate(Activity.java:6682) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2619) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6121)
Today I learnt a way to avoid having to write factories for my
ViewModel
classes:EDIT: As pointed out by @Calin in the comments, we are using Dagger's
Lazy
in the code snippet above, not Kotlin's.Rather than injecting the
ViewModel
, you can inject a genericViewModelFactory
into your activities and fragments and obtain an instance of anyViewModel
:I used
AndroidInjection.inject(this)
as with thedagger-android
library, but you can inject your activity or fragment the way you prefer. All that is left is to make sure you provide yourViewModel
from a module:Or applying the
@Inject
annotation to its constructor:I believe there is a second option if you don't want to use the factory mentioned in Robert's answer. It is not necessarily better solution but it is always good to know the options.
You can leave your viewModel with default constructor and inject your dependencies just as you do in case of activities or other elements created by system. Example:
ViewModel:
Component:
Module:
Cheers, Piotr
What may not be obvious in the question is that the ViewModel cannot be injected that way because the ViewModelProvider default Factory that you get from the
method with only the LifecycleOwner parameter can only instantiate a ViewModel that has a no-arg default constructor.
You have a param: 'api' in your constructor:
In order to do that you need to create a Factory so that you can tell it how to create itself. The sample code from google gives you Dagger config and Factory code as mentioned in the accepted answer.
DI was created to avoid use of the new() operator on dependencies because if the implementations change, every reference would have to change as well. The ViewModel implementation wisely uses a static factory pattern already with ViewProvider.of().get() which makes its injection unnecessary in the case of no-arg constructor. So in the case you don't need to write the factory you don't need to inject a factory of course.
You need to implement your own
ViewModelProvider.Factory
. There is an example app created by Google demonstrating how to connect Dagger 2 with ViewModels. LINK. You need those 5 things:In ViewModel:
Define annotation:
In ViewModelModule:
In Fragment:
Factory:
I'd like to provide a third option for anyone stumbling upon this question. The Dagger ViewModel library will allow you to inject in a Dagger2-like way with ViewModels optionally specifying the ViewModel's scope.
It removes a lot of the boilerplate and also allows the ViewModels to be injected in a declarative way using an annotation:
It also requires a small amount of code to setup a module from which the fully dependency injected ViewModels can be generated and after that it's as simple as calling:
On the ViewModelInjectors class which is generated.
DISCLAIMER: it is my library but I believe it's also of use to the author of this question and anyone else wanting to achieve the same thing.
The default ViewModel factory used to get an instance of your
DispatchActivityModel
in your view constructs the ViewModels using assumed empty constructors.You can write your custom
ViewModel.Factory
to get around it, but you'd stil need to take care of completing the dependency graph yourself if you want to provide yourAPI
class.I wrote a small library that should make overcoming this common more straightforward and way cleaner, no multibindings or factory boilerplate needed, while also giving the ability to further parametrise the
ViewModel
at runtime: https://github.com/radutopor/ViewModelFactoryIn the view:
Like I mentioned, you can also easily add runtime parameters to your
ViewModel
instances as well: