Android 多模块项目:如何设置Dagger以提供接口,但隐藏特定于实现的依赖关系?

Android 多模块项目:如何设置Dagger以提供接口,但隐藏特定于实现的依赖关系?,android,dagger-2,dagger,modularization,android-module,Android,Dagger 2,Dagger,Modularization,Android Module,在我的应用程序中,我有两个模块:app和repository 存储库取决于房间,并具有目标存储库界面: interface GoalRepository 以及一个内部的GoalRepositoryImpl类,因为我不想将它或文件室依赖项暴露给其他模块: @Singleton internal class GoalRepositoryImpl @Inject constructor(private val dao: GoalDao) : GoalRepository app依赖于reposit

在我的应用程序中,我有两个模块:
app
repository

存储库
取决于房间,并具有
目标存储库
界面:

interface GoalRepository
以及一个内部的
GoalRepositoryImpl
类,因为我不想将它或文件室依赖项暴露给其他模块:

@Singleton
internal class GoalRepositoryImpl @Inject constructor(private val dao: GoalDao) : GoalRepository
app
依赖于
repository
获取
GoalRepository
实例。
我有一个目标存储模块,目前是:

@Module
class GoalRepositoryModule {
    @Provides
    @Singleton
    fun provideRepository(impl: GoalRepositoryImpl): GoalRepository = impl

    @Provides
    @Singleton
    internal fun provideGoalDao(appDatabase: AppDatabase): GoalDao = appDatabase.goalDao()

    @Provides
    @Singleton
    internal fun provideDatabase(context: Context): AppDatabase =
        Room.databaseBuilder(context, AppDatabase::class.java, "inprogress-db").build()
}
问题是,由于public
providedepository
函数公开了
GoalRepositoryImpl
,这是一个
内部的
类,因此它(显然)无法编译。
如何构建匕首设置以实现我的目标


编辑:
我尝试根据@David Medenjak注释将
provideRepository
设置为内部,现在Kotlin编译器抱怨它无法解决RoomDatabase依赖关系:

Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
    class xxx.repository.database.AppDatabase, unresolved supertypes: androidx.room.RoomDatabase    
为完整起见,
app
模块中我的组件的代码:

@Component(modules = [ContextModule::class, GoalRepositoryModule::class])
@Singleton
interface SingletonComponent

查看生成的代码匕首后,我了解到错误在于使
应用程序
模块内的
@组件
依赖于
存储库
模块内的
@模块。
因此,我在
存储库
模块中创建了一个单独的
@组件
,并使
应用程序
模块的组件依赖于它

代码
存储库
模块的组件:

@Component(modules = [GoalRepositoryModule::class])
interface RepositoryComponent {
    fun goalRepository(): GoalRepository
}
应用程序
的一个:

@Component(modules = [ContextModule::class], dependencies = [RepositoryComponent::class])
@Singleton
interface SingletonComponent

通过这种方式,
RepositoryComponent
负责构建
存储库
并了解其所有依赖项,而
SingletonComponent
只需了解
RepositoryComponent

为什么您不能将
providedepository
也设置为内部?刚刚尝试过,似乎效果不错,特别是因为Dagger生成java代码,而internal在java中仍然是“公共的”。我尝试了你的建议,现在Kotlin编译器抱怨缺少依赖项。我已经更新了我的问题你能提供项目的GitHub链接吗?当我试图在一个多模块的android项目中实现dagger 2时,我遇到了类似的情况,有人认为让我怀疑的是(我是dagger的新手)。。RepositoryComponent和repository模块本身需要上下文才能工作,但“依赖性”是隐藏的,因此其他人在编译项目时会意识到这一点,并抛出错误。这是使用匕首时常见的情况吗?