1

I am using Dagger 2.24.

I have a subcomponent:

@Subcomponent(modules = MyModule.class)
public interface MySubComponent {
    void inject(MyApplication app);
}

Since my subcomponent is only used by classes of a specific module, so, I try to declare my subcomponent in that module class by:

//Compiler error: An annotation argument must be a compile-time constant
@Module(subcomponents = MySubComponent.class)
abstract class MyModule {
  ...
}

When I do above thing, compiler complains "An annotation argument must be a compile-time constant". Why?

Leem
  • 17,220
  • 36
  • 109
  • 159

1 Answers1

0

You have a circular dependency between your module and your subcomponent.

Since my subcomponent is only used by classes of a specific module [...]

You have to remove subcomponents = MySubComponent.class from MyModule.

If you want to define some dependencies only for MySubcomponent, your main module (which is not MyModule) should list MySubcomponent in its subcomponent and, as you did correctly, you have to list MyModule in the subcomponent's modules. In this way, the dependency of MyModule will be provided only to MySubcomponent

Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70