18

I want to inject my Androidx fragments using dagger 2. In my activity I have:

public class MainActivity extends AppCompatActivity implements HasSupportFragmentInjector { @Inject Repository repository; @Inject DispatchingAndroidInjector<androidx.fragment.app.Fragment> dispatchingAndroidInjector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public AndroidInjector<androidx.fragment.app.Fragment> supportFragmentInjector() { return dispatchingAndroidInjector; } } 

the problem is when i want to build the project i get this error:

error: cannot find symbol class MapBuilder 

and when i change androidx.fragment.app.Fragment to Fragment in DispatchingAndroidInjector i don't get this error any more.

5 Answers 5

15

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) 
Sign up to request clarification or add additional context in comments.

Comments

14

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:

3 Comments

thank you for your answer. it worked, but i had to upgrade dagger to 2.16 version .
It still doesn't accept android x Fragment even if I enable jetifier Dagger version 2.19
I'm using Dagger 2.21 with androidx and it works but you have to use AndroidSupportInjection.inject(this) as @cherif mentioned below.
10

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

3 Comments

Since you are commenting now, have you tried koin? If you are using Kotlin, and require to inject, compare Koin vs Dagger and use. github.com/InsertKoinIO/koin
@jimt patel yes i tried it. Was stuck since two days and now it's resolved. Thanks!
yeah i'm using kotlin and sure will try koin.
4

as was suggested before, 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)

Comments

2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.