I have a gradle project and I want to use dagger 2.0 in it. I don't know how to configure IntelliJ and gradle to generate files and let IntelliJ find them?
My build.gradle file looks like:
apply plugin: 'java'
apply plugin: 'idea'
version = '1.0'
repositories {
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.12'
compile 'org.slf4j:slf4j-simple:1.7.12'
compile 'commons-configuration:commons-configuration:1.10'
compile 'commons-collections:commons-collections:3.2.1'
compile 'com.google.dagger:dagger:2.0'
compile 'com.google.dagger:dagger-compiler:2.0:jar-with-dependencies'
compile 'com.pi4j:pi4j-distribution:1.1-SNAPSHOT'
}
In the build directory of my application the file DaggerXmlConfigurationComponent
exists, which is a component Dagger creates. But I can't use it in IntelliJ because it can't find the class.
This is not an Android application but an application for the Raspberry Pi.
I had problems with the existings plugins, so I added the following to my
build.gradle
:I finished up with the following solution (and it seems to be the simplest one from all the sent answers):
In my case the problem was IDEA creating a separate module for the dagger generated files. I had to go to
File -> Project Structure -> Modules
and remove theprojectname_dagger
module (by clicking the red minus), then add the generated source folder to myprojectname_main
module by clickingAdd Content Root
and selecting it.The for some reason I had to delete Dagger's files and let IDEA regenerate them because I was getting errors about duplicate files in the project.
Now it works, event with Annotation Processors being turned off (I suspect they must mainly be important for Android projects).
I've found a solution.
https://github.com/tbroyer/gradle-apt-plugin
Additionally if you are using Intellij a following configuration is recommended:
When using the Gradle integration in IntelliJ IDEA however, rather than the idea task, you'll have to manually enable annotation processing: in Settings… → Build, Execution, Deployment → Compiler → Annotation Processors, check Enable annotation processing and Obtain processors from project classpath. To mimic the Gradle behavior and generated files behavior, you can configure the production and test sources directories to build/generated/source/apt/main and build/generated/source/apt/test respectively and choose to Store generated sources relative to: Module content root. I've also had to remove Exclude from whole build directory and mark generated/source/apt/main directory as source.
The easiest way, I know of is to use the apt-idea plugin
Just activate the plugin in the
build.gradle
file:and then add the annotation processors to the
annotationProcessor
configuration:I've created a very simple test-project on GitHub: ex.dagger
(using IntelliJ 2018.1.4, Gradle 4.7)
Since net.ltgt.apt version
0.11
(Feb 2018) you can just apply the pluginnet.ltgt.apt-idea
tobuild.gradle
: