question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion needed for using Hilt in library modules

See original GitHub issue

In 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:closed
  • Created 3 years ago
  • Reactions:22
  • Comments:20 (4 by maintainers)

github_iconTop GitHub Comments

17reactions
joshfriendcommented, Oct 7, 2020

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?

5reactions
danysantiagocommented, Jan 22, 2021

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hilt in multi-module apps - Android Developers
Hilt code generation needs access to all the Gradle modules that use Hilt. The Gradle module that compiles your Application class needs to ......
Read more >
How to use Hilt in a Library : r/androiddev - Reddit
I made a library doing exactly that, libraries don't need the hilt android app part because libraries are not intended to have an...
Read more >
How can I use Hilt for module or library(aar)? - Stack Overflow
I am making a library. And I am using Hilt. The sample app is done and I am just trying to separate it...
Read more >
Standardizing Android Dependency Injection with Hilt - Medium
If you don't already know, Hilt is a library built on top of Dagger that provides a standard way of implementing Dependency Injection...
Read more >
Explore Hilt: A Dependency Injection Library for Android
This is similar in Dagger. @InstallIn. The Hilt module annotated with @InstallIn(ActivityComponent::class) because we want Hilt to inject that ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found