简体   繁体   English

Kotlin + Dagger 2:不会生成 Dagger* 文件

[英]Kotlin + Dagger 2: Dagger* files won't generate

I started using Kotlin and Dagger 2 together for the first time.我第一次开始同时使用 Kotlin 和 Dagger 2。 I assumed everything is the same as in Java, but apparently, not quite.我假设一切都与 Java 中的一样,但显然,不完全相同。 Dagger won't generate Dagger*files for me. Dagger 不会为我生成 Dagger* 文件。 Here is my code:这是我的代码:

Components:成分:

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

Modules:模块:

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

Scope Annotations:范围注释:

@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

This is basically my whole DI system which I was using in my java code successfully.这基本上是我在我的 java 代码中成功使用的整个 DI 系统。 But with Kotlin, for some reason it does not work.但是对于 Kotlin,由于某种原因它不起作用。 I have also added: apply plugin: 'kotlin-kapt' to gradle.build like:我还添加了: apply plugin: 'kotlin-kapt'gradle.build像:

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

And in dependencies I have:在依赖项中,我有:

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
}

Basically, this it https://github.com/ribot/android-boilerplate transformed into Kotlin.基本上,这https://github.com/ribot/android-boilerplate转化为 Kotlin。

Kotlin works with kapt instead of the annotationProcessor from the Android plugin. Kotlin使用kapt而不是Android插件中的annotationProcessor

You need to include and use the following in your build.gradle file: 您需要在build.gradle文件中包含并使用以下内容:

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

More detailed instructions can also be found here: Dagger and Kotlin 更详细的说明也可以在这里找到: Dagger和Kotlin

I use this gradle and is working fine我使用这个 gradle 并且工作正常

buildscript {

  val kotlinVersion = "1.4.20"

  repositories {
    mavenCentral()
  }
  dependencies {
    classpath ("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
  }
}
plugins {
  kotlin("jvm") version "1.5.30"
  kotlin("kapt") version "1.5.30"
  application
}


repositories {
  mavenCentral()
  google()
}

application {
  mainClass.set("dp.strategy.MiniDuckSimulator")
}

java {
  sourceCompatibility = JavaVersion.VERSION_1_8
  targetCompatibility = JavaVersion.VERSION_1_8
}


dependencies {
  implementation ("com.google.dagger:dagger:2.39.1")
  kapt ("com.google.dagger:dagger-compiler:2.39.1")
}

kapt {
  mapDiagnosticLocations = true // include the Kotlin files into error reports
}

kapt {
  javacOptions {
    // Increase the max count of errors from annotation processors.
    // Default is 100.
    option("-Xmaxerrs", 500)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM