How to create an android library with kodein?
1 - I want to pass the app context to the library builder and init kodein from there.
2- The application class in sample knows nothing about kodein.
How I can achieve this?
s
salomonbrys
08/02/2019, 6:58 PM
Can you be more specific ?
g
Giovani Guerra
08/06/2019, 4:19 PM
My library project doesn’t have a Application class.
I created a builder to pass the app context to my library like:
Copy code
class MyLibrarySdk private constructor(
context: Context
) : KodeinAware {
override val kodein: Kodein = Kodein.lazy {
// my bindings
}.on(context.applicationContext)
class Builder(private val context: Context) {
fun build(): MyLibrarySdk {
return MyLibrarySdk(context).also {
sdk = it
}
}
}
companion object {
lateinit var sdk: MyLibrarySdk
fun init(context: Context) {
context.startActivity(MyActivity.createIntent(context))
}
}
}
And a base activity to reference Kodein:
Copy code
class BaseActivity: AppCompatActivity(), KodeinAware {
override val kodein = Kodein.lazy {
extend(MyLibrarySdk.sdk.kodein)
}
}