Can any one tell me why this error is showing. I tried a lot some stackoverflow post, but can not identify. Am struggling with this error for whole day. before dagger 2.11 it was working fine. after updating to 2.11 is the error.
E:\studio projects\MVP-Login\app\build\tmp\kapt3\stubs\debug\com\example\anbu\mvpkotlin\di\component\AppComponent.java:8: error: [dagger.android.AndroidInjector.inject(T)] Found a dependency cycle:
public abstract interface AppComponent {
^
com.example.anbu.mvpkotlin.data.network.NccApi is injected at
com.example.anbu.mvpkotlin.di.modules.AppModules.provideAPI$app_debug(apiManager)
com.example.anbu.mvpkotlin.data.network.NccApi is injected at
com.example.anbu.mvpkotlin.ui.accounts.interactor.AccountInteractor.<init>(…, api)
com.example.anbu.mvpkotlin.ui.accounts.interactor.AccountInteractor is injected at
com.example.anbu.mvpkotlin.ui.accounts.AccountModule.provideAccountInteractor$app_debug(accountInteractor)
com.example.anbu.mvpkotlin.ui.accounts.interactor.AccountInteractorContract is injected at
com.example.anbu.mvpkotlin.ui.accounts.presenter.AccountPresenter.<init>(interactor, …)
com.example.anbu.mvpkotlin.ui.accounts.presenter.AccountPresenter<com.example.anbu.mvpkotlin.ui.accounts.view.AccountViewContract,com.example.anbu.mvpkotlin.ui.accounts.interactor.AccountInteractorContract> is injected at
com.example.anbu.mvpkotlin.ui.accounts.AccountModule.provideAccountPresenter$app_debug(accountPresenter)
com.example.anbu.mvpkotlin.ui.accounts.presenter.AccountPresenterContract<com.example.anbu.mvpkotlin.ui.accounts.view.AccountViewContract,com.example.anbu.mvpkotlin.ui.accounts.interactor.AccountInteractorContract> is injected at
com.example.anbu.mvpkotlin.ui.accounts.view.AccountActivity.presenter
com.example.anbu.mvpkotlin.ui.accounts.view.AccountActivity is injected at
dagger.android.AndroidInjector.inject(arg0)
Here is my AppComponent class I have added in my project. It is not exactly abstract as the error shows.
@Singleton
@Component(modules = [(AndroidInjectionModule::class), (ActivityBuilder::class),
(AppModules::class)])
interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application): Builder
fun build(): AppComponent
}
fun inject(app: MyApplication)
}
And here is Application Module class I have added in my project.
@Module
class AppModules {
@Provides
@Singleton
internal fun provideContext(application: Application): Context = application
@Provides
@PreferenceInfo
internal fun providePrefName(): String = AppConstants.PREF_NAME
@Provides
@Singleton
internal fun providePreference(prefManager: PrefManager):
PrefManagerContract = prefManager
@Provides
@Singleton
internal fun provideAppDatabase(context: Context): NccDatabase =
Room.databaseBuilder(context, NccDatabase::class.java, "****")
.allowMainThreadQueries()
.fallbackToDestructiveMigration()
.build()
@Provides
internal fun provideCompositeDisposable(): CompositeDisposable =
CompositeDisposable()
@Provides
@Singleton
internal fun provideAPI(apiManager: NccApi): NccApi = apiManager
@Provides
internal fun provideSchedulerProvider(): SchedulerProvider =
SchedulerProvider()
/*@Provides
@Singleton
internal fun providerTokenManager(): TokenManager = TokenManager()*/
This is my AccountModule class
@Module
class AccountModule {
@Provides
internal fun provideAccountInteractor(accountInteractor: AccountInteractor): AccountInteractorContract = accountInteractor
@Provides
internal fun provideAccountPresenter(accountPresenter: AccountPresenter<AccountViewContract, AccountInteractorContract>)
: AccountPresenterContract<AccountViewContract, AccountInteractorContract> = accountPresenter
}
Here is my Retrofit web services interface
public interface NccApi {
Observable<Profile> getProfile(@Url String url, @Header("authorization") String auth);
@POST
Call<SigninResponseModel> refreshToken(@Url String url, @Header("authorization") String auth, @Body RefreshTokenRequest body);
}
Here is my retrofit module class
@Module
class RetrofitModule(internal var mBaseUrl: String) {
@Provides
@Singleton
internal fun provideInterceptor(): okhttp3.Interceptor {
val interceptorAPI = Interceptor { chain ->
val original = chain.request()
var request: Request? = null
try {
request = chain.request().newBuilder()
.addHeader("Content-Type", "application/json")
.method(original.method(), original.body())
.build()
} catch (authFailureError: Exception) {
authFailureError.printStackTrace()
}
val response = chain.proceed(request)
response
}
return interceptorAPI
}
@Provides
@Singleton
internal fun provideGson(): Gson {
val gsonBuilder = GsonBuilder()
return gsonBuilder.create()
}
@Provides
@Singleton
internal fun provideOkHttpClient(interceptor: Interceptor): OkHttpClient {
val okHttpBuilder = OkHttpClient.Builder()
okHttpBuilder.interceptors().add(interceptor)
val logging = HttpLoggingInterceptor()
logging.setLevel(HttpLoggingInterceptor.Level.BODY)
okHttpBuilder.interceptors().add(logging)
val client = okHttpBuilder.build()
return client
}
@Provides
@Singleton
internal fun provideRetrofit(gson: Gson, okHttpClient: OkHttpClient): Retrofit {
val retrofit = Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(mBaseUrl)
.client(okHttpClient)
.build()
return retrofit
}
}
Here is my Application class
class MyApplication : Application(), HasActivityInjector {
@Inject
lateinit internal var activityDispatchingAndroidInjector: DispatchingAndroidInjector<Activity>
override fun activityInjector() = activityDispatchingAndroidInjector
override fun onCreate() {
super.onCreate()
DaggerAppComponent.builder()
.application(this)
.appmodule(AppModules())
.retrofitModule(RetrofitModule(""))
.build()
.inject(this)
}
}
Your dependency cycle is here:
@Provides
@Singleton
internal fun provideAPI(apiManager: NccApi): NccApi = apiManager
You're dependening on the apiManager to provide an apiManager. You should have something like this in here:
@Provides
@Singleton
internal fun provideAPI(): NccApi = NccApi()
The above is just an example, the point is that you need to instantiate your api to provide it. Or, if you have an interface that your apiManager implements you should return that from the provide method, so you request an actual object and returns the interface. Bu requesting an actual object and returning it you doing the dependency cycle right there.
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.