dagger icon indicating copy to clipboard operation
dagger copied to clipboard

Code generation for annotations in Intellij

Open kurokoda opened this issue 12 years ago • 7 comments

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?

kurokoda avatar Nov 05 '13 01:11 kurokoda

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?

marcoRS avatar Nov 05 '13 03:11 marcoRS

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

JakeWharton avatar Nov 05 '13 03:11 JakeWharton

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

kurokoda avatar Nov 05 '13 04:11 kurokoda

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.

marcoRS avatar Nov 05 '13 05:11 marcoRS

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

kurokoda avatar Nov 05 '13 05:11 kurokoda

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']
    }

xiezefan avatar Apr 27 '15 08:04 xiezefan

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)

tbroyer avatar May 13 '15 18:05 tbroyer