0
votes

I'm trying to implement Dagger 2 DI to our multi library module project (30 modules and it will grow), each library module is dependent on few other modules in build.gradle files like this for example: gradle implementation project(":core").

We have main module :app, which contains application class and includes every library module.

So I decided there will be one singleton component in :app module, which will include all modules like below.

@Component(modules = [
    AppModule::class,
    AndroidSupportInjectionModule::class,
    ViewModelFactoryModule::class,
    BusinessModule::class,
    BusinessRepositoryModule::class,
    BusinessDetailFragmentModule::class,
    BusinessCollectionVerticalFragmentModule::class
])
@Singleton
interface AppComponent {

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(application: Application): Builder

        fun build(): AppComponent
    }

    fun inject(mainApplication: MainApplication)
}

The reason I have 4 business modules is because

  • BusinessModule is an abstract class, which contains all activities and viewmodels in that library module.

  • BusinessRepositoryModule provides repository instance (later will be alternative with mock repository).

  • Remaining two modules are fragments, which are included in activities in BusinessModule.. like this: @ContributesAndroidInjector(modules = [BusinessCollectionVerticalFragmentModule::class])

Problem is, that I have covered only two library modules and it's already messy. What is the right way of managing modules of a project this size?

1

1 Answers

0
votes

you are invoking all component on application level.it is inefficient to use all component at application level. make separate component and use @scope notation that will bind the lifecycle of component to activity.