Issue
This Content is from Stack Overflow. Question asked by Diego Perez
I have a class that extends an interface as follows:
class AWSTestTypeService : IAWSTestTypeService {
override fun getTestTypes(): List<TestTypeDTO>?
{
val pAWS = instance
return pAWS!!.getTestTypes()
}
}
And the corresponding interface:
interface IAWSTestTypeService {
fun getTestTypes(): List<TestTypeDTO>?
}
And trying to inject it through the next module:
@Module
@InstallIn(SingletonComponent::class)
abstract class Dependencies {
@EntryPoint
@InstallIn(SingletonComponent::class)
interface IEntryPoint{
fun awsTestTypeService(): IAWSTestTypeService
}
@Binds
@Singleton
abstract fun bindsAWSTestTypeService(
awsTestTypeService: AWSTestTypeService
): IAWSTestTypeService
}
And requesting it in:
class FreddieMercuryClass {
private lateinit var awsTestTypeService : IAWSTestTypeService
init {
setDependencies()
}
private fun setDependencies(){
val entryPointServices = EntryPointAccessors.fromApplication(tm.context, DependenciesServices.IEntryPoint::class.java)
this.awsTestTypeService = entryPointServices.awsTestTypeService()
}
}
But when compiling I get the following Hilt error:
/Users/xxx/StudioProjects/app_name/app/build/generated/hilt/component_sources/debug/com/xxx/xxx/ui/app/TMApplication_HiltComponents.java:161: error: [Dagger/MissingBinding] xxx.xxx.xxx.services.aws.AWSTestSubjectService cannot be provided without an @Inject constructor or an @Provides-annotated method.
public abstract static class SingletonC implements xxx.xxx.xxx.common.dependencies.Dependencies.IEntryPoint,
^
xxx.xxx.xxx.services.aws.AWSTestSubjectService is injected at
xxx.xxx.xxx.dependencies.Dependencies.bindAWSTestSubjectService(awsTestSubjectService)
xxx.xxx.xxx.core.iservice.aws.IAWSTestSubjectService is requested at
xxx.xxx.xxx.dependencies.Dependencies.IEntryPoint.awsTestSubjectService()
What’s wrong?
Solution
After long investigation found that if you don’t specify/inject an empty constructor in classes that are to be injected (and their corresponding interfaces are to be injected too) then Hilt doesn’t know how to provide them, an so the error:
cannot be provided without an @Inject constructor or an
@Provides-annotated method.
So, the solution is as simple as @Inject(ing) an empty constructor in classes to be injected (where there is not one already, of course).
class FreddieMercuryClass
@Inject constructor() {
private lateinit var awsTestTypeService : IAWSTestTypeService
init {
setDependencies()
}
private fun setDependencies(){
val entryPointServices = EntryPointAccessors.fromApplication(tm.context, DependenciesServices.IEntryPoint::class.java)
this.awsTestTypeService = entryPointServices.awsTestTypeService()
}
}
This Question was asked in StackOverflow by Diego Perez and Answered by Diego Perez It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.