Search code examples
androidandroid-studiokotlindagger-2dagger

error: @Component.Builder is missing setters for required modules or components in Dagger 2 instead of having setter method


I am new to dagger 2. I was making a CarComponent on kotlin, I was trying to call my DaggerCarComponent with horsePower value without calling petrolEngineModule. the following is my code :

import dagger.BindsInstance
import dagger.Component
import javax.inject.Singleton

@Component (
    modules = [WheelModule::class, PetrolEngineModule::class]
)
interface CarComponent {

    fun getCar(): Car

    fun inject(mainActivity: MainActivity)

    @Component.Builder
    interface Builder {

        @BindsInstance
        fun horsePower(horsePower : Int) : Builder

        fun build(): CarComponent
    }
}

this is PetrolEngine.kt:

package com.example.daggerapp

import android.util.Log
import javax.inject.Inject

class PetrolEngine : Engine {

    private var horsePower : Int

    @Inject constructor(horsePower: Int){
        this.horsePower = horsePower
    }

    override fun start() {
        Log.d("Engine", "Broom..., horsePower: ${this.horsePower}")
    }
}

this is PetrolEngineModule.kt:

package com.example.daggerapp

import dagger.Module
import dagger.Provides
import javax.inject.Inject

@Module
class PetrolEngineModule {

    private var horsePower: Int

    @Inject constructor(horsePower: Int) {
        this.horsePower = horsePower
    }

    @Provides
    fun provideHorsePower(): Int {
        return horsePower
    }

    @Provides
    fun provideEngine(engine: PetrolEngine): Engine
    {
        return engine
    }
}

I added the DaggerComponent here as DaggerCarComponent :

package com.example.daggerapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import javax.inject.Inject

class MainActivity : AppCompatActivity() {
    @Inject
    lateinit var car:Car

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val daggerCar: CarComponent = DaggerCarComponent.builder().petrolEngineModule(PetrolEngineModule(140)).build()
        daggerCar.inject(this)

        Log.d("Car instance", "$car")
        car.drive()
    }
}

I was following this tutorial: https://www.youtube.com/watch?v=3tIvekCTSJg&list=PLrnPJCHvNZuA2ioi4soDZKz8euUQnJW65&index=8


Solution

  • In your Builder:

    @BindsInstance
        Builder horsePower(@Named("horse_power") int horsePower);
    

    After this you will be able to pass horsePower from MainActivity without passing instance of PetrolEngineModule.And same way in ur PetrolEngine constructor:

    @Inject
    public PetrolEngine(@Named("horse_power") int horsePower) {
        this.horsePower = horsePower;
    }
    

    And in your PetrolEngineModule u can remove everything and just leave @Provides for PetrolEngine.