Dagger: AndroidX Support Library classes cause binding failure

Created on 23 Jun 2018  路  5Comments  路  Source: google/dagger

Versions:
Dagger 2.16
Android Studio 3.2 Beta 1
Support Library: androidx.appcompat:appcompat:1.0.0-alpha3

When I use dagger.android.support.DaggerApplication as the base class for my Application, I receive this error upon compilation.

:app:kaptGenerateStubsDebugKotlin
e: C:\Users\Mitchell\StudioProjects\AndroidXDatabindingBug\app\build\tmp\kapt3\stubs\debug\com\github\magneticflux\androidxdatabindingbug\di\AppComponent.java:10: error: [Dagger/MissingBinding] java.util.Map<java.lang.Class<? extends androidx.fragment.app.Fragment>,javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends androidx.fragment.app.Fragment>>> cannot be provided without an @Provides-annotated method.
    public abstract void inject(@org.jetbrains.annotations.NotNull()
                         ^
      java.util.Map<java.lang.Class<? extends androidx.fragment.app.Fragment>,javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends androidx.fragment.app.Fragment>>> is injected at
          dagger.android.DispatchingAndroidInjector.<init>(injectorFactories)
      dagger.android.DispatchingAndroidInjector<androidx.fragment.app.Fragment> is injected at
          dagger.android.support.DaggerApplication.supportFragmentInjector
      com.github.magneticflux.androidxdatabindingbug.MyApplication is injected at
          com.github.magneticflux.androidxdatabindingbug.di.AppComponent.inject(com.github.magneticflux.androidxdatabindingbug.MyApplication)
:app:kaptDebugKotlin FAILED

FAILURE: Build failed with an exception.

When I replace import dagger.android.support.DaggerApplication with import dagger.android.DaggerApplication, the error goes away but I am unable to use DaggerAppCompatActivity or DaggerFragment from the support libraries.

A minimal project to reproduce the issue is located here: magneticflux-/AndroidXDagger2Bug, simply try to use the support library features and it will fail to compile.

Most helpful comment

If you're using the support application, you need AndroidInjectionSupportModule

All 5 comments

If you're using the support application, you need AndroidInjectionSupportModule

@ronshapiro If the support module is required, why does adding a useless support fragment to be injected prevent the issue? Surely it should fail to be injected as well?

The "useless" fragment actually satisfies a binding that Dagger application requests, specifically the map of injectors for fragments. If there are no fragments that need injection, an empty map is needed to supply that binding - that's what AndroidInjectionSupportModule does. If there is, however, a contribution to that multibound map of injectors, AndroidInjectionSupportModule's @Multibinds method acts as a noop

That makes sense, thanks for explaining it; I don't have much experience with the advanced features of Dagger 2.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vorburger picture vorburger  路  4Comments

peter-tackage picture peter-tackage  路  3Comments

matpag picture matpag  路  3Comments

feinstein picture feinstein  路  3Comments

pyricau picture pyricau  路  4Comments