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 ?
Dagger is a fully static, compile-time dependency injection framework for Java, Kotlin, and Android.
Dagger 2 is a compile-time android dependency injection framework that uses Java Specification Request 330 and Annotations. Some of the basic annotations that are used in dagger 2 are: @Module This annotation is used over the class which is used to construct objects and provide the dependencies.
Dagger automatically generates code that mimics the code you would otherwise have hand-written. Because the code is generated at compile time, it's traceable and more performant than other reflection-based solutions such as Guice. Note: Use Hilt for dependency injection on Android.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With