简体   繁体   中英

Android Dagger - Hilt ViewModel can't detect dependency

I am facing runtime issue while running the application. I am new to dagger and hilt. Please help me out to resolve the issue. Build Issue:

error: [Dagger/DependencyCycle] Found a dependency cycle:
  public abstract static class SingletonC implements AppController_GeneratedInjector,
                         ^
      com.hilt.hiltsampleproject.app.ApiHelper is injected at
          com.hilt.hiltsampleproject.app.ApiHelperImpl(apiHelper)
      com.hilt.hiltsampleproject.app.ApiHelperImpl is injected at
          com.hilt.hiltsampleproject.app.AppModule.provideApiHelper(apiHelper)
      com.hilt.hiltsampleproject.app.ApiHelper is injected at
          com.hilt.hiltsampleproject.repositories.MainRepository(apiHelper)
      com.hilt.hiltsampleproject.repositories.MainRepository is injected at
          com.hilt.hiltsampleproject.ui.posts.PostViewModel(mainRepository)
      javax.inject.Provider<com.hilt.hiltsampleproject.ui.posts.PostViewModel> is injected at
          com.hilt.hiltsampleproject.app.ViewModelByDaggerFactory(viewModelProvider)
      com.hilt.hiltsampleproject.app.ViewModelByDaggerFactory<com.hilt.hiltsampleproject.ui.posts.PostViewModel> is injected at
          com.hilt.hiltsampleproject.ui.posts.PostFragment.viewModelFactory
      com.hilt.hiltsampleproject.ui.posts.PostFragment is injected at
          com.hilt.hiltsampleproject.ui.posts.PostFragment_GeneratedInjector.injectPostFragment(com.hilt.hiltsampleproject.ui.posts.PostFragment) [com.hilt.hiltsampleproject.app.AppController_HiltComponents.SingletonC ? com.hilt.hiltsampleproject.app.AppController_HiltComponents.ActivityRetainedC ? com.hilt.hiltsampleproject.app.AppController_HiltComponents.ActivityC ? com.hilt.hiltsampleproject.app.AppController_HiltComponents.FragmentC]

Gradle Dependencies:

implementation 'com.google.dagger:hilt-android:2.33-beta'
kapt 'com.google.dagger:hilt-compiler:2.33-beta'

// Hilt Jetpack Integrations
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
annotationProcessor 'androidx.hilt:hilt-compiler:1.0.0-beta01'

PostFragment:

private val postViewModel: PostViewModel by viewModels()

PostViewModel:

@HiltViewModel class PostViewModel @Inject constructor(private val mainRepository: MainRepository) : ViewModel()

MainRespository:

class MainRepository @Inject constructor(
private val apiHelper: ApiHelper)

ApiHelper:

interface ApiHelper {
suspend fun getPosts(): Response<Posts>}

I took a look at the github sample.

The issue is that you have a DependencyCycle:

class ApiHelperImpl @Inject constructor(
    private val apiHelper: ApiHelper
) : ApiHelper {
...
}

You're trying to Construct/Provide an instance of ApiHelper which also takes an instance of ApiHelper so, to construct the first ApiHelper, Hilt/Dagger have to construct the dependencies, so this ApiHelperImpl needs a ApiHelper... which needs a ApiHelper... which needs an ApiHelper... you hopefully get the idea:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM