Dagger 2:没有为组件接口生成实现 [英] Dagger 2: No implementation generated for component interface

查看:29
本文介绍了Dagger 2:没有为组件接口生成实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个演示 Android Lib 项目并通过以下步骤使用了 dagger 2.0:

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

  1. 将以下 jars 添加到/libs 文件夹:

  1. Added the following jars to /libs folder:

dagger-2.0.jar

dagger-2.0.jar

dagger-compiler-2.0.jar

dagger-compiler-2.0.jar

dagger-producers-2.0-beta.jar

dagger-producers-2.0-beta.jar

guava-18.0.jar

guava-18.0.jar

javawriter-2.5.1.jar

javawriter-2.5.1.jar

javax.annotation-api-1.2.jar

javax.annotation-api-1.2.jar

javax.inject-1.jar

javax.inject-1.jar

  • Project -> Properties -> Java Compiler -> Annotation Processing(启用注解处理)

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

    项目 -> 属性 -> Java 编译器 -> 注释处理 - 工厂路径:添加了上述所有 jar.

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

    创建了以下类:

    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 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();
        }
    }
    

  • 但在 /.apt-generated 文件夹中只有两个文件:

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

    Car_Factory.java            EngineModule_ProvideEngineFactory.java
    

    DaggerEngineComponent.java 不是我来构建组件的.

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

    有人可以帮忙吗?

    推荐答案

    我猜注释处理器遇到了错误,Eclipse 没有向您显示日志.如果输出"视图中有日志输出,则可能需要将其粘贴到问题中.

    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.

    具体来说,我认为它在 void inject() 上出错,这不是 @Component 文档.这些文档描述了三种类型的方法:

    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:

    • 返回 Dagger 创建和注入的可注入类型的无参数工厂方法,例如 Engine createEngine()
    • 接收在别处创建的实例并应用方法和字段注入的单参数 void 方法,例如 void injectEngine(Engine)Engine injectionEngine(Engine).立>
    • 将您的组件的绑定与来自另一个模块的绑定相结合的子组件返回方法.

    因为您的 void inject() 不匹配任何这些格式,Dagger 可能会出错并拒绝创建 DaggerEngineComponent.

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

    这篇关于Dagger 2:没有为组件接口生成实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆