谁能告诉我为什么这个错误。我尝试了很多堆叠溢出的帖子,但无法识别。我整天都在为这个错误而挣扎。在匕首2.11之前,它运转良好。在更新到2.11之后是错误。
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)这是我在我的项目中添加的AppComponent类。它不完全像错误所显示的那样抽象。
@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)
}下面是我在我的项目中添加的应用程序模块类。
@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()*/这是我的AccountModule课
@Module
class AccountModule {
@Provides
internal fun provideAccountInteractor(accountInteractor: AccountInteractor): AccountInteractorContract = accountInteractor
@Provides
internal fun provideAccountPresenter(accountPresenter: AccountPresenter<AccountViewContract, AccountInteractorContract>)
: AccountPresenterContract<AccountViewContract, AccountInteractorContract> = accountPresenter
}这是我的Retrofit服务界面
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);
}这是我的改装模块类
@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
}
}这是我的应用程序课
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)
}
}发布于 2018-05-11 14:03:26
您的依赖周期在这里:
@Provides
@Singleton
internal fun provideAPI(apiManager: NccApi): NccApi = apiManager您依赖于apiManager来提供apiManager。你应该在这里有这样的东西:
@Provides
@Singleton
internal fun provideAPI(): NccApi = NccApi()以上只是一个例子,重点是您需要实例化api来提供它。或者,如果您有一个apiManager实现的接口,您应该从provide方法中返回该接口,因此您请求一个实际的对象并返回该接口。如果请求一个实际的对象并返回它,您就会在那里执行依赖周期。
https://stackoverflow.com/questions/50293229
复制相似问题