Search code examples
androiddagger-2daggerandroid-architecture-lifecycle

Dagger : Why does dagger require a @inject constructor for an object that does't depend on another object


I think I'm missing something. I get this error:

PostsVM cannot be provided without an @Inject constructor or from an 
@Provides-annotated method.

Assume classes as follows :

@Module
public class AppModule {
    private final Application mApplication;

    @Singleton
    @Provides
    ViewModel provideListViewModel() {
        return new PostsVM();
    }
}

And a class PostVM

@Singleton
public class PostsVM extends ViewModel {

    public boolean get(){
        return true;
    }
}

And a component :

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {

    void inject(Global global);

    void inject(MainActivity mainActivity);

    @Architecture.ApplicationContext
    Context getContext();

    Application getApplication();
}

And in activity :

@Inject
public         ViewModelProvider.Factory factory;

@Override
protected void onCreate(Bundle savedInstanceState) {
    InjectorClass.inject(this);

As you can see, the example given for PostVM class, does't depend on anything, why do I need an @inject constructor in it?


Solution

  • tl;dr To prevent errors and to follow convention.


    From the JavaDoc of @Inject you can read:

    Injectable constructors are annotated with @Inject and accept zero or more dependencies as arguments. @Inject can apply to at most one constructor per class.

    And it's always good practice to follow the convention / documentation.


    So @Inject marks an entry point for Dagger to signal it how and where to create your class. It is a clear sign of how you intend your class to be used.

    • What if you have multiple constructors?
    • What if you require additional setup and should use a @Module instead?

    By just defaulting to a no-args constructor (if possible) things could start breaking very easily and you might not be able to pinpoint the source easily if you just assume Dagger does its job.

    __ cannot be provided without an @Inject constructor or from an @Provides-annotated method.
    

    This error on the other hand gives you a strong signal that you're missing something and cannot be ignored.