8

Trying to set up DI with a project I'm working on (one module per layer app), and I've ran into an issue that I don't know how to fix:

 public abstract static class SingletonC implements FragmentGetContextFix.FragmentGetContextFixEntryPoint,
 ^
 @Singleton @Provides @org.jetbrains.annotations.NotNull winged.example.data.DoggoApi winged.example.data.di.DataModule.provideDoggoApi()
 @Singleton @Provides @org.jetbrains.annotations.NotNull winged.example.data.DoggoApi winged.example.modularretrofitapp.NetworkingModule.provideDoggoApi(okhttp3.OkHttpClient)
 winged.example.data.DoggoApi is injected at
 winged.example.data.di.DataModule.provideRepository(api)
 winged.example.domain.repository.DoggoRepository is injected at
 winged.example.presentation.doggoFragment.DoggoFragmentViewModel(repository)
 winged.example.presentation.doggoFragment.DoggoFragmentViewModel is injected at
 winged.example.presentation.doggoFragment.DoggoFragmentViewModel_HiltModules.BindsModule.binds(arg0)
 @dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.String,javax.inject.Provider<androidx.lifecycle.ViewModel>> is requested at
 dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFac

The project structure is as follows:

  1. Application class in the app module:
@HiltAndroidApp
class DoggoApp: Application()

which is referenced in the manifest android:name=".DoggoApp"

  1. in the data module I have declared a hilt module:
@Module
@InstallIn(SingletonComponent::class)
object DataModule {
 @Singleton
 @Provides
 fun provideDoggoApi(): DoggoApi {
 return Retrofit.Builder()
 .baseUrl("https://dog.ceo/")
 .addConverterFactory(GsonConverterFactory.create())
 .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
 .build()
 .create(DoggoApi::class.java)
 }
 @Provides
 fun provideRepository(api: DoggoApi): DoggoRepository {
 return DoggoRepositoryImpl(api)
 }
}

and this is later injected into a view model which currently looks like this:

@HiltViewModel
class DoggoFragmentViewModel @Inject constructor(private val repository: DoggoRepository): ViewModel() {
 fun makeRequest() {
 repository.getRandomDoggo()
 }
}

what changes should I make to fix it? any links/hints/answers will be appreciated :)

asked Sep 16, 2022 at 20:02

1 Answer 1

6

It seems you are providing DoggoApi in two different modules, DataModule and NetworkingModule. You should have only one provider for a type in SingletonComponent, So you need to separate them using Qualifiers or remove either the provider in DataModule or NetworkingModule

answered Sep 16, 2022 at 20:14
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, I'm blind and forgot to remove that... thanks m8

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.