0
votes

I am trying to set up a project with dagger 2.21 and these are my classes

BaseApp

class BaseApp : DaggerApplication() {
    override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
        return DaggerAppComponent.builder().create(this)
    }
}

App Component

@Singleton
@Component(
    modules = [
        AndroidSupportInjectionModule::class,
        AppModule::class,
        NetworkModule::class,
        ActivityBuilder::class]
)
interface AppComponent: AndroidInjector<BaseApp> {

    @Component.Builder
    abstract class Builder : AndroidInjector.Builder<BaseApp>() {}
}

Base Activity

abstract class BaseActivity: DaggerAppCompatActivity(), BaseFragment.Callback {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onFragmentAttached() {

    }

    override fun onFragmentDetached(tag: String) {

    }

    fun isNetworkConnected(): Boolean {
        return NetManager(applicationContext).isConnectedToInternet
    }

}

Base Fragment

abstract class BaseFragment: DaggerFragment() {

    abstract fun layoutId(): Int

    public interface Callback {

        fun onFragmentAttached()

        fun onFragmentDetached(tag: String)
    }

}

Flight Activity

class FlightPricesActivity : BaseActivity() {

    @Inject
    lateinit var flightPricesFragment: FlightPricesFragment

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

        addFragment(flightPricesFragment, R.id.flightPricesContainer)

    }
}

Flight Fragment

class FlightPricesFragment: BaseFragment(), IFlightPricesView {

    override fun layoutId() = R.layout.flight_prices_fragment



    override fun showFlightPrices(flightPrices: FlightPricesResults) {
        // connect to recycleview
    }

    override fun loadingStarted() {

    }

    override fun loadingFailed(errorMessage: String?) {

    }

}

Activity module

@Module
internal class FlightPricesActivityModule {

    @Provides
    fun provideFlightPricesPresenter(flightPricesApi: FlightPricesApi) : IFlightPricesPresenter {
        return FlightPricesPresenter(flightPricesApi)
    }

    @Provides
    fun provideFlightPricesApi(retrofit: Retrofit) : FlightPricesApi {
        return retrofit.create(FlightPricesApi::class.java)
    }

    @Provides
    fun provideFlightPricesRVAdapter() : FlightPricesRVAdapter {
        return FlightPricesRVAdapter()
    }

}

Fragment Module Provider

@Module
public abstract class FlightPricesFragmentProvider {

      @ContributesAndroidInjector(modules = [FlightPricesFragmentModule::class])
      abstract fun provideFlightPricesFragment(): FlightPricesFragment

}

Fragment module

@Module
class FlightPricesFragmentModule {


    @Provides
    @FragmentScope
    fun provideFlightPricesFragment() : FlightPricesFragment {
        return FlightPricesFragment()
    }

}

Activity Builder


@Module
public abstract class ActivityBuilder {

    @ContributesAndroidInjector(modules = [FlightPricesActivityModule::class, FlightPricesFragmentProvider::class])
    public abstract fun bindFlightPricesActivity(): FlightPricesActivity


}

Dagger 2.21 depenedancies


    implementation 'com.google.dagger:dagger:2.21'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.21'
    implementation 'com.google.dagger:dagger-android:2.21'
    implementation 'com.google.dagger:dagger-android-support:2.21'
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.21'
    kapt "com.google.dagger:dagger-compiler:2.21"
    kapt "com.google.dagger:dagger-android-processor:2.21"

this is stack trace error(updated):

/app/build/tmp/kapt3/stubs/debug/com/reza/skyscannertest/di/component/AppComponent.java:8: error: [Dagger/MissingBinding] com.reza.skyscannertest.ui.flightPrices.view.FlightPricesFragment cannot be provided without an @Inject constructor or an @Provides-annotated method. This type supports members injection but cannot be implicitly provided.
public abstract interface AppComponent extends dagger.android.AndroidInjector<com.reza.skyscannertest.BaseApp> {
                ^
  A binding with matching key exists in component: com.reza.skyscannertest.di.module.flightPrices.FlightPricesFragmentProvider_ProvideFlightPricesFragment.FlightPricesFragmentSubcomponent
      com.reza.skyscannertest.ui.flightPrices.view.FlightPricesFragment is injected at
          com.reza.skyscannertest.ui.flightPrices.view.FlightPricesActivity.flightPricesFragment
      com.reza.skyscannertest.ui.flightPrices.view.FlightPricesActivity is injected at
          dagger.android.AndroidInjector.inject(T) [com.reza.skyscannertest.di.component.AppComponent → com.reza.skyscannertest.di.module.ActivityBuilder_BindFlightPricesActivity.FlightPricesActivitySubcomponent]

I try to provide flightFragment but look like not going there.

1
What is the entire error stack trace? - Ray Hunter
Tahnks Ray. I've updated the question. - user1820178
try to add FlightPricesFragmentModule::class in AppComponent - Shweta Chauhan
FlightPricesFragmentModule::class is part of ActivityBuilder which is in appComponent and I just tried and same :( error. thanks for ur time - user1820178

1 Answers

0
votes

I should add constructor for the my fragment as dagger looking for constructor to build provider

class FlightPricesPresenter @Inject constructor(flightPricesApi: FlightPricesApi) : IFlightPricesPresenter {

thanks my 3 days reading docs but it was worth to have dagger 2.21 on board in replacement of dagger 2.10 :)