dagger android support to androidx.fragment

Irving Lóp picture Irving Lóp · Aug 16, 2018 · Viewed 7k times · Source

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 do this to inject my fragment

@Override
public void onAttach(Activity activity) {
    AndroidInjection.inject(this);
    super.onAttach(activity);
    // ...
}

the problem is that AndroidSupportInjection class accept only fragments of the package android.support.v4.app.Fragment or if I use AndroidInjection class only accept fragments of the package android.app.Fragment and I want to use fragments of the androidx.fragment.app.Fragment package.

Also DaggerFrament extend from android.support.v4.app.Fragment and want to use a fragment from androidx

And If I try to implement HasSupportFragmentInjector also this interface use a fragment from android.support

Answer

cherif picture cherif · Feb 4, 2019

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)