Suggestion needed for using Hilt in library modules
See original GitHub issueIn my sample app I have created a separate library module called core. In the core module I am using network and db related dependencies like OkHttp, Retrofit and Room. Hence to access these instances as singleton, I declared them in the hilt module.
import com.mobile.app.core.BuildConfig
import com.mobile.app.core.remote.RemoteService
import com.mobile.app.core.remote.RemoteServiceInterceptor
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton
@Module
@InstallIn(ApplicationComponent::class)
object CoreModule {
@Provides
fun provideOkHttpClient(): OkHttpClient {
return OkHttpClient
.Builder()
.addInterceptor(RemoteServiceInterceptor())
.addInterceptor(HttpLoggingInterceptor().apply {
level =
if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY
else
HttpLoggingInterceptor.Level.NONE
}).build()
}
@Provides
@Singleton
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit =
Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()
@Provides
@Singleton
fun provideRemoteService(retrofit: Retrofit): RemoteService =
retrofit.create(RemoteService::class.java)
}
In my app module I have my application class as mentioned below:
import android.app.Application
import dagger.hilt.android.HiltAndroidApp
@HiltAndroidApp
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
}
}
My app build.gradle
also implements core module:
dependencies {
implementation project(path: ':core')
}
But when I build my project, it throws error as:
Task :app:kaptDebugKotlin error: cannot access OkHttpClient class file for okhttp3.OkHttpClient not found Consult the following stack trace for details.
cannot access OkHttpClient
I can overcome this error only if I change implementation
to api
for OkHttp client dependency which is delcared in the core module build.gradle. Is there anything I’m missing here.
Want I am trying to achieve is to have separate library modules for network service, local storage and firebase. So that I can implement this to app module as needed. Hence I started declaring Hilt module in each of these library modules like NetworkModule
, StorageModule
, FirebaseModule
, etc. But I couldn’t figure this out.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:22
- Comments:20 (4 by maintainers)
Top GitHub Comments
It seems to me like Hilt is breaking everyone’s ability to take advantage of gradle compile avoidance, and is polluting gradle modules with transitive dependencies.
If all of my module dependencies have to be declared
api
, and I can no longer have an@Module
in my libraries without it being required to have@InstallIn
added, what is even the point of having separated gradle modules when using hilt?@massivemadness, in Hilt 2.31.x-alpha we added an experimental flag to be able to use Hilt in a layered multi-module project. See the docs at https://dagger.dev/hilt/gradle-setup#classpath-aggregation. Please try it out and let us know if you run into trouble, its experimental because it has build performance implications we want to solve before completely committing to it.