gpt4 book ai didi

java - Android Kotlin Dagger 2 组件未生成

转载 作者:行者123 更新时间:2023-12-02 09:43:02 24 4
gpt4 key购买 nike

我不知道为什么重建项目后没有生成我的 DaggerComponent。

@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class NewsScope
<小时/>
@NewsScope
@Component(
dependencies = [BaseComponent::class],
modules = [NewsModule::class]
)
interface NewsComponent {
fun newsManager(): NewsManager
}
<小时/>
@NewsScope
@Component(
dependencies = [BaseComponent::class],
modules = [NewsModule::class]
)
interface NewsComponent {
fun newsManager(): NewsManager
}
<小时/>
@Module
class NewsModule {

@NewsScope
@Provides
fun provideMoshiConverterFactory(): MoshiConverterFactory {
val moshi = Moshi.Builder().build()
return MoshiConverterFactory.create(moshi)
}

@NewsScope
@Provides
fun provideNewsApi(okHttpClient: OkHttpClient): NewsApi {
val moshi = Moshi.Builder().build()

val retrofit = Retrofit.Builder()
.client(okHttpClient)
.baseUrl(NewsApi.API_URL)
.addCallAdapterFactory(RxJavaCallAdapterFactory
.createWithScheduler(Schedulers.io()))
.addConverterFactory(MoshiConverterFactory.create(moshi))
.build()

val retrofitApi = retrofit.create(RetrofitNewsApi::class.java)

return RetrofitNewsApiImpl(retrofitApi)
}

@NewsScope
@Provides
fun provideNewsManager(newsApi: NewsApi): NewsManager {
return NewsManager(newsApi)
}
}
<小时/>
class NewsFragment : BaseFragment() {
@Inject lateinit var newsManager: NewsManager private set


override fun onCreate(savedInstanceState: Bundle?) {
newsManager = DaggerNewsComponent.builder()
.baseComponent(baseComponent())
.newsModule(NewsModule())
.build().newsManager()
}
}
  • *** DaggerNewsComponent 未生成
  • $dagger_version = "2.23.2"
  • AndroidX 已实现
  • 我是 iOS 专业人士,但 Android 菜鸟

    实现“com.google.dagger:dagger:$dagger_version”实现“com.google.dagger:dagger-android:$dagger_version”实现“com.google.dagger:dagger-android-support:$dagger_version”

    annotationProcessor“com.google.dagger:dagger-android-processor:$dagger_version”注释处理器“com.google.dagger:dagger-compiler:$dagger_version”

最佳答案

您应该在 annotationProcessor 旁边使用 kapt

kapt 'com.google.dagger:dagger-android-processor:2.23.2'
kapt 'com.google.dagger:dagger-compiler:2.23.2'

并在 build.gradle 顶部添加 apply plugin: 'kotlin-kapt'

关于java - Android Kotlin Dagger 2 组件未生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56906595/

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