Hello ! Is there a way to use "by inject()" inside an instance of an object created by deserialization ?
Here is a dummy sample to reproduce my issue :
interface LogInterface {
fun logTag(tag: String)
}
class LogInterfaceImpl: LogInterface {
override fun logTag(tag: String) {
Log.d("LogInterface", "Tag : $tag")
}
}
val logModule = module {
single<LogInterface> { LogInterfaceImpl() }
}
class Logger(private val tag: String): KoinComponent{
private val internalLogger: LogInterface by inject()
fun log(){
internalLogger.logTag(tag)
}
}
data class LoggerTest(val logger : Logger)
val loggerTest = LoggerTest(
Logger("TEST_1")
)
loggerTest.logger.log() // display "TEST_1" in logs
val loggerTestJson = "{\"logger\":{\"tag\":\"TEST_2\"}}"
val loggerTest2: LoggerTest = Gson().fromJson(loggerTestJson, LoggerTest::class.java)
loggerTest2.logger.log() // throw a NPE :
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object kotlin.Lazy.getValue()' on a null object reference
at Logger.getInternalLogger(line of "by inject()")
at Logger.log(line of "internalLogger.logTag(tag)")
I'm not very used to Koin, should I use scope / something else that "single" / ... ?
Thanks !