I have a multi modular android app setup which consists of a Data, Domain and Presentation module. The Domain module is java-only. I know it's possible to support hilt in non-android modules by adding:
Domain build.gradle
implementation "com.google.dagger:hilt-core:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"
The domain module provides implementations of UseCase classes that should be injected in the ViewModels which live in the Presentation (app)module.
Domain Module:
@Module
@InstallIn(SingletonComponent::class)
// @InstallIn(ViewModelComponent::class)
object UseCaseModule {
@Provides
// @ViewModelScoped
fun provideGetMovieDetailsUseCase(
movieRepository: MovieRepository
): GetMovieDetailsUseCase {
return GetMovieDetailsUseCaseImpl(movieRepository)
}
}
Presentation Module:
@HiltViewModel
class MovieDetailViewModel @Inject constructor(
private val getMovieDetailsUseCase: GetMovieDetailsUseCase
) : ViewModel() {
...
}
Because of the nature of the java-only module, I can't use the @InstallIn(ViewModelComponent::class)
annotation. Instead I have to install the dependency in the SingletonComponent::class
. Which is also done in an awnser here
My Question
Is this approach 'best practice'? Or is it better to make the library an Android library so that I can scope the dependency to the ViewModel? I would prefer to keep it a java-only library.