1 year ago
#166091
Arpit Shukla
Hilt dependency injection in multi-module project
I have two modules in my android app, app
and data
. data
module contains retrofit dependency and app
module depends on data
module.
app/build.gradle:
implementation project(':data')
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
data/build.gradle:
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-moshi:$retrofit_version"
In data
module, I have the following Hilt module:
@Module
@InstallIn(SingletonComponent::class)
internal object DataModule {
@Singleton
@Provides
fun provideRetrofit(): Retrofit {
return Retrofit.Builder()
.baseUrl(Constants.API_BASE_URL)
.addConverterFactory(MoshiConverterFactory.create())
.build()
}
@Singleton
@Provides
fun provideLoginApiService(retrofit: Retrofit): LoginApiService {
return retrofit.create(LoginApiService::class.java)
}
@Singleton
@Provides
fun provideLoginRepository(loginRepository: LoginRepositoryImpl): LoginRepository {
return loginRepository
}
}
Here, LoginApiService
and LoginRepositoryImpl
are internal in data
module and LoginRepository
is public (in data
module):
@Singleton
internal class LoginRepositoryImpl @Inject constructor(
private val loginApiService: LoginApiService
) : LoginRepository {
// ...
}
Now when I try to build the project, I get the following error:
DaggerMyApplication_HiltComponents_SingletonC.java:41: error: package retrofit2 does not exist
import retrofit2.Retrofit;
The error goes away if I add retrofit dependency in my app
module which I don't want to do.
What's happening here and how can I resolve this error without adding data
module dependencies in app
module?
android
kotlin
multi-module
dagger-hilt
android-multi-module
0 Answers
Your Answer