-->

How to configure dagger in IntelliJ

2019-06-23 23:18发布

问题:

I downloaded dagger as it seems to be a good option for dependency injection in Android. But I have some troubles integrating the compiler. Are there any hints regarding the right settings? I followed the instructions regarding android annotations as this project seems to be similar. But it doesn't work for dagger.

What I did: I added dagger-1.1.0.jar and javax.inject-1.jar to my project's lib folder and I added a new folder called "compile-libs" containing the "dagger-compiler-1.1.0.jar" as I do not want this file to be packed in *.apk.

I added dagger and javax.inject to the module's dependencies.

Now the tricky part: I added the compiler to annotation compiler settings:

When I run the program I get the following error: Module adapter for class de.myproject.app.modules.AndroidModule could not be loaded. Please ensure that code generation was run for this module.

So it seems to me the code generation did not take place. Addionally the "generated" folder in my project is empty. So what are the correct settings to get dagger to work in Android project in IntelliJ?

回答1:

The dagger-compiler dependencies are not being found.

Dagger, JavaWriter and javax.inject are also required.
(For 1.2+ Guava is also required - guava discussion)

Try putting those .jars in the same folder as dagger-compiler and then adding all 4 also as annotation processors. This works for Eclipse and should for IntelliJ too.

Alternatively make 1 "fat" jar with all those dependencies together. See issue 360 for details.

Files may be downloaded from Maven:

  • Dagger
  • JavaWriter
  • javax.inject
  • Guava