[英]Cannot be Provided Without an @Provides-annotated Method
There have been many other similar questions, but none of the answers have been applicable to my code. 还有许多其他类似的问题,但是没有一个答案适用于我的代码。 I cannot figure out what I have done wrong.
我无法弄清楚我做错了什么。
First I have a NetworkModule
that is used as a module for the ApplicationComponent
: 首先,我有一个
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 {}
Then the ApplicationComponent
is built in my Application
class: 然后
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()
}
// ...
}
Then I have an ActivityModule
that is used in the ActivityComponent
(which has ApplicationComponent
as a dependency): 然后我有一个
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)
}
Finally, I create a new ActivityComponent
in the MainActivity
and @Inject
the ArticleService
: 最后,我在
MainActivity
创建一个新的ActivityComponent
,并@Inject
the 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) })
}
// ...
}
But when I try to build I get the following error, even though I believe the provideArticleService()
function in NetworkModule
is annotated correctly: 但是,当我尝试构建时,即使我相信
NetworkModule
的provideArticleService()
函数已正确标注,也会出现以下错误:
ArticleService cannot be provided without an @Provides- or @Produces-annotated method.
没有@Provides或@Produces批注的方法就无法提供ArticleService。
You're missing the provision methods to inherit to your Activity scoped component. 您缺少了继承到Activity范围内的组件的置备方法。 Either use subcomponents instead of component dependency, or define the provision methods in your application component.
使用子组件代替组件依赖关系,或者在应用程序组件中定义供应方法。
@ApplicationScope @Component(modules = arrayOf(ContextModule::class, RealmModule::class, NetworkModule::class))
interface ApplicationComponent {
ArticleService articleService();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.