javaintellij-ideadagger-2

Java/IntelliJ not recognising the "Dagger" prefixed classes for Dagger2 Components


I'm probably missing something very basic here, so hopefully it's not a hard question.

My equivalent of

CoffeeShop coffeeShop = DaggerCoffeeShop.create();

is not being recognized by the IDE (IntelliJ). This is a native Java project. The "DaggerCoffeShop" part is all red.

The component is using this syntax:

@Component(modules = <ModuleClassName>.class)
public interface CoffeeShop {
    // some methods.
    // does it matter what goes in here for it to recognize the component?
}

The "ModuleClassName" is the name of a module annotated with @Module and includes @Provides methods.

To get the libs I'm using this in Gradle:

compile 'com.google.dagger:dagger:2.4'
compile 'com.google.dagger:dagger-compiler:2.4'

Is that all I need? I don't get any errors for any of the annotations, it's just this Dagger keyword it can't recognize. What am I missing?

Any help or direction appreciated. I'm not finding the documentation to be that great for beginners like me.


Solution

  • Your annotation processing is probably not enabled.

    In Settings - > Compiler -> Annotation Processor -> Enable annotation processing

    Afterwards, Rebuild Project

    If it still thinks the files don't exist (despite the app running properly), you'll have to add the generated source folder as a source folder: File -> Project Structure -> Modules -> select your project -> Sources -> right click on the generated folder with the Dagger stuff in it -> Sources


    And compile 'com.google.dagger:dagger-compiler:2.4' should be either apt scoped or annotationProcessor scoped. If you use Kotlin, then kapt scoped