Dagger 2: No implementation generated for component interface

北慕城南 提交于 2019-12-11 13:51:51

问题


I have created a demo Android Lib project and used dagger 2.0 with the following steps:

  1. Added the following jars to /libs folder:

    dagger-2.0.jar

    dagger-compiler-2.0.jar

    dagger-producers-2.0-beta.jar

    guava-18.0.jar

    javawriter-2.5.1.jar

    javax.annotation-api-1.2.jar

    javax.inject-1.jar

  2. Project -> Properties -> Java Compiler -> Annotation Processing (Enabled annotation processing)

  3. Project -> Properties -> Java Compiler -> Annotation Processing - Factory path: Added all the above mentioned jars.

  4. Created the following classes:

    public class Car {
    
        private Engine engine;
    
        @Inject
        public Car(Engine engine) {
            this.engine = engine;
        }
    
        public String carDetails(){
            String engineName = this.engine.getName();
            int engineNumber = this.engine.getNumber();
    
            return "This car has the following details: \n" + engineName + "----" + engineNumber;
        }
    }
    

    public interface Engine {

            public String getName();
    
            public int getNumber();
    
        }
    
    public class Toyota implements Engine{
    
        @Override
        public String getName() {
            return "This is toyota engine";
        }
    
        @Override
        public int getNumber() {
            return 1234567890;
        }
    
    
    
    }
    
    
    @Component(modules = EngineModule.class)
    public interface EngineComponent {
        void inject();
    }
    
    @Module
    public class EngineModule {
    
        public EngineModule(DemoApplication demoApplication) {
    
        }
    
        @Provides
        Engine provideEngine(){
            return new Toyota();
        }
    }
    

But inside /.apt-generated folder there are only two files:

Car_Factory.java            EngineModule_ProvideEngineFactory.java

DaggerEngineComponent.java is not there for me to build the component.

Could someone please help?


回答1:


I'm guessing the annotation processor is encountering an error and Eclipse is not showing you the log. If you have log output in the Output view, you may want to paste that into the question.

Specifically, I think it's erroring out on void inject(), which isn't a format descibed in the @Component docs. Those docs describe three types of methods:

  • Parameterless factory methods that return an injectable type Dagger creates and injects, like Engine createEngine(), or
  • Single-parameter void methods that receive an instance created elsewhere and apply method and field injection, like void injectEngine(Engine) or Engine injectEngine(Engine).
  • Subcomponent-returning methods that combine your Component's bindings with those from another module.

Because your void inject() doesn't match any of those formats, Dagger is likely erroring out and refusing to create a DaggerEngineComponent.



来源:https://stackoverflow.com/questions/32906199/dagger-2-no-implementation-generated-for-component-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!