Dagger 2 error: dependency “cannot be provided without an @Inject constructor” while it actually annotated with @Inject

I got this same error because I forgot to expose the objects provided by the modules in the parent component to the other components that are depend on it.

Parent component example:

@Singleton
@Component(modules = {AppModule.class})
public interface AppComponent {
    AppPref exposeAppPref(); /* my issue was caused by forgot this line,
the method name doesn't matter, what matters is the object type AppPref provided in the AppModule 
that you want it to be available in the component that declares this component as one of its dependencies*/
}

Sample component that makes the above component as a dependency

@UserScope
@Component (dependencies = {AppComponent.class})
public interface ActivityComponent {
    void inject(MainActivity activity);
}

Update:

AppModule:

...
    @Provides
    @Singleton
    AppPref provideAppPref() {
        return appPref;
    }
...

Leave a Comment