6

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 ?

R.Tissier
  • 108
  • 7
  • Did you ``import`` the class? – f1sh Sep 06 '17 at 12:02
  • Shouldn't the dagger-compiler dependency scope be `compileOnly`? I am not even sure that `apt` type is proper for non-android projects. – yegodm Sep 06 '17 at 12:10
  • Which classes ? The problem it's i can't generate the class DaggeAppComponent. Thinks – R.Tissier Sep 06 '17 at 12:14
  • Possible duplicate of [Is there any way of making IntelliJ IDEA recognizing Dagger 2 generated classes in a Java project?](https://stackoverflow.com/questions/33444356/is-there-any-way-of-making-intellij-idea-recognizing-dagger-2-generated-classes) – David Rawson Sep 06 '17 at 21:24

3 Answers3

1

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.

Novaterata
  • 4,356
  • 3
  • 29
  • 51
0

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

Alberto S.
  • 7,409
  • 6
  • 27
  • 46
  • Enable annotation processing and Obtain processors from project classpath are already enable in my IDE. i have add apply `plugin: "net.ltgt.apt"` but no change. Thanks – R.Tissier Sep 06 '17 at 13:06
0

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?

Dharman
  • 30,962
  • 25
  • 85
  • 135