Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting Androidx Fragments using Dagger 2

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.

like image 445
mahdi jamshidian Avatar asked Aug 13 '18 17:08

mahdi jamshidian


People also ask

How do you inject a ViewModel with a dagger?

Setup a Dagger 2 Module Then setup the Dagger 2 Module with that provide the ViewModel. Like normal ViewModel creation, use ViewModelProvider and the Factory to create it. We'll need to pass in the dependencies which can be automatically injected by Dagger 2 using the conventional approach.

What is dagger 2 used for?

Dagger is arguably the most used Dependency Injection, or DI, framework for Android. Many Android projects use Dagger to simplify building and providing dependencies across the app. It gives you the ability to create specific scopes, modules, and components, where each forms a piece of a puzzle: The dependency graph.

How does dagger injection work?

Dagger automatically generates code that mimics the code you would otherwise have hand-written. Because the code is generated at compile time, it's traceable and more performant than other reflection-based solutions such as Guice. Note: Use Hilt for dependency injection on Android.


3 Answers

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)
like image 52
Sirojiddin Komolov Avatar answered Oct 20 '22 10:10

Sirojiddin Komolov


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

like image 14
Saeed Masoumi Avatar answered Oct 20 '22 10:10

Saeed Masoumi


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

like image 10
Jimit Patel Avatar answered Oct 20 '22 12:10

Jimit Patel