Injecting Androidx Fragments using Dagger 2

Dagger supports were missing for AndroidX. It is added for version 2.21 and above

So you can use it as -

implementation 'com.google.dagger:dagger:2.21'
implementation 'com.google.dagger:dagger-android:2.21'
implementation 'com.google.dagger:dagger-android-support:2.21'
kapt "com.google.dagger:dagger-compiler:2.21"
kapt "com.google.dagger:dagger-android-processor:2.21"

Apart from this, if you are using for first time and migrating from support to AndroidX, you will also need to take care in gradle.properties as @Saeed Masoumi mentioned. You need to add following -

android.useAndroidX=true
android.enableJetifier=true

Jetifier will help you to migrate from support libraries to AndroidX packages at run time. Best answer you can find for that over here - https://stackoverflow.com/a/52002285/842607


The following worked for me:

First, add gradle dependency the dagger for support library:

implementation "com.google.dagger:dagger-android-support:2.23.2"

Then in your fragment which is the child of androidx.fragment inject in the following way:

AndroidSupportInjection.inject(this)

Androidx is not supported yet, but enabling jetifier may solve your problem.

Just add the below code to your gradle.properties

android.useAndroidX=true
android.enableJetifier=true

Also see these issues for detail:

  • migration to androidx library

  • AndroidInjection support for androidx Fragment