Search code examples
androidkotlindagger-2dagger-hilt

Hilt java.lang.Integer cannot be provided without @inject constructor or @Provides - annotated method


Hello I'm building the app with hilt dependecy injector, but when I try to build the app i had error from the title. Here's my code:

Flow class:

@Entity(tableName = "flows_table")
data class Flow @Inject constructor(@PrimaryKey(autoGenerate = true) val id:Int, val name:String, val duration:Int, val actions:ArrayList<Action>)

Action class:

data class Action @Inject constructor(
    val type: String,
    val color: String?,
    val brightness: Int?,
    val duration: Float
)

My application:

@HiltAndroidApp
class YeebumApplication : Application() {
     //get bulbs repository
     private val bulbsDatabase by lazy { BulbsDatabase.getInstance(this)}
     val bulbsRepository by lazy { BulbsRepository(bulbsDatabase!!.bulbsDao()) }

     //get flows repository
     private val flowsDatabase by lazy { FlowsDatabase.getInstance(this) }
     val flowsRepository by lazy { FlowsRepository(flowsDatabase!!.flowsDao())}
}

My Fragment:

@AndroidEntryPoint
class ActionDetailsFragment : Fragment() {


    @Inject
    lateinit var flow: Flow

With the activity everything works perfectly, but when I try to inject dependiences in fragment android studio throw that exception.


Solution

  • When the Hilt annotation processor comes across the @Inject annotation in your fragment it is going to try and find (from a Hilt @Module with a @Provides annotated method) an instance of Flow.

    I'm assuming from the error message you have not created this, so instead Hilt will move to the next option and try and create and instance of Flow itself.

    So Hilt looks at the first property of Flow, id of type Int. And just like with the injection of Flow in your fragment, it repeats the process. It looks for an instance of Int from a Hilt @Module with a @Provides annotated method. This doesn't exist. Next option, try and construct it itself. Well Int is a platform type, you have no access, so you haven't created an Int class with an @Inject annotated constructor.

    Hilt is now out of options and throws this Exception:

    Hilt java.lang.Integer cannot be provided without @inject constructor or @Provides - annotated method