I\'m having a problem in understanding why the following code doesn\'t work.
I have following project structure:
@Component(modules = CCModule.class)
Add
@Singleton
@Component(modules = {NameModule.class})
public interface NameComponent {
}
for the component because dagger2 don't allow to use unscoped components with scoped modules
You should put @Singleton
to CComponent
class declaration.
@Singleton
@Component(modules = CCModule.class)
public interface CComponent {
XXX getXXX();
}
Explanation is in error message: CComponent
is unscoped, @Singleton
is a scope. Dagger 2 does not allow unscoped components to use modules with scoped bindings.
However, now you will get the following error:
AComponent (unscoped) cannot depend on scoped components:
@Component(dependencies = CComponent.class, modules = AModule.class)
Unscoped components cannot have scoped dependencies. So you need to make AComponent
scoped. To do this create custom AScope
annotation.
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface AScope {
}
And annotate with it AComponent
:
@AScope
@Component(dependencies = CComponent.class, modules = AModule.class)
public interface AComponent {
}
These are new requirements appeared in latest snapshot release. It was discussed in corresponding issue and may still be changed.
Looks like a bug in the latest Dagger-2 release: https://github.com/google/dagger/issues/107