dagger android support to androidx.fragment

后端 未结 7 1855
孤城傲影
孤城傲影 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: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

提交回复
热议问题