未生成Android Kotlin Dagger 2组件

时间:2019-07-05 16:34:07

标签: java android kotlin dagger-2 dagger

我不知道为什么重建项目后未生成我的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 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”

2 个答案:

答案 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
    }
}