有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我第一次开始同时使用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系统。但对于科特林来说,由于某种原因,它不起作用。我还添加了:apply plugin: 'kotlin-kapt'gradle.build类似:

apply plugin: 'com.安卓.application'
apply plugin: 'kotlin-安卓'
apply plugin: 'kotlin-kapt'

在依赖性方面,我有:

dependencies {

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

基本上,这个ithttps://github.com/ribot/安卓-boilerplate转化为Kotlin


共 (2) 个答案

  1. # 1 楼答案

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

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

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

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

  2. # 2 楼答案

    我用这个渐变,效果很好

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