Injecting Androidx Fragments using Dagger 2

mahdi jamshidian picture mahdi jamshidian · Aug 13, 2018 · Viewed 9k times · Source

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.

Answer

Saeed Masoumi picture Saeed Masoumi · Aug 13, 2018

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: