I have a app with 3 different modules: app(main)
,api
and repository
My app
module depends on repository
module, that depends on api
module:
app -(depends)-> repository -(depends)-> api
I would like to use Dagger2 in all 3 modules to inject dependencies in each other without exposing theses dependencies to the modules that doesn't need to see it.
Basically the api
module will provide service related classes to communicate with the API
The repository
module will use the api
classes to coordinate the call of the resources
The app
module will use only the repository
classes directly, without knowing anything of the api
module
Here are how my dagger modules/components are structured:
ApiModule:
@Component(modules = [ApiModule::class])
interface ApiComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun requestApiModule(apiModule: ApiModule): Builder
fun build(): ApiComponent
}
}
@Module
class ApiModule {
@Provides
@Reusable
fun provideApiClient(apiConfig: ApiConfig) = ApiClient(apiConfig)
@Provides
@Reusable
fun provideConsumerService(appConsumerService: AppConsumerService): ConsumerService = appConsumerService
@Provides
@Reusable
fun provideParkingService(appParkingService: AppParkingService): ParkingService = appParkingService
}
RepositoryModule:
@Component(
modules = [RepositoryModule::class],
dependencies = [ApiComponent::class]
)
interface RepositoryComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun requestRepositoryModule(repositoryModule: RepositoryModule): Builder
fun apiComponent(apiComponent: ApiComponent): Builder
fun build(): RepositoryComponent
}
}
@Module
class RepositoryModule {
@Provides
@Reusable
fun provideLocalStorage(application: Application) = LocalStorage(application.applicationContext)
@Provides
@Reusable
fun provideLocalSession(application: Application) = LocalSession(application.applicationContext)
@Provides
@Reusable
fun provideApiConfig(apiRepositoryConfig: ApiRepositoryConfig): ApiConfig = apiRepositoryConfig
@Provides
@Reusable
fun provideConsumerRepository(appConsumerRepository: AppConsumerRepository): ConsumerRepository =
appConsumerRepository
@Provides
@Reusable
fun provideParkingRepository(appParkingRepository: AppParkingRepository): ParkingRepository = appParkingRepository
}
AppModule:
@Singleton
@Component(
modules = [ActivityModule::class, ViewModelModule::class, AndroidInjectionModule::class],
dependencies = [RepositoryComponent::class]
)
interface ApplicationComponent : AndroidInjector<CustomApplication> {
override fun inject(customApplication: CustomApplication)
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application): Builder
fun repositoryComponent(repositoryComponent: RepositoryComponent): Builder
fun build(): ApplicationComponent
}
}
After trying to run the project I'm seeing the current errors in the build output
error: cannot access ApiComponent class file for com.spaces.api.module.ApiComponent not found Consult the following stack trace for details
error: [ComponentProcessor:MiscError] dagger.internal.codegen.ComponentProcessor was unable to process this interface because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.