15

I'm using Dagger for dependency injection in Android, using Eclipse to build. I've cloned android-activity-graphs to use as an example.

I've set up my environment according to staxgr from https://github.com/square/dagger/issues/126
These are my libs: dagger-1.1.0.jar, dagger-compiler-1.1.0.jar, and javax.inject.jar

And lastly, I've changed the source folders in Eclipse to point to src/main/java (instead of just src/) so that Eclipse detects the related files through the package keyword.

The project builds, but fails immediately when it's run with this exception:

Caused by: java.lang.IllegalStateException: 
    Module adapter for class 
    com.example.dagger.activitygraphs.AndroidModule could not be loaded. 
    Please ensure that code generation was run for this module.

at dagger.internal.FailoverLoader.getModuleAdapter(FailoverLoader.java:41)
at dagger.internal.Modules.getAllModuleAdapters(Modules.java:43)
at dagger.ObjectGraph$DaggerObjectGraph.makeGraph(ObjectGraph.java:167)
at dagger.ObjectGraph$DaggerObjectGraph.access$000(ObjectGraph.java:134)
at dagger.ObjectGraph.create(ObjectGraph.java:126)
at com.example.dagger.activitygraphs.DemoApplication.onCreate(DemoApplication.java:29)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1000)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4391)

How do I get Dagger examples to work through eclipse?

JJD
  • 50,076
  • 60
  • 203
  • 339
sudocoder
  • 1,164
  • 1
  • 13
  • 26

1 Answers1

23

The problem is I am missing the javawriter library. Also, you MUST use version 2.2.1 of javawriter and NOT the latest which is 2.3.0 (this will probably change, but remains true at the time of writing this on 11/27/2013). The link provided should take you to the correct place. Please double triple check this, for a total of six times. I'll also include a list of other reasons why you might see this message.

First, please ensure that all these checkboxes are checked.

Project -> Properties ->Java Compiler -> Enable Project Specific Settings

Project -> Properties ->Java Compiler -> Annotation Processing -> Enable Project Specific Settings

Project -> Properties ->Java Compiler -> Annotation Processing -> Factory path -> Enable Project Specific Settings.

Then on the same screen, click "Add jars" and add: javawriter-2.2.1.jar, dagger-1.1.0.jar, dagger-compiler-1.1.0.jar and javax.inject.jar in this list as well for a total of four jars.

Those four jars should be in a folder called 'libs' on your project's main path.

If you are using libraries and want to use Dagger for DI in those libraries, you will have to do the steps above on those projects as well.

sudocoder
  • 1,164
  • 1
  • 13
  • 26
  • Thanks for posting your solution. I noticed that Eclipse (Kepler) doesn't have Java Compiler -> Factory Path. Instead there's Java Compiler -> Annotation Processing -> Factory Path. Are you using Kepler? If so, did you need to make any changes in the Annotation Processing settings? I followed your instructions but still get the same exception you were getting. – Julian A. Nov 27 '13 at 20:27
  • Hey Julian, I've updated the description. Sorry, I forgot the Annotation Processing part. Can you check to make sure you have the three "enable project specific settings" checkboxes checked, and the four libraries added into a "libs" folder on your project's home directory. And that these libraries are also added in the list from the "Factory Path" screen? – sudocoder Nov 27 '13 at 21:02
  • Thanks for your update. Making the changes got rid of the exception. But now I get a different one. I've [created another SO post](http://stackoverflow.com/questions/20253373/dagger-setup-in-eclipse) for that. But here's the exception message: `"Internal compiler error: java.lang.NoSuchMethodError: com.squareup.javawriter.JavaWriter.beginType(Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/String;)Lcom/squareup/javawriter/JavaWriter; at dagger.internal.codegen.InjectAdapterProcessor.writeInjectAdapter(InjectAdapterProcessor.java:265)"` – Julian A. Nov 27 '13 at 21:36
  • 2
    One more thing: although following all these steps I did not succeed in the generating for quite some time, until I found that Project properties -> Java Compiler -> Compiler compliance level was 1.5. This should be at least 1.6 – Boris Strandjev Dec 29 '13 at 09:26
  • Hm... I don't have "Annotation Processing" anywhere, eclipse 4.2.1 – Eran Medan Oct 25 '14 at 01:42
  • Works also for IDEA, just search for "Annotation Processors" and check "Enable annotation processing" to true – Strassenrenner Jun 18 '15 at 09:53