简体   繁体   中英

Dagger 2: Unable to find generated class in Intellij Idea (Kotlin)

I was trying to write a application to understand spark and dagger 2. But was unable to use generated dagger files.

There are multiple questions similar to this one, but I am not able to solve the problem using any of those.

My project can be found at github here

The build.gradle file looks like this

...
apply plugin: 'kotlin-kapt'
dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
    compile "com.sparkjava:spark-kotlin:$spark_kotlin_version"
    compile "org.slf4j:slf4j-log4j12:$slf4j_version"
    compile "com.google.dagger:dagger:$dagger_version"
    kapt "com.google.dagger:dagger-compiler:$dagger_version"

    testCompile group: 'junit', name: 'junit', version: '4.12'
}
....

It's the SparkSetup.kt Class that I am trying to inject. Module and Component for this are present in co.pissarra.util.dagger package

The contents of SetupModule.kt looks like this

@Module
class SetUpModule {
    @Provides
    @Singleton
    fun provideSparkSetup() : SparkSetup {
        return SparkSetup()
    }
}

And this is AppComponent.kt Class

@Singleton
@Component(modules = arrayOf(SetUpModule::class))
interface AppComponent {
    fun sparkSetup() : SparkSetup
}

Ideally there should be a class by the name DaggerAppComponent , which infact is present in the build directory (both generated and classes). But this only works if I am not using DaggerAppComponent anywhere in the project. If I try using this, intellij idea gives the error that Unresolved reference: DaggerAppComponent and the project also fails to build.

I would like to change the file ContainerRunner.kt and instead of calling SparkSetup().init() directly, I would like to have that done through DI.

This looks like KT-17923 . Please update your Kotlin IDE plugin version to 1.1.60 or 1.2-Beta.

Also, as a work-around, you can specify the generated directories manually:

apply plugin: 'idea'
idea {
    module {
        sourceDirs += file('build/generated/source/kapt/main')
        generatedSourceDirs += file('build/generated/source/kapt/main')
    }
}

Note that you need to re-import project after adding this to your build.gradle .

The only thing that seems strange for me is that the error also reproduces during the build. Please check if DaggerAppComponent lays in the same package with the class that references it (or there is an import directive).

To me, it looks like an IDE (AndroidStudio / IntelliJ) bug.

Facts:

  • Generated classes are present in path build/generated/source/kapt/main
  • Code that uses these generated classes actually works even tough that IDE marks it with red color and Unresolved reference error

Fix:

sourceSets {
    main.java.srcDirs += "$buildDir/generated/source/kapt/main"
}

If you use protobuf there is a similar problem where generated proto classes are not visible (again Unresolved reference error)

Fix:

sourceSets {
    main {
        java {
            srcDirs += file("${protobuf.generatedFilesBaseDir}/main/java")
            srcDirs += file("${protobuf.generatedFilesBaseDir}/main/javalite")
            srcDirs += file("${protobuf.generatedFilesBaseDir}/main/grpc")
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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