Anton Makov
Anton Makov

Reputation: 845

Dagger 2 ViewModelProvider.Factory bound multiple times

I'm trying to create an application while using fragments. I created a test fragment (HomeFragment) it has only a simple TextView. I created all the necessary classes (module, model and provider). But I'm getting a strange complication error

Error:(25, 10) error: [dagger.android.AndroidInjector.inject(T)] android.arch.lifecycle.ViewModelProvider.Factory is bound multiple times:
@Provides android.arch.lifecycle.ViewModelProvider.Factory app.series.com.series3go.ui.main.MainActivityModule.mainViewModelProvider(app.series.com.series3go.ui.main.MainViewModel)
@Provides android.arch.lifecycle.ViewModelProvider.Factory app.series.com.series3go.ui.home.HomeFragmentModule.provideHomeFragmentViewModel(app.series.com.series3go.ui.home.HomeFragmentViewModel)

HomeFragment

public class HomeFragment extends BaseFragment<HomeFragmentBinding, HomeFragmentViewModel> {

public static final String TAG = HomeFragment.class.getSimpleName();
@Inject
ViewModelProvider.Factory mViewModelFactory;


HomeFragmentBinding mHomeFragmentBinding;
private HomeFragmentViewModel mHomeFragmentViewModel;

public static HomeFragment newInstance() {
    Bundle args = new Bundle();
    HomeFragment fragment = new HomeFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mHomeFragmentBinding = getViewDataBinding();
    setUp();
    subscribeToLiveData();
}

@Override
public HomeFragmentViewModel getViewModel() {
    mHomeFragmentViewModel = ViewModelProviders.of(this, mViewModelFactory).get(HomeFragmentViewModel.class);
    return mHomeFragmentViewModel;
}

@Override
public int getBindingVariable() {
    return BR.viewModel;
}

@Override
public int getLayoutId() {
    return R.layout.home_fragment;
}

private void setUp() {

}

private void subscribeToLiveData() {

}
@Override
public void onDestroyView() {
    super.onDestroyView();
}


}

HomeFragmentModule

@Module
public class HomeFragmentModule
{

    @Provides
    HomeFragmentViewModel homeFragmentViewModel()
    {
        return new HomeFragmentViewModel();
    }

    @Provides
    ViewModelProvider.Factory provideHomeFragmentViewModel(HomeFragmentViewModel homeFragmentViewModel)
    {
        return new ViewModelProviderFactory<>(homeFragmentViewModel);
    }

}

HomeFragmentProvider

@Module
public abstract class HomeFragmentProvider {

    @ContributesAndroidInjector(modules = HomeFragmentModule.class)
    abstract HomeFragment provideHomeFragmentFactory();

} 

HomeFragmentViewModel

public class HomeFragmentViewModel extends BaseViewModel {

private final ObservableField<String> appVersion = new ObservableField<>();

public HomeFragmentViewModel() {
    super();
    appVersion.set("123");
}

public ObservableField<String> getAppVersion() {
    return appVersion;
}

}

home_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="app.series.com.series3go.ui.main.MainActivity">

    <data>

        <import type="android.view.View" />

        <variable
            name="viewModel"
            type="app.series.com.series3go.ui.home.HomeFragmentViewModel" />

    </data>


    <TextView
        android:id="@+id/tvAppVersion"
        style="@style/TextStyle.Title.Sub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:padding="5dp"
        android:text="@{viewModel.appVersion}" />

</layout>

Is there a naming convention about function names in provider class(HomeFragmentProvider), I didn't see any use of those functions any where in the project. Are they used in the generated classes of dagger?

Thanks

UPDATE

AppComponent

@Singleton
@Component(modules = {AndroidSupportInjectionModule.class, AppModule.class, ActivityBuilder.class})
public interface AppComponent {

    @Component.Builder
    interface Builder {

        @BindsInstance
        Builder application(Application application);

        AppComponent build();

    }

    void inject(SeriesApp app);

}

Upvotes: 5

Views: 15491

Answers (2)

dev.bmax
dev.bmax

Reputation: 10591

Looks like you have two methods in your dependency graph that provide ViewModelProvider.Factory.

To solve this ambiguity, use @Named annotation.

@Module
public class HomeFragmentModule {
    @Provides
    @Named("HomeFragment")
    ViewModelProvider.Factory provideHomeFragmentViewModel(HomeFragmentViewModel homeFragmentViewModel) {
        return new ViewModelProviderFactory<>(homeFragmentViewModel);
    }
    /* Rest of the code */
}

public class HomeFragment {
    @Inject
    @Named("HomeFragment")
    ViewModelProvider.Factory mViewModelFactory;
    /* Rest of the code */
}

You should do the same for the second ViewModelProvider.Factory too (in MainActivity and it's module).

Upvotes: 24

azizbekian
azizbekian

Reputation: 62189

I will edit error log in order to make it a bit more understandable:

Error: ViewModelProvider.Factory is bound multiple times:

@Provides ViewModelProvider.Factory MainActivityModule.mainViewModelProvider(MainViewModel)

@Provides ViewModelProvider.Factory HomeFragmentModule.provideHomeFragmentViewModel(HomeFragmentViewModel)

You have declared inside HomeFragment, that you are willing to inject ViewModelProvider.Factory. Dagger tries to find a provider method and finds two of them: one is being provided from MainActivityModule, the other from HomeFragmentModule. So, dagger gets confused and aborts compilation.

I would suggest you to adopt the approach similar to what is present in Google's showcase GithubBrowserSample app. In that app ViewModels are being injected into a map (Map<Class, ViewModel>) using @IntoMap annotation. In your case you would inject ViewModelProvider.Factory-ies into map.

You can see the injection of the map inside GithubViewModelFactory.

Alternatively, you may consider approach suggested by @dev.bmax.

Upvotes: 1

Related Questions