I tried creating a pure Kotlin/Gradle project in I...
# dagger
m
I tried creating a pure Kotlin/Gradle project in Intellij to play around with dagger (currently losing my mind over it lol) But even with very simple module/component it's not working as soon as I add a simple @provides or @binds annotation inside the module I get something along the lines of:
Copy code
[kapt] di.HeaterModule: Can't reference type 'Heater' from default package in Java stub.
as a warning and the error is:
Copy code
error: [ComponentProcessor:MiscError] dagger.internal.codegen.ComponentProcessor was unable to process this interface because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public abstract interface HeaterModule {
To be clear, this is my module:
Copy code
@Module
interface HeaterModule {

    //Tried both
//    @Binds
//    fun bindHeater(heater: SmallHeater): Heater
    
    @Provides
    fun provideHeater(): Heater {
        return SmallHeater()
    }
}
The component that doesn't even do anything:
Copy code
@Component(modules = [HeaterModule::class])
interface TeaComponent{
//    fun teaMaker(): TeaMaker
}
And the cherry on the top where I suspect the errors to be, but can't find them i.e.
build.gradle.kts
file:
Copy code
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("kapt") version "1.5.31"
    kotlin("jvm") version "1.5.10"
    application
}

group = "me.mkarwowski"
version = "1.0-SNAPSHOT"

kapt {
    generateStubs = true
    keepJavacAnnotationProcessors = true
}

repositories {
    mavenCentral()
}

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

    testImplementation(kotlin("test"))
}

tasks.test {
    useJUnit()
}

tasks.withType<KotlinCompile>() {
    kotlinOptions.jvmTarget = "1.8"
}

application {
    mainClass.set("MainKt")
}
Am I doing anything obviously wrong here? I'll upload it to github in a second so You can reproduce it
w
You definitely don’t need both
kapt
and
annotationProcessor
configurations, Dagger works just as well when running just in
kapt
I also don’t think
generateStubs = true
is still needed. Plus, you’re using different Kotlin and Kapt versions, which seems off
m
Honestly, sometimes I hate dagger with passion. I've spent last 2 days on it and it started working right after I posted the question... 🤦‍♂️ And I have no clue why wasn't it working anyway
Anyway, Thank You for looking into it. You re right about the versions, I d never think it's correlated. About other things like annotationProcessor and generateStubs - first I tried without it but it's gotten to the point of desperate trying out old solutions lol
👍 1
c
Dagger comes with a steep learning curve and it is easy to hate (I did for a long time) before you learn it. Their hopelessly bad way of explaining it surely doesn't help (or we wouldn't have 1000+ articles titled "Dagger, the simple way...". But hang in there, when it finally drops, you'll realise that it is an extremely powerful and helpful tool
👍 1
144 Views