如果没有@Provides注释的方法,则无法提供AndroidInjector.Factory

时间:2018-08-13 17:19:03

标签: android dependency-injection dagger-2 dagger

我想在我的应用程序中使用Dagger2,但是由于某些原因,我无法编译代码,因为出现以下错误:

 AndroidInjector.Factory<? extends android.app.Activity>>> cannot be provided without an @Provides-annotated method.

这是我的ApiModule(我只有的模块

@Module
class ApiModule {

    @Singleton
    @Provides
    fun provideLoggingInterceptor() : HttpLoggingInterceptor {
        val httpLoggingInterceptor = HttpLoggingInterceptor()
        httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
        return httpLoggingInterceptor
    }

    @Singleton
    @Provides
    fun provideRetrofit(gson: Gson, okHttpClient: OkHttpClient) : Retrofit {
        return Retrofit.Builder()
                .baseUrl(BuildConfig.API_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(okHttpClient)
                .build()
    }

    @Singleton
    @Provides
    fun provideApiService(retrofit: Retrofit) : PHService {
        return retrofit.create(PHService::class.java)
    }
}

这是我的应用程序组件

    @Singleton
    @Component(modules = [
        ApiModule::class
    ])
    interface AppComponent : AndroidInjector<App> {

        @Component.Builder
        interface Builder {
            @BindsInstance
            fun application(application: Application): Builder
            fun build(): AppComponent
        }
        override fun inject(app: App)
    }

This is my Application class:

open class App : DaggerApplication() {
    override fun applicationInjector() = DaggerAppComponent.builder().application(this).build()
}

在我的主要活动中,使用以下命令:

class MainActivity : DaggerAppCompatActivity() 

0 个答案:

没有答案