Closed alex-y-su closed 7 years ago
@alexeysuvorov Thank you for reporting this issue. Unfortunately, injection into library project isn't supported at the moment, which is definitely not good. I'm going to support Android library projects in the nearest future.
I am not sure this problem is in library. If all dependencies are defined inside library - everything just fine, but when I want a dependency to be defined out of library - on level of application which host the library - it stops to compile. The more I think about this problem the more I understand that it is quite hard to resolve this on compile time. However I still think it is quite often case and probably it is good to have option to "Promise" some dependencies inside a library module and then provide it by application injector or etc.
I've tried to imagine how could it be:
//------------------------------------------------------------------------------------
//In android library
//------------------------------------------------------------------------------------
@Module
@ProxyModule
class LibraryModule(val hostInjector: Injector) {
@Provides
fun proxyComponent() : IComponent = hostInjector.getInstance<IComponent>()
}
@Component
class LibraryComponent() {
@Provides
@Proxy
fun getLibraryModule(hostInjector: Injector) : LibraryModule(hostInjector)
}
class SomeFragmentInLibrary : Fragment {
override fun onCreate() {
//get
val appInjector = ... //somehow get app injector
val libInjector = Lightsaber.get().createChildInjector(appInjector, LibraryComponent())
//resolve everything we need in lib
}
}
//------------------------------------------------------------------------------------
//In anroid app
//------------------------------------------------------------------------------------
@Module
class ApplicationModule() {
@Provides
fun component() : IComponent = ComponentImpl()
}
@Component
class ApplicationComponent {
@Provides
fun appModule() = ApplicationModule()
}
//We should pass this instance to library
val appInjector = Lightsaber.get().createInjector(ApplicationComponent())
Actually you don't need to pass an Injector
to LibraryModule
. Instead you can just use the app injector and inject dependencies into SomeFragmentInLibrary
. I think I've already fixed this issue so an updated version is going to be published soon.
@alexeysuvorov Now everything should work fine.
Works great in 0.8.3, thank you.
Hello, my project contains several libraries with business components inside and application which contains UI and use business components from libraries. I want to use logger as a dependency inside business components, but actual logger implementation is known only within application itself, so I have
ILogger
interface andMyLibraryComponentImpl @Inject constructor(logger: ILogger)
. When I try to compile my app I see:It looks like Android library cannot have dependencies from app which host it.