Reputation: 108
I want use Dagger2 and MVP in a project Gradle but without Android, in native Java. But i can't build my project, the class DaggerAppComponent is never generated. This class is automatically generated by Dagger lib in compile time.
build.gradle :
plugins {
id "net.ltgt.apt" version "0.11"
}
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
compileJava {
options.annotationProcessorPath = configurations.apt
}
configurations {
apt
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile "com.google.dagger:dagger:2.11"
apt "com.google.dagger:dagger-compiler:2.11"
apt "com.google.dagger:dagger-producers:2.11"
compileOnly "com.google.auto.factory:auto-factory:1.0-beta3"
apt "com.google.auto.factory:auto-factory:1.0-beta3"
compileOnly "org.immutables:value:2.2.10:annotations"
apt "org.immutables:value:2.2.10"
provided 'javax.annotation:jsr250-api:1.0'
compile 'org.glassfish:javax.annotation:10.0-b28'
}
Main.java
public class Main {
private static AppComponent appComponent;
public static void main(String[] args) {
/**
* Launch the application.
*/
EventQueue.invokeLater(new Runnable() {
public void run() {
appComponent = initDagger();
try {
MainViewImpl mainView = new MainViewImpl();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public AppComponent getAppComponent() {
return appComponent;
}
public static AppComponent initDagger() {
return DaggerAppComponent.builder().appModule(new AppModule())
.build();
}
}
When i build my project i have this error :
Error:(38, 16) java: cannot find symbol
symbol: variable DaggerAppComponent
location: class main.Main
The class DaggerAppComponent is never build. Do you have an idea ?
Upvotes: 6
Views: 1402
Reputation: 750
If you want to implement Dagger2 in a java library for example. I have written a short demo where you'll see how to do it. Take a look at this:
How to use Dagger in Java library module in Android Studio?
Upvotes: 0
Reputation: 4805
You need to follow the IDE instructions for the Gradle Annotation Processing plugin https://github.com/tbroyer/gradle-apt-plugin
Annotations like Dagger2 and Lombok need an annotation processor. This can be accomplished by configuring the IDE, but it's best to have as much as possible handled by gradle.
That being said there are still things you have to do for Eclipse or IntelliJ. For IntelliJ, this is important part:
When using the Gradle integration in IntelliJ IDEA (rather than the ida task), it is recommended to delegate the IDE build actions to Gradle itself starting with IDEA 2016.3: https://www.jetbrains.com/idea/whatsnew/#v2016-3-gradle Otherwise, 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.
Configure this in Project Defaults at the Launcher so that you only have to do it once.
Note that starting with IntelliJ IDEA 2016.1, and unless you delegate build actions to Gradle, you'll have to uncheck Create separate module per source set when importing the project.
Upvotes: 1
Reputation: 7649
I would say that this code
compileJava {
options.annotationProcessorPath = configurations.apt
}
configurations {
apt
}
is not necessary.
You also have to activate the annotation processing in the IDE options. You can check if that is your case by running gradle compile
and check if gradle generates the sources
I had a similar issue a couple of years ago and I finally solved it. You can find the solution here
Upvotes: 0