Skip to content Skip to sidebar Skip to footer

Dagger Android Support To Androidx.fragment

How to inject a fragment from the package androidx.fragment.app.Fragment ? I'm using the dagger-android framework to inject my dependencies in my code. As the documentation says I

Solution 1:

add the below code to your gradle.properties

android.useAndroidX=true android.enableJetifier=true

And if you are trying to inject into a Fragment you have to replace AndroidInjection.inject(this) with AndroidSupportInjection.inject(this)

Solution 2:

I had the same problem in case of HasFragmentInjector. You need to use HasSupportFragmentInjector for fragment injection. This is because, HasFragmentInjector uses android.app.Fragment which is not effected by jetifier. You need to add android-dagger-support library, jetifier converts all the support packages to androidx in Studio 3.3 (if jetifier is enabled).

If jetifier does not change support packages to androidx packages. You can download jetifier tool from here and convert the android-dagger-support.aar file manually by using the following command.

./jetifier-standalone -i dagger-android-support-<version>.aar -o <output-name>

Then add the library to your project. This is the HasSupportFragment class after conversion

import androidx.fragment.app.Fragment;
import dagger.android.AndroidInjector;

publicinterfaceHasSupportFragmentInjector {
    AndroidInjector<Fragment> supportFragmentInjector();
}

Somehow, jetifier tool was not converting libraries in AndroidStudio. I had to do it manually.

Solution 3:

I had a similar error and it was due to the Dagger version. On version 2.17 there is an strange issue, but if you roll back to version 2.16 it compiles perfectly (apart from the flags on gradle.properties that Paul posted).

From there using the tutorials you won't have trouble. Forgot to mention that on my project I had the non-androidX version of everything, then I ran the androidX migration that android studio offers, and after that I had to switch the Dagger version, but I suppose that if you do it from the start it's the same.

Hope this helps, if you switch and it doesn't work, post a little bit of your dagger implementation and plugins versions and I will try to help more!

Solution 4:

Add the following to your gradle.properties file

android.useAndroidX = trueandroid.enableJetifier = true

Solution 5:

Just for reference. i had the same problem. It was Jetifier issue. please upgrade your gradle build tools plugin to 3.3.0

        classpath 'com.android.tools.build:gradle:3.3.0'

Sample code: https://github.com/jega-ms/android-dagger2-mvp-rx

Post a Comment for "Dagger Android Support To Androidx.fragment"