简体   繁体   中英

Android + Hilt: Cannot inject interface

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?

In your CryptoCurrencyViewModelFactory constructor, change private val creators: Map<Class to private val creators: MutableMap<Class. Kotlin's Map forces immutability, but internally Dagger needs the map to be mutable.

After checking your source code, I noticed that you're importing kotlin.reflect.jvm.internal.impl.javax.inject.Inject in your CryptoCurrencyViewModelFactory class. Change it to the correct import (javax.inject.Inject), or Dagger won't recognize the @Inject annotation.

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()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM