square / dagger

A fast dependency injector for Android and Java.
https://square.github.io/dagger/
Apache License 2.0
7.31k stars 3.06k forks source link

Code generation for annotations in Intellij #331

Open kurokoda opened 10 years ago

kurokoda commented 10 years ago

When attemptiong to run a test project in IntelliJ, I'm getting this error:

"... Please ensure that code generation was run for this module."

Code annotation is enabled.

Any suggestions as to running annotation processors for Dagger in IntelliJ?

marcoRS commented 10 years ago

I am also seeing this error in Android Studio as well as when running "mvn clean install". I tried upgrading to 1.1.0 and this error started appearing. I also am seeing errors in the generated files about modules that are in my test project. Do the packages in the test project have to be the same as in the main project?

JakeWharton commented 10 years ago

When building from an IDE you need to enable annotation processing. Instructions for IntelliJ can be found here for a similar library.

kurokoda commented 10 years ago

Sorry, to clarify, code annotation IS enabled. I've tried it both as a compiled and provided dependency. I'm getting this stack trace:

11-04 23:33:18.037: ERROR/AndroidRuntime(916): FATAL EXCEPTION: main java.lang.RuntimeException: Unable to create application com.indigocode.dagger.DemoApplication: java.lang.IllegalStateException: Module adapter for class com.indigocode.dagger.AndroidModule could not be loaded. Please ensure that code generation was run for this module. at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4447) at android.app.ActivityThread.access$1300(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5103) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.IllegalStateException: Module adapter for class com.indigocode.dagger.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.indigocode.dagger.DemoApplication.onCreate(DemoApplication.java:32) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4444) ... 10 more

And here are my screenshots of the relevant settings.

screen shot 2013-11-04 at 8 37 09 pm screen shot 2013-11-04 at 8 37 41 pm

marcoRS commented 10 years ago

It might be possible that your project set up is not including the generated code as a source. You can check to see if it's included under Project Settings --> Module --> Sources.

kurokoda commented 10 years ago

The code itself is not being generated, as far as I can tell. It's set to be deployed to the the project's generated code folder 'gen', but no dagger-generated files are appearing in that folder. You can see that that folder is linked in this screenshot: screen shot 2013-11-04 at 9 27 20 pm

xiezefan commented 9 years ago

dagger will generate code to the folder app/gen so add

java.srcDirs = 'gen'

to you app/build.gradle like this

sourceSets.main {
        java.srcDirs = ['src/main/java', 'gen']
    }
tbroyer commented 9 years ago

FYI, I published a Gradle plugin that automatically sets up IntelliJ IDEA and/or Eclipse (in addition to adding new configurations and automatically configuring JavaCompile tasks): https://github.com/tbroyer/gradle-apt-plugin

For IntelliJ IDEA, when using IntelliJ's built-in Gradle integration (rather than ./gradlew idea), there are a couple of manual steps to enable annotation processing in the IDE, but that's optional (for example if you regularly run ./gradlew assemble or ./gradlew compileJava)