java - How to inject implementations from another module -
i'm having project based on dagger 2 consists of 2 modules. core module includes interfaces , classes have member injections declared these interfaces.
the actual implementations of these interfaces included in second module android project. so, naturally provide methods these included in android project.
dagger complain during compilation not knowing how inject these in core module.
any thoughts on how achieve without using constructor injections?
in short, tried this, , works. sure check exact error messages , make sure providing these interfaces , @inject
annotations present.
there wrong named interface or missing annotation. following full sample using described architecture compiling fine. issue experiencing 1 described in last part of post. if possible, should go first solution though , add annotations.
the library
for reproducability sample has minimalist models. first, interface needed class in library module:
public interface myinterface { }
here class needs interface. make sure declare in constructor , provide @inject
annotation!
@myscope // sure add scopes in class if use constructor injection! public class myclassusingmyinterface { private myinterface mmyinterface; @inject public myclassusingmyinterface(myinterface myinterface) { mmyinterface = myinterface; } }
the idea interface implemented app using myclassusingmyinterface
, provided dagger. code nicely decoupled, , awesome library not many features complete.
the application
here need supply actual coupling. means myclassusingmyinterface
have make sure can supply myinterface
. let's start module supplying that:
@module public class mymodule { @provides myinterface providesmyinterface() { return new myinterface() { // super awesome implementation. mit license applies. }; } }
and use this, provide component can inject mytestinjectedclass
going need myclassusingmyinterface
.
@component(modules = mymodule.class) public interface mycomponent { void inject(mytestinjectedclass testclass); }
now have way provide requested interface. declared interface needed library class in constructor marked @inject
. want class requires awesome library class use. , want inject dagger.
public class mytestinjectedclass { @inject myclassusingmyinterface mmyclassusingmyinterface; void onstart() { daggermycomponent.create().inject(this); } }
now hit compile...and dagger create factories needed.
inject libraries can not modify
to provide full scale of dagger, sample have been without actual access source code of library. if there no @inject
annotation dagger have hard time creating object. notice missing annotation:
public class myclassusingmyinterface { private myinterface mmyinterface; public myclassusingmyinterface(myinterface myinterface) { mmyinterface = myinterface; } }
in case have manually provide class. module needed modified following:
@module public class mymodule { @provides myinterface providesmyinterface() { return new myinterface() { }; } @provides myclassusingmyinterface providesmyclass(myinterface myinterface) { return new myclassusingmyinterface(myinterface); } }
this introduces more code write, make classes available can not modify.
Comments
Post a Comment