我不知道为什么重建项目后未生成我的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()
}
}
我是iOS Pro,但Android Noob
实现“ 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”
答案 0 :(得分:1)
您应该在kapt
旁边使用annotationProcessor
kapt 'com.google.dagger:dagger-android-processor:2.23.2'
kapt 'com.google.dagger:dagger-compiler:2.23.2'
并在您的apply plugin: 'kotlin-kapt'
顶部添加build.gradle
答案 1 :(得分:0)
是的,还添加
android {
kapt {
generateStubs = true
}
}