Android Dagger-Hilt ignores @Provides method

Viewed 634

I have a NetworkModule and a RepositoryModule. In my NetworkModule I have methods that create a retrofit service instance (there are also methods for creating the okHttpClient and gson, but those are not relevant):

@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {

    @Singleton
    @Provides
    fun provideRetrofit(okHttpClient: OkHttpClient, gson: Gson): Retrofit {
        return Retrofit.Builder()
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl("null")
            .client(okHttpClient)
            .build()
    }

    @Singleton
    @Provides
    fun provideApiService(retrofit: Retrofit): ApiService {
        return retrofit.create(ApiService::class.java)
    }
}

Then I want to use the ApiService in my RepositoryModule but Hilt just ignores that the Provides method for it in NetworkModule even exists. I've looked at the generated code and can't find a mention of provideApiService. Android Studio also marks it as unused. I'm sure the thing should work, Android Studio even shows it in the related files: android studio related files

Here is the NetworkModule:

@Module
@InstallIn(SingletonComponent::class)
object RepositoryModule {

    @Singleton
    @Provides
    fun provideLoginRepository(
        userMessage: MutableLiveData<Message>,
        apiService: ApiService
    ): LoginRepository {
        return LoginRepositoryImpl(userMessage, apiService)
    }
}

When I try to build it, I get this error:

error: [Dagger/MissingBinding] LoginRepository cannot be provided without an @Provides-annotated method.

At first it seemed like the repository fails to inject but I've pin pointed it to the ApiService, since if I delete it from provideLoginRepository method parameters, it builds successfully.

I've been trying to solve this for hours and tried pretty much everything. If anyone knows what's up with this I would be so greatful. Thanks in advance.

EDIT: Creating the instance of ApiService inside the provideLoginRepository function makes everything work and the app successfully builds, this is not the desired fix tho:

@Singleton
@Provides
fun provideLoginRepository(
    userMessage: MutableLiveData<Message>,
    retrofit: Retrofit
): LoginRepository {
    return LoginRepositoryImpl(userMessage, retrofit.create(ApiService::class.java))
}
1 Answers

Well I've fixed the issue. It turns out java does not (as opposed to kotlin) support interface in the package name. And guess what, the ApiService was in a package called interface. After renaming the package, everything works as expected.

Related