Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Dagger2 with Lombok

Has anyone used Lombok 1.16 with Dagger2?

My current code looks like:

@AllArgsConstructor(onConstructor = @__(@Inject))
public class JuiceMaker {
    private final Apple apple;

The error is:

JuiceMaker cannot be provided without an @Inject constructor or from an @Provides-annotated method.

Without the Lombok annotation this actually works, so:

public class JuiceMaker {
    private final Apple apple;
    @Inject
    public JuiceMaker(Apple apple){
        this.apple = apple
    }
}

works

like image 555
Jessica Avatar asked May 16 '18 02:05

Jessica


People also ask

What is dagger2?

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.

What does @singleton annotation do?

With CDI (Contexts and Dependency Injection), we can easily create singletons using the @Singleton annotation. This annotation is a part of the javax. inject package. It instructs the container to instantiate the singleton once and passes its reference to other objects during the injection.

What is dagger 2 used for?

Dagger is arguably the most used Dependency Injection, or DI, framework for Android. Many Android projects use Dagger to simplify building and providing dependencies across the app. It gives you the ability to create specific scopes, modules, and components, where each forms a piece of a puzzle: The dependency graph.

How does dagger generate code?

Dagger generates code similar to what you would have written manually. Internally, Dagger creates a graph of objects that it can reference to find the way to provide an instance of a class. For every class in the graph, Dagger generates a factory-type class that it uses internally to get instances of that type.


1 Answers

This is a copy paste version of my answer here:

This is not a direct answer to the question, which seems to be solved, but acts as reference for future searchers:

If you're using Dagger (or something else) to process your annotations like

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.7.0</version>
      <configuration>
        <annotationProcessorPaths>
          <path>
            <groupId>com.google.dagger</groupId>
            <artifactId>dagger-compiler</artifactId>
            <version>2.15</version>
          </path>
        </annotationProcessorPaths>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>
    ....
  </plugins>
</build>

You have to add here lombok as path like

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.7.0</version>
      <configuration>
        <annotationProcessorPaths>
          <path>
            <groupId>com.google.dagger</groupId>
            <artifactId>dagger-compiler</artifactId>
            <version>2.15</version>
          </path>

          <!-- SOLUTION --> 
          <path>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
          </path>


        </annotationProcessorPaths>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>
    ....
  </plugins>
</build>

But you still have to list lombok as provided dependency ;)

like image 95
Ore Avatar answered Sep 21 '22 06:09

Ore