Hello ! Is there a way to use "by inject()" inside...
# koin
c
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 :
Copy code
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)
Copy code
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 :
Copy code
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 !
p
why do you need such a situation ?
Why do you want the Logger to be a Json deserializable ?
c
Hi, the sample I provide here (the all "log" thing) is not my real usecase, it's just to illustrate my issue and to reproduce it easily. The summary is : I have to create an instance of a class by deserializing a json retrieved from the backend and I want to use "by inject()" inside this class.
p
but why to you want to use by inject in that class?
c
Because the implementation of the interface I want to inject come from another module.
e
you could use this helper function:
Copy code
inline fun <reified T> getKoinInstance() =
    object : KoinComponent {
        val value: T by inject()
    }.value
and then retrieve the logger like this:
Copy code
val internalLogger = getKoinInstances<LogInterface>()
c
It also throw a NPE. I manage to get it work by getting the instance inside the log() function :
Copy code
fun log() {
        val internalLogger: LogInterface = get()
        internalLogger.logTag(tag)
    }