Start Koin on Android
The koin-android
project is dedicated to provide Koin powers to Android world. See the Android setup section for more details.
From your Application class
From your Application
class you can use the startKoin
function and inject the Android context with androidContext
as follows:
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
// Log Koin into Android logger
androidLogger()
// Reference Android context
androidContext(this@MainApplication)
// Load modules
modules(myAppModules)
}
}
}
You can also start Koin from anywhere if you don't want to start it from your Application class.
If you need to start Koin from another Android class, you can use the startKoin
function and provide your Android Context
instance with just like:
startKoin {
//inject Android context
androidContext(/* your android context */)
// ...
}
Extra Configurations
From your Koin configuration (in startKoin { }
block code), you can also configure several parts of Koin.
Koin Logging for Android
Within your KoinApplication
instance, we have an extension androidLogger
which uses the AndroidLogger()
class.
This logger is an Android implementation of the Koin logger.
Up to you to change this logger if it doesn't suit to your needs.
startKoin {
// use Android logger - Level.INFO by default
androidLogger()
// ...
}
Loading Properties
You can use Koin properties in the assets/koin.properties
file, to store keys/values:
startKoin {
// ...
// use properties from assets/koin.properties
androidFileProperties()
}
Start Koin with Androidx Startup (4.0)
By using Gradle packge koin-androidx-startup
, we can use onKoinStartup
function durectly in init
block of your Application class:
class MainApplication : Application() {
init {
// Use AndroidX Startup for Koin
onKoinStartup {
androidContext(this@MainApplication)
modules(allModules)
}
}
override fun onCreate() {
super.onCreate()
}
}
This replaces the startKoin
function that is usally used in onCreate
.
Gain over from onKoinStartup
to regular startKoin
can go over 30% of time gained, for startup time.