如何提供对“嵌套”模块的主应用程序模块的依赖关系

时间:2019-07-30 20:37:21

标签: android kotlin dagger-2

我有一个包含3个不同模块的应用:app(main)apirepository 我的app模块依赖于repository模块,而依赖于api模块:

app -(depends)-> repository -(depends)-> api

我想在所有3个模块中使用Dagger2相互注入依赖关系,而不必将这些依赖关系暴露给不需要查看的模块。

基本上,api模块将提供与服务相关的类以与API通信

repository模块将使用api类来协调资源的调用

app模块将仅直接使用repository类,而无需了解api模块的任何内容

以下是我的匕首模块/组件的结构:

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

        }
    }

尝试运行项目后,我在生成输出中看到当前错误

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.

0 个答案:

没有答案