dagger android support to androidx.fragment

后端 未结 7 1864
孤城傲影
孤城傲影 2021-02-07 02:47

How to inject a fragment from the package androidx.fragment.app.Fragment ?

I\'m using the dagger-android framework to inject my dependencies in

7条回答
  •  别那么骄傲
    2021-02-07 03:21

    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-.aar -o 
    

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

    import androidx.fragment.app.Fragment;
    import dagger.android.AndroidInjector;
    
    public interface HasSupportFragmentInjector {
        AndroidInjector supportFragmentInjector();
    }
    

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

提交回复
热议问题