如果没有@ Provide-annotated方法

时间:2016-08-25 18:53:30

标签: android kotlin dagger-2

还有许多其他类似的问题,但没有一个答案适用于我的代码。我无法弄清楚我做错了什么。

首先,我有NetworkModule用作ApplicationComponent的模块:

@Module
open class NetworkModule {

    companion object {
        private val BASE = "http://www.example.com/"
    }

    @Provides @ApplicationScope
    fun provideClient(): OkHttpClient = OkHttpClient()

    @Provides @ApplicationScope
    fun provideMoshi(): Moshi {
        return Moshi.Builder().add(InstantAdapter).add(UriAdapter).build()
    }

    @Provides @ApplicationScope
    fun provideRetrofit(client: OkHttpClient, moshi: Moshi): Retrofit {
        return Retrofit.Builder().client(client).baseUrl(BASE)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(MoshiConverterFactory.create(moshi))
                .build()
    }

    @Provides @ApplicationScope
    fun provideArticleService(retrofit: Retrofit): ArticleService {
        return retrofit.create(ArticleService::class.java)
    }

}
@ApplicationScope @Component(modules = arrayOf(ContextModule::class, RealmModule::class, NetworkModule::class))
interface ApplicationComponent {}

然后ApplicationComponent内置Application类:

class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        AndroidThreeTen.init(this)

        plantLog()
        drawDagger()
    }

    private fun drawDagger() {
        Injector.initializeApplicationComponent(this)
    }

    // ...

}
object Injector {

    lateinit var applicationComponent: ApplicationComponent
        private set

    fun initializeApplicationComponent(context: Context) {
        applicationComponent = DaggerApplicationComponent.builder()
                .contextModule(ContextModule(context))
                .networkModule(NetworkModule())
                .realmModule(RealmModule())
                .build()
    }

    // ...

}

然后我在ActivityModuleActivityComponent作为依赖项)中使用了ApplicationComponent

@Module
open class ActivityModule(private val activity: AppCompatActivity) {

    @Provides @ActivityScope @ActivityContext
    fun provideContext(): Context = activity

    @Provides @ActivityScope
    fun provideFragmentManager(): FragmentManager = activity.supportFragmentManager

}
@ActivityScope @Component(dependencies = arrayOf(ApplicationComponent::class), modules = arrayOf(ActivityModule::class))
interface ActivityComponent {

    fun inject(activity: MainActivity)

}

最后,我在ActivityComponentMainActivity @Inject中创建了一个新的ArticleService

class MainActivity : AppCompatActivity() {

    @Inject lateinit var service: ArticleService

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        DaggerActivityComponent.builder()
                .applicationComponent(Injector.applicationComponent)
                .activityModule(ActivityModule(this))
                .build().inject(this)

        service.getNewsArticles()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread())
                .subscribe(
                        { response -> onNext(response) },
                        { error -> onError(error) })
    }

    // ...

}

但是当我尝试构建时,我得到以下错误,即使我相信provideArticleService()中的NetworkModule函数已正确注释:

  

如果没有@ Provide-或者,则无法提供ArticleService   @ Produces-annotated方法。

1 个答案:

答案 0 :(得分:2)

您缺少要继承到Activity范围组件的提供方法。使用子组件而不是组件依赖项,或在应用程序组件中定义提供方法。

 @ApplicationScope @Component(modules = arrayOf(ContextModule::class, RealmModule::class, NetworkModule::class))
 interface ApplicationComponent {
      ArticleService articleService();
 }