【发布时间】:2021-08-24 09:51:55
【问题描述】:
我有一个由数据、域和演示模块组成的多模块化安卓应用程序设置。 Domain 模块是 java-only。我知道可以通过添加来支持非android模块中的hilt:
域名build.gradle
implementation "com.google.dagger:hilt-core:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"
域模块提供了 UseCase 类的实现,这些类应该被注入到存在于 Presentation (app) 模块中的 ViewModel 中。
域模块:
@Module
@InstallIn(SingletonComponent::class)
// @InstallIn(ViewModelComponent::class)
object UseCaseModule {
@Provides
// @ViewModelScoped
fun provideGetMovieDetailsUseCase(
movieRepository: MovieRepository
): GetMovieDetailsUseCase {
return GetMovieDetailsUseCaseImpl(movieRepository)
}
}
演示模块:
@HiltViewModel
class MovieDetailViewModel @Inject constructor(
private val getMovieDetailsUseCase: GetMovieDetailsUseCase
) : ViewModel() {
...
}
由于java-only模块的性质,我不能使用@InstallIn(ViewModelComponent::class)注解。相反,我必须在SingletonComponent::class 中安装依赖项。这也是在 awnser here
我的问题
这种方法是“最佳实践”吗?还是将库设为 Android 库以便我可以将依赖关系限定为 ViewModel 是否更好?我宁愿把它保留为一个仅限 java 的库。
【问题讨论】:
-
@ADM 该评论具有误导性。该模块被标记为 SingletonComponent 而不是它提供的类型。
标签: java android kotlin dagger-hilt android-module