gpt4 book ai didi

android - 没有 @Provides-annotated 方法就无法提供

转载 作者:搜寻专家 更新时间:2023-11-01 08:33:02 25 4
gpt4 key购买 nike

还有很多其他类似的问题,但没有一个答案适用于我的代码。我不知道我做错了什么。

首先我有一个 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()
}

// ...

}

然后我有一个在 ActivityComponent 中使用的 ActivityModule(它有 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)

}

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

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

// ...

}

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

ArticleService cannot be provided without an @Provides- or @Produces-annotated method.

最佳答案

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

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

关于android - 没有 @Provides-annotated 方法就无法提供,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39152777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com