错误:[dagger.android.AndroidInjector.inject(T)]找到一个依赖循环

时间:2018-05-11 13:17:24

标签: android android-studio dagger-2

任何人都可以告诉我为什么会出现这个错误。我尝试了很多stackoverflow帖子,但无法识别。我整天都在努力解决这个错误。在匕首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)

}

这是我在项目中添加的Application Module类。

@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 Web服务界面

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
    }
    }

这是我的Application类

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)

    }
    }

1 个答案:

答案 0 :(得分:3)

你的依赖周期在这里:

@Provides
@Singleton
internal fun provideAPI(apiManager: NccApi): NccApi = apiManager

您依赖apiManager来提供apiManager。你应该在这里有类似的东西:

@Provides
@Singleton
internal fun provideAPI(): NccApi = NccApi()

上面只是一个例子,重点是你需要实例化你的api来提供它。或者,如果你有一个apiManager实现的接口,你应该从provide方法返回它,所以你请求一个实际的对象并返回接口。请求实际对象并返回它,然后在那里进行依赖循环。