Search code examples
androiddependency-injectiondagger-2

Dynamically include dagger2 modules


I have dagger component MainComponent, which is dependent on another module: LocalModule or PrdModule. I would like to dynamically include one of the modules based on some flag. Quick example:

In code it looks like this:

@Component(modules = [LocalModule::class])
interface MainComponent

However, if I'm building my app for customer, then I must change it manually to:

@Component(modules = [PrdModule::class])
interface MainComponent

What I would like to have is something like this:

@Component(modules = [if(someFlag) {LocalModule::class} else {PrdModule::class}])
interface MainComponent

It'd be nice if I could set this flag in gradle's build options.


Solution

  • Thanks jsamol, different source sets solved my problem. It was exactly what I was looking for!