Reputation: 163
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.
Upvotes: 3
Views: 2339
Reputation: 307
To me, it looks like an IDE (AndroidStudio / IntelliJ) bug.
Facts:
build/generated/source/kapt/main
Unresolved reference
errorFix:
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")
}
}
}
Upvotes: 2
Reputation: 1109
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).
Upvotes: 3