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
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!
Add the following to your gradle.properties file
add the below code to your gradle.properties
And if you are trying to inject into a Fragment you have to replace
AndroidInjection.inject(this)
withAndroidSupportInjection.inject(this)
This is what I did to work with
androidx
namespace for Dagger 2.21Downloaded the
jetifier
tool from here: https://developer.android.com/studio/command-line/jetifierUnzip into a folder and open a terminal pointing into the extracted
bin
folderFrom Android Studio, open a class like
DaggerFragment
to check the path where the file is stored, for me (in MacOS) is something like this: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 yourbin
folderNow open your app
build.gradle
file and change this lineimplementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
adding the
, '*.aar'
part in the include arrayMove the generated
dagger-android-support-2.21.aar
frombin
tolibs
folder inside your project.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 usesandroidx.fragment.app.Fragment
I had the same problem in case of HasFragmentInjector. You need to use HasSupportFragmentInjector for fragment injection. This is because,
HasFragmentInjector
usesandroid.app.Fragment
which is not effected byjetifier
. You need to addandroid-dagger-support
library,jetifier
converts all thesupport packages
toandroidx
in Studio 3.3 (ifjetifier
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.Then add the library to your project. This is the HasSupportFragment class after conversion
Just for reference. i had the same problem. It was Jetifier issue. please upgrade your gradle build tools plugin to 3.3.0
Sample code: https://github.com/jega-ms/android-dagger2-mvp-rx