Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2 does not generate component

I totally exhausted with Dagger 2 in non-Android app (IDE is IntelliJ IDEA).

build.gradle

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "net.ltgt.gradle:gradle-apt-plugin:0.9"
    }
}

apply plugin: 'java'
apply plugin: 'net.ltgt.apt'

dependencies {
    compile 'com.google.dagger:dagger:2.8'
    apt 'com.google.dagger:dagger-compiler:2.8'
    compileOnly 'javax.annotation:jsr250-api:1.0'
}

Component

@Component(modules = {MyModule.class}) public interface MyComponent {}

Module

@Module class MyModule {}

Main

DaggerMyComponent component = DaggerMyComponent.builder().build();

Compiler says

cannot find symbol DaggerMyComponent

Absolutely have no more ideas what could be wrong :(

By the way, if I run gradlew clean build --refresh-dependencies, there is no error, everything fine!

But as only I use 'Rebuild project' in IDE - error again. Possibly IDEA bug (time to go back to Eclipse)?

And yes, I have added directory with generated classes as a content root, so IDE can see DaggerMyComponent.

like image 597
Alexey Avatar asked Jan 25 '17 03:01

Alexey


People also ask

What is Dagger 2 Component?

Define components. Components define from which modules (or other components) dependencies are provided. Dagger 2 uses this interface to generate the accessor class which provides the methods defined in the interface.

How Dagger 2 works?

To instantiate the dependency, Dagger will go through a class using the @Module annotation. Inside it, it will be instantiated using a method using the @Provides annotation. The @Singleton annotation ensures that the dependency is instantiated only once.

Why use Dagger 2 Android?

Dagger 2 walks through the dependency graph and generates code that is both easy to understand and trace, while also saving you from writing the large amount of boilerplate code you would normally need to write by hand to obtain references and pass them to other objects as dependencies.


1 Answers

I had post the issue to JetBrains

https://youtrack.jetbrains.com/issue/IDEA-169387

and the solution is in first comment.

If you use IntelliJ internal compiler for the project build you need to enable annotation processing at File | Settings | Build, Execution, Deployment | Compiler | Annotation Processors

As an alternative you can delegate build/run IDE actions to gradle. You can find the option at File | Settings | Build, Execution, Deployment | Build Tools | Gradle | Runner

In build 171.4073.35 Delegate IDE build/run actions to gradle option is set by default.

like image 79
Alexey Avatar answered Sep 22 '22 22:09

Alexey