I'm currently developing a simple KMM module, which needs Context
in order to perform some operations. I am aware of ways to achieve that with extending Application
class and making dependency injection. What am I trying to do right now - to make this module available out of the box with no need to modify manifest
or make manual injection on start up. I am just wondering is it a bad practice to make something like so:
@SuppressLint("StaticFieldLeak")
object SomeUtil {
private val context = Activity().applicationContext
}
Since applicationContext
return the Context
for the whole app and we're initializing it once will there be a leak? Or are there some other points not to make it?
Maybe there are some other possibilities to get app context from the module? I've seen some examples of retrieving it from threads, but as I understand this will be (or is already) deprecated.
UPD: This causes an error. Activity()
seems to be null
. So any ideas how to achieve that without DI and "MyApplication"?