Hey everyone! This isn't directly related to Kotli...
# feed
u
Hey everyone! This isn't directly related to Kotlin, but I thought it might help those of us working with KMP. I often struggle when implementing Android-specific functions that need the application context. There are many methods online, but most add too much boilerplate. To solve this problem, I wrote a library called Android Context Provider, which makes it so much easier. It initializes automatically via
ContentProvider
, and you can call
ContextProvider.getContext()
to get the application context (not an activity context) from anywhere. No extra code, lightweight, and dependency-free. If you use KMP and need Android-specific context functions, check it out! https://github.com/kdroidFilter/AndroidContextProvider
👍 5
h
Or you can use AndroidX start-up
Copy code
internal lateinit var applicationContext: Context
    private set
internal data object ContextProviderInitializer
Copy code
class ContextProvider: Initializer<ContextProviderInitializer> {
    override fun create(context: Context): ContextProviderInitializer {
        applicationContext = context.applicationContext
        return ContextProviderInitializer
    }
Copy code
override fun dependencies(): List<Class<out Initializer<*>>> = emptyList()
}
👀 1
u
Thanks! I hadn't thought about using AndroidX Startup for this. It's definitely a clean way to handle initialization. I'll give it a closer look!
a
Dependency-free? Isn’t your library itself becomes dependency to others?
1
u
👍 1