MichaelRocks / lightsaber

Compile time dependency injection framework for JVM languages. Especially for Kotlin.
Apache License 2.0
118 stars 8 forks source link

Compile error when consumer is in library but dependency is in app #2

Closed alex-y-su closed 7 years ago

alex-y-su commented 7 years ago

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 and MyLibraryComponentImpl @Inject constructor(logger: ILogger) . When I try to compile my app I see:

Lightsaber failed
java.lang.IllegalStateException: Key for Dependency(type=xxx.project.ILogger, qualifier=null) not found
    at io.michaelrocks.lightsaber.processor.generation.GeneratorAdapterExtensionsKt.getKey(GeneratorAdapterExtensions.kt:124)
    at io.michaelrocks.lightsaber.processor.generation.GeneratorAdapterExtensionsKt.getInstance(GeneratorAdapterExtensions.kt:90)
    at io.michaelrocks.lightsaber.processor.generation.GeneratorAdapterExtensionsKt.getDependency(GeneratorAdapterExtensions.kt:65)
    at io.michaelrocks.lightsaber.processor.generation.ProviderClassGenerator.generateProviderMethodArgument(ProviderClassGenerator.kt:193)
    at io.michaelrocks.lightsaber.processor.generation.ProviderClassGenerator.generateProvideMethodArguments(ProviderClassGenerator.kt:187)
    at io.michaelrocks.lightsaber.processor.generation.ProviderClassGenerator.generateConstructorInvocation(ProviderClassGenerator.kt:160)
    at io.michaelrocks.lightsaber.processor.generation.ProviderClassGenerator.generateGetWithInjectorMethod(ProviderClassGenerator.kt:138)
    at io.michaelrocks.lightsaber.processor.generation.ProviderClassGenerator.generate(ProviderClassGenerator.kt:88)
    at io.michaelrocks.lightsaber.processor.generation.ProvidersGenerator.generate(ProvidersGenerator.kt:37)
    at io.michaelrocks.lightsaber.processor.generation.Generator.generateProviders(Generator.kt:168)
    at io.michaelrocks.lightsaber.processor.generation.Generator.generate(Generator.kt:59)
    at io.michaelrocks.lightsaber.processor.ClassProcessor.performGeneration(ClassProcessor.kt:102)
    at io.michaelrocks.lightsaber.processor.ClassProcessor.processClasses(ClassProcessor.kt:67)
    at io.michaelrocks.lightsaber.processor.LightsaberProcessor.process(LightsaberProcessor.kt:32)
    at io.michaelrocks.lightsaber.plugin.LightsaberTransform.transform(LightsaberTransform.kt:78)

It looks like Android library cannot have dependencies from app which host it.

MichaelRocks commented 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.

alex-y-su commented 7 years ago

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())
MichaelRocks commented 7 years ago

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.

MichaelRocks commented 7 years ago

@alexeysuvorov Now everything should work fine.

alex-y-su commented 7 years ago

Works great in 0.8.3, thank you.