还有许多其他类似的问题,但没有一个答案适用于我的代码。我无法弄清楚我做错了什么。
首先,我有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()
}
// ...
}
然后我在ActivityModule
(ActivityComponent
作为依赖项)中使用了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)
}
最后,我在ActivityComponent
和MainActivity
@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方法。
答案 0 :(得分:2)
您缺少要继承到Activity范围组件的提供方法。使用子组件而不是组件依赖项,或在应用程序组件中定义提供方法。
@ApplicationScope @Component(modules = arrayOf(ContextModule::class, RealmModule::class, NetworkModule::class))
interface ApplicationComponent {
ArticleService articleService();
}