How to create an android library with kodein? 1 -...
# kodein
g
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
Can you be more specific ?
g
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)
    }
}
This is working for now, but is this right?
s
Yep ;)
👍 1