Mark
08/16/2021, 4:02 AM// common module
interface Brand {
val foo: String // plus lots more properties and funs
// implement Brand so usage like Brand.foo instead of Brand.INSTANCE.foo
companion object : Brand {
// this must be set very early in Application.onCreate()
// why not use DI? Because sometimes we need access to Brand before DI has finished setting up
lateinit var INSTANCE: Brand
override val foo: String
get() = INSTANCE.foo
}
}
// brand module (dependency on common module)
// different brand modules for different product flavors
object ActualBrand : Brand {
override val foo: String
get() = "actual brand"
}
fun Brand.Companion.init() {
INSTANCE = ActualBrand
}
// app module (dependencies on brand and common modules)
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
Brand.init() // note: we do not need to hardcode ActualBrand
// proceed to DI initialisation
}
}
Tomasz Krakowiak
08/16/2021, 4:07 AMMark
08/16/2021, 4:08 AMApplication
not always availableApplication.onCreate()
is being called too late. I’m using Android AppInitializers which are triggered earlier, so next step is to investigate: https://proandroiddev.com/app-startup-under-the-hood-with-koin-library-example-380e188568ad
But still, I’m interested in general thoughts about implementing an interface with the interface’s companion object.Tomasz Krakowiak
08/16/2021, 4:47 AMMark
08/16/2021, 4:48 AMColton Idle
08/16/2021, 1:53 PMMark
08/16/2021, 1:55 PMApplication.onCreate()
occurs after ContentProvider
initialisationColton Idle
08/16/2021, 1:57 PM