Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2 Component not generating Dagger Prefixed Classes for building

I am new to Dagger 2 and am trying out the Dagger 2 Coffee Example in IntelliJ and it seems that is does not generate a DaggerCoffeeApp_Coffee (It should be generating it) even though I followed the code example for Dagger 2 in github closely.

Public class CoffeeApp {
    @Singleton
    @Component(modules = {DripCoffeeModule.class})
    public interface Coffee {

        CoffeeMaker maker();
    }
    public static void main(String args[]){


        Coffee coffee = DaggerCoffeeApp_Coffee.builder().build();
    }
}

Here is my pom.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tim.test</groupId>
    <artifactId>Dagger2Experiment</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <slf4j-api.version>1.7.12</slf4j-api.version>
    </properties>
    <repositories>
        <repository>
            <id>sonatype</id>
            <name>sonatype-repo</name>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
        </repository>
    </repositories>
    <dependencies>

        <dependency>
            <groupId>com.google.dagger</groupId>
            <artifactId>dagger</artifactId>
            <version>2.0.1</version>

        </dependency>
        <dependency>
            <groupId>com.google.dagger</groupId>
            <artifactId>dagger-compiler</artifactId>
            <version>2.0.1</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j-api.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j-api.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

</project>

I have tried various solution from the topic below but nothing seems to work:

I have also added jar file to my application buildpath dagger-2.0.1.jar in your application's runtime and dagger-compiler-2.0.1.jar in your build at compile time.

Update I used DaggerCoffeeApp_Coffee.builder().build() in the code snippet above since I edited my code to follow the code example in Dagger 2's github below after I couldn't find the constructor. Link below:

https://github.com/google/dagger/blob/master/examples/simple/src/main/java/coffee/CoffeeApp.java

Any help will be greatly appreciated.

like image 400
t22000 Avatar asked Oct 31 '22 17:10

t22000


1 Answers

Update Yep, You caught me skimming the question. I failed to read your code example otherwise I would have seen the pom.xml wasn't the only issue.

  1. I'm assuming your DripCoffeeModule is annotated correctly and has no parameters in its constructor so that you don't need to specify this in building the Component. E.g. :

    @Module
    
    public DripCoffeeModule {
    
    //Uses default constructor
    
    }
    
  2. I've not seen the Component implemented as an inner class before but I'm betting that Dagger won't treat this any different in terms of instantiating. (I would recommend moving it out of CoffeeApp class.) The naming, however, would be incorrect. Instead of

    DaggerCoffeeApp_Coffee.builder().build();
    

You have to follow the naming convention defined by Dagger. From the section titled Building the Graph on Dagger's website:

The implementation has the same name as the interface prefixed with Dagger.

So you need to modify the line to:

DaggerCoffee.builder().build();

or you can use the convenience method:

DaggerCoffee.create();

If I'm wrong about number 1, then you will need to construct your Module as well like so:

DaggerCoffee.builder().dripCoffeeModule(new DripCoffeeModule()).build();

Original

Move the compiler from the dependencies section into the compiler section. From the Dagger 2 official site.

In a Maven project, one would include the runtime in the dependencies section of your pom.xml, and the dagger-compiler artifact as a dependency of the compiler plugin:

For example:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>com.google.dagger</groupId>
                    <artifactId>dagger-compiler</artifactId>
                    <version>2.0</version>
                    <optional>true</optional>
                </dependency>
            </dependencies>
        </plugin>
like image 176
fakataha Avatar answered Nov 09 '22 07:11

fakataha