6

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 recognised 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 recognise 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 recognise. What am I missing?

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

Michael Vescovo
  • 3,741
  • 4
  • 32
  • 45
  • 1
    One issue might be that IntelliJ has not synchronized with the libraries you are using. You should see a Gradle refresh/sync symbol somewhere. Clicking that should help you. Also, it might be IntelliJ's issue and not Java's. You might want to rephrase your question title. – Sid May 07 '16 at 05:33
  • Thanks for the tip. I updated the subject. As far as the Gradle sync is concerned I've done that many times. I'm stumped. I've looked at this too long. I need another set of eyes from someone knows done it before. I would have thought it would be super simple for such a person to answer. Hopefully I've explained my self well enough. – Michael Vescovo May 07 '16 at 05:48
  • See: http://stackoverflow.com/a/41815478/1010868 – Tomasz Gawel Jan 23 '17 at 21:00

1 Answers1

5

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

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • The "Enable annotation processing" has made the coffeemachine sample app work. As in it runs properly. But for some reason it still marks the "DaggerCoffeeApp_Coffee" in red saying it cannot resolve the symbol. I downloaded the sample app and am trying to get it to run using Gradle before bothering with my other app. It's an improvement with your suggestion! I would mark it resolved but technically it's not. The buildscript thing didn't do anything and it said it couldn't find the path anyway for the maven url. – Michael Vescovo May 07 '16 at 08:08
  • Hrmm... so you're saying the project itself runs with enabled annotation processing, but Intellij still can't resolve the path? That would mean you have to add the generated folder as a source folder. – EpicPandaForce May 07 '16 at 08:51
  • Ticking enable annotation processing without doing anything else made the app work. Then later adding the other stuff in the gradle file didn't do anything (except give that additional message about the url). I later changed the paths in the annotations section and that message doesn't come up any more but I still get the red text. So it's got to be something simple. Maybe something to do with this apt thing. I'm doing what is says here but it's still complaining: https://github.com/tbroyer/gradle-apt-plugin – Michael Vescovo May 07 '16 at 09:07
  • FYI, like I said I'm just trying to get the sample coffee app to work so anyone can try to reproduce the problem fairly easily (assuming all relevant software is installed). If someone gets it to work then just post the relevant parts and I'll see what's different with mine. – Michael Vescovo May 07 '16 at 09:10
  • did you add the generated source as a source file as i said – EpicPandaForce May 07 '16 at 10:22
  • Yes I did that. I think maybe it's a problem with that link https://discuss.gradle.org/t/problems-with-https-plugins-gradle-org-m2/13554 – Michael Vescovo May 07 '16 at 12:18
  • I would up vote your answer but I can't because I don't have enough reputation. I can't really mark it as accepted because it's still red and I'm only speculating that the problem is with the link. Someone else may have the answer. – Michael Vescovo May 07 '16 at 12:20
  • Ok I haven't quite got rid of the red yet, but I feel like I know what the issue is. I'll give you the accepted answer because you set me on the right path. So dagger is generating the class it's looking for as I can see it in the folder, and I've set the sources to point to that folder, but for some reason it's not seeing it. I'll have to work it out. The program works and I know what the issue is. Thanks for you help! – Michael Vescovo May 07 '16 at 13:38
  • after I'm assuming you up voted my question I got 5 points and then another 2 for accepting the answer. So now I can up vote things! Awesome. Been wanting this for a long time. So I've immediately tested it on your answer and it worked. Thanks again. – Michael Vescovo May 07 '16 at 13:41