简体   繁体   中英

Providing Injection for AndroidX Fragment with Dagger2?

I'm having some issues trying to provide an Injection to an AndroidX fragment, and I'm not sure what the exact issue is, and how to fix it. The app refuses to build, giving the following error:

 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.

Here's the method to provide the Injection in the Fragment:

private fun performDependencyInjection() = AndroidSupportInjection.inject(this)

The fragment's parent Activity implements HasSupportFragmentInjector:

class MainActivity : BaseActivity(), MainMVPView, HasSupportFragmentInjector {
    @Inject
    internal lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Fragment>
    ... 
    override fun supportFragmentInjector() = dispatchingAndroidInjector
}

I'm completely lost as to where to solve this error from here. It doesn't appear that there is much current documentation for using Dagger2 with AndroidX.

I do feel it is important to note I enabled AndroidX and Jetifier through gradle.properties:

android.useAndroidX=true
android.enableJetifier=true

However, nothing had changed after a clean and rebuild of the project.

What is the proper way to provide an Injection to an AndroidX fragment using Dagger2?

Edit: For the record, this is on Dagger 2 version 2.19. If I switch to using 2.16, everything works fine.

This is due to a mismatch in the Jetifier sources , as you can see from the below code:

# Androidx compatible dagger
{
    "from": { "groupId": "com.google.dagger", "artifactId": "dagger-android-processor", "version": "2.16" },
    "to": { "groupId": "com.google.dagger", "artifactId": "dagger-android-processor", "version": "2.16" }
}

From the release note of dagger-2.19:

In the next release (2.20), we will remove the old format. This will allow us to support AndroidX packages better.

So for now you either have to stick with version 2.16 or wait for the 2.20 release.

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