dagger android support to androidx.fragment

后端 未结 7 1862
孤城傲影
孤城傲影 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-<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;
    
    public interface HasSupportFragmentInjector {
        AndroidInjector<Fragment> supportFragmentInjector();
    }
    

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

    0 讨论(0)
  • 2021-02-07 03:22

    Add the following to your gradle.properties file

    android.useAndroidX = true
    android.enableJetifier = true
    
    0 讨论(0)
  • 2021-02-07 03:24

    The solution to my particular problem was to use android dagger classes as interfaces instead of extend of them:

    class MyFragment() : HasSupportFragmentInjector {
    
        @Inject
        lateinit var childFragmentInjector: DispatchingAndroidInjector<Fragment>
    
        override fun onAttach(context: Context?) {
            AndroidSupportInjection.inject(this)
            super.onAttach(context)
        }
    
        override fun supportFragmentInjector(): AndroidInjector<Fragment> {
            return childFragmentInjector
        }
    
    ........
    
    }
    

    To my Activities

    class MyActivity : HasSupportFragmentInjector {
        @Inject
        internal lateinit var fragmentInjector: DispatchingAndroidInjector<Fragment>
    
        override fun onCreate(savedInstanceState: Bundle?) {
            AndroidInjection.inject(this)
            super.onCreate(savedInstanceState)
        }
    
        override fun supportFragmentInjector(): AndroidInjector<Fragment> = fragmentInjector
    

    ...... }

    and also I have this in my gradle.properties file:

    android.useAndroidX = true android.enableJetifier = true

    0 讨论(0)
  • 2021-02-07 03:33

    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!

    0 讨论(0)
  • 2021-02-07 03:38

    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)

    0 讨论(0)
  • 2021-02-07 03:39

    This is what I did to work with androidx namespace for Dagger 2.21

    1. Downloaded the jetifier tool from here: https://developer.android.com/studio/command-line/jetifier

    2. Unzip into a folder and open a terminal pointing into the extracted bin folder

    3. From Android Studio, open a class like DaggerFragment to check the path where the file is stored, for me (in MacOS) is something like this:

    4. From terminal execute this command (replacing with the correct variables and path)

      ./jetifier-standalone -i /Users/{YOUR_USER}/.gradle/caches/{PATH_TO_DAGGER_ANDROID_SUPPORT_FOLDER}/dagger-android-support-2.21.aar -o dagger-android-support-2.21.aar

      The converted dagger-android-support-2.21.aar will appear in your bin folder

    5. Now open your app build.gradle file and change this line
      implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
      adding the , '*.aar' part in the include array

    6. Move the generated dagger-android-support-2.21.aar from bin to libs folder inside your project.

    7. Remove (or comment) this line from the dependencies implementation "com.google.dagger:dagger-android-support:2.21

      Now you can proceed invalidating the cache and rebuild the project and now DaggerFragment will point to your converted version which uses androidx.fragment.app.Fragment

    NOTE: Obviously this is a temporary workaround and you should move to the official version as soon this is fixed in Dagger

    0 讨论(0)
提交回复
热议问题