Error de Dagger 2: la dependencia "no se puede proporcionar sin un constructor @Inject" mientras se anota con @Inject
Comencé a usar Dagger 2 y enfrenté un problema extraño que me parece un error.
Tengo 3 módulos, que se componen en un subcomponente, que a su vez extiende / aumenta el componente de nivel superior.
El subcomponente es bastante simple: solo combinación de módulos y un único punto de inyección:
@Singleton
@Subcomponent(
modules = {
NavigationDrawerModule.class,
NavigationListModule.class,
SwitcherModule.class
}
)
public interface NavigationDrawerComponent {
NavigationDrawerFragment inject(NavigationDrawerFragment object);
}
El primer módulo tiene este aspecto: proporciona dependencias generales de nivel de fragmento:
@Module
public class NavigationDrawerModule {
private final Activity activity;
private final View rootView;
private final LoaderManager loaderManager;
public NavigationDrawerModule(Activity activity, View rootView, LoaderManager loaderManager) {
this.activity = activity;
this.rootView = rootView;
this.loaderManager = loaderManager;
}
@Provides @Singleton EventBus provideLocalBus() {
return EventBus.builder().build();
}
@Provides @Singleton View provideViewRoot() {
return rootView;
}
@Provides @Singleton LoaderManager provideLoaderManager() {
return loaderManager;
}
@Provides @Singleton Context provideContext() {
return activity;
}
}
El segundo módulo tiene este aspecto: proporciona un presentador / controlador y sus dependencias para un subconjunto de IU en la pantalla:
@Module
public class SwitcherModule {
@Provides SwitchController provideSwitcherController(SwitchControllerImpl impl) {
return impl;
}
@Provides SwitcherView provideSwitcherView(SwitcherViewImpl impl) {
return impl;
}
}
Tercer módulo: otro presentador / controlador para un subconjunto de IU:
@Module
public class NavigationListModule {
@Provides @Singleton NavigationListController provideNavigationListController(NavigationListControllerImpl impl) {
return impl;
}
@Provides @Singleton NavigationListView provideNavigationListView(NavigationListViewImpl impl) {
return impl;
}
}
Parte relevante del fragmento que se inyecta:
@Inject SwitchController identitySwitchController;
@Inject SwitcherView identitySwitcherView;
@Inject NavigationListController navigationListController;
@Inject NavigationListView navigationListView;
NavigationListControllerImpl
implementa el siguiente constructor:
@Inject
public NavigationListControllerImpl(Context ctx, EventBus bus) {
this.ctx = ctx;
this.bus = bus;
}
El error que obtengo del compilador Dagger 2 es el siguiente:
error: ...sidenavigation.navigationlist.NavigationListControllerImpl cannot be provided without an @Inject constructor or from an @Provides-annotated method.
...sidenavigation.NavigationDrawerFragment.navigationListController
[injected field of type: ...sidenavigation.navigationlist.NavigationListController navigationListController]
...sidenavigation.navigationlist.NavigationListModule.provideNavigationListController(...sidenavigation.navigationlist.NavigationListControllerImpl impl)
[parameter: ...sidenavigation.navigationlist.NavigationListControllerImpl impl]
El error se queja de la falta del constructor anotado @ Inject, ¡pero existe! Si reemplazo la creación de instancia de NavigationListControllerImpl implícita (pasando por@Provides
-parámetro de método) con explícito (connew
), dagger comienza a quejarse por el mismo error pero ahora por el objeto presentador, que es la segunda entrada en el mismo módulo, y así sucesivamente.
Toda esta situación parece muy extraña, y me gustaría escuchar algunos comentarios de los usuarios más experimentados de Dagger 2 (¿y desarrolladores?).
¡Gracias de antemano!