问题
I have created a demo Android Lib project and used dagger 2.0 with the following steps:
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
Project -> Properties -> Java Compiler -> Annotation Processing (Enabled annotation processing)
Project -> Properties -> Java Compiler -> Annotation Processing - Factory path: Added all the above mentioned jars.
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)
orEngine 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