java - Kotlin + Dagger 2 : Dagger* files won't generate

标签 java dependency-injection kotlin dagger-2

我第一次开始同时使用 Kotlin 和 Dagger 2。我假设一切都与 Java 中的相同,但显然不完全相同。 Dagger 不会为我生成 Dagger* 文件。这是我的代码:

组件:

@PerActivity
@Subcomponent(modules = arrayOf(ApplicationModule::class))
interface ActivityComponent {
    fun inject(app: OneAccountApplication)
}

@Singleton
@Component(modules = arrayOf(ApplicationModule::class))
interface ApplicationComponent {

fun inject(syncService: SyncService)
@ApplicationContext fun context(): Context
fun application(): Application
fun ribotsService(): OneAccountService
fun preferencesHelper(): PreferencesHelper
fun databaseHelper(): DatabaseHelper
fun dataManager(): DataManager
}

@ConfigPersistent
@Component(dependencies = arrayOf(ApplicationComponent::class))
interface ConfigPersistentComponent {
    fun activityComponent(activityModule: ActivityModule): ActivityComponent
}

模块:

@Module
class ActivityModule(private val mActivity: Activity) {

    @Provides
    internal fun provideActivity() = mActivity

    @Provides
    @ActivityContext
    internal fun providesContext() = mActivity
}

@Module
class ApplicationModule(val mApplication: Application) {

    @Provides @Singleton
    internal fun provideApplication() = mApplication

    @Provides
    @ApplicationContext
    internal fun provideContext() = mApplication

    @Provides
    @Singleton
    internal fun provideOneAccountService() = OneAccountService.Creator.newOneAccountService()
}

作用域注解:

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityContext

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class ApplicationContext

@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class ConfigPersistent

@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class PerActivity

这基本上是我在 Java 代码中成功使用的整个 DI 系统。但是对于 Kotlin,由于某种原因它不起作用。我还添加了:apply plugin: 'kotlin-kapt'gradle.build 就像:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

在依赖项中我有:

dependencies {

    final DAGGER_VERSION = '2.8'
    def daggerCompiler = "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
    annotationProcessor daggerCompiler
    testAnnotationProcessor daggerCompiler
    androidTestAnnotationProcessor daggerCompiler
    compile  "com.google.dagger:dagger:$DAGGER_VERSION"
}
kapt {
    generateStubs = true
}

基本上就是这个https://github.com/ribot/android-boilerplate转化为 Kotlin。

最佳答案

Kotlin 使用 kapt 而不是 Android 插件中的 annotationProcessor

您需要在 build.gradle 文件中包含并使用以下内容:

kapt {
    generateStubs = true
}
dependencies {
    // ...
    kapt daggerCompiler
}

更详细的说明也可以在这里找到:Dagger and Kotlin

关于java - Kotlin + Dagger 2 : Dagger* files won't generate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44976746/

相关文章:

java - 从 SecureRandom 获取确定性值?

java - 在运行时获取java字段和方法描述符

java - Spring Boot如何管理分布式系统中的bean?

c# - Spring.NET 和即时 CMS

android - 使用 androids 可视化器类获取可变频率范围

java - 非随机散列函数导致的应用程序漏洞

unit-testing - 使用 Docker 和依赖注入(inject)框架

unit-testing - 具有文件系统依赖性的单元测试代码

android - 从 kotlin REPL 和临时文件中使用 json 类时出错

android - 单击 RecyclerView 后在主要 Activity 中更新 TextView