Hi, how do I inject context with Koin? I am follow...
# koin
t
Hi, how do I inject context with Koin? I am following this guide: https://blog.insert-koin.io/koin-in-kotlin-multiplatform-a-complete-guide-576a24ffeeab but also working with expected and actual objects for modules. I need context for init-ing sharedpreferences in android. I tried putting it in class constructor of the actual implementation but it fails to compile.
d
You can define it in your class constructor
Copy code
@Single
class MyClass(context: Context)
And pass the context instance while initializing koin
Copy code
onCreate() {
    startKoin {
       androidContext(context)
       modules(koinModule)
    }
}
You can get more details in the official documentation https://insert-koin.io/docs/reference/koin-android/start/
t
Thanks, I did looked into that but as per that blog I shared, passing context during onCreate shouldn't be required with this method, right?
Copy code
KoinApplication(configuration = koinConfiguration<KoinApp>()) {}
I am also using the new compiler plugin with annotations so maybe I am missing something?
d
I guess you refer to Composable function to init koin in Compose. I don't have experience using it, but it should inject context automatically like you said. What error are you experiencing? Also did you try to manually pass context to koin configuration just in case?
t
Untitled
Expected:
Copy code
package net.newpipe.app.di

import com.russhwolf.settings.Settings
import org.koin.core.annotation.Configuration
import org.koin.core.annotation.Module
import org.koin.core.annotation.Singleton

/**
 * Expected settings module to access key-value pairs across different platforms
 */
@Module
@Configuration
expect class SettingsModule {

    @Singleton
    fun provideSettings(): Settings
}
Actual:
Copy code
package net.newpipe.app.di

import android.content.Context
import androidx.preference.PreferenceManager
import com.russhwolf.settings.Settings
import com.russhwolf.settings.SharedPreferencesSettings
import org.koin.core.annotation.Configuration
import org.koin.core.annotation.Module
import org.koin.core.annotation.Singleton

@Module
@Configuration
actual class SettingsModule(private val context: Context) {

    @Singleton
    actual fun provideSettings(): Settings = SharedPreferencesSettings(
        PreferenceManager.getDefaultSharedPreferences(context)
    )
}
I haven't tried passing context manually to koin as I wasn't sure how to do that with the expect-actual classes and the new compose method. Any tips for that?
d
Oh, so you have the compiler plugin crash. This is definitely a compiler plugin issue. Could you ensure you use the latest Kotlin and Koin-compiler-plugin versions. If the error still exists, please file an issue https://github.com/InsertKoinIO/koin-compiler-plugin
t
Thank you. I will open an issue. I am using the latest version of both Koin and compiler-plugin (RC2).
❤️ 1
d
Btw in your case you don't need expect/actual You can define your module in common code with ComponentScan annotation and add your Singleton in Android like this
Copy code
@Singleton
fun provideSettings(context: Context): Settings = ...
When you build for Android the Koin plugin will bind your provideSettings function to your Module
t
Does that means that its okay to define singleton outside the module? I was assuming they need to be defined inside the module class which is why I went with expect-actual.
Sorry for the dumb questions but I am confused how the binding of singleton to module would work in this case
d
If you use koin compiler plugin with ComponentScan annotation then no need to put into class
You provide your package name in ComponentScan annotation parameter and compiler plugin includes all your definitions from your package into this module
👍 1
t
Got it. What if I have two modules in the same package? Will it be still able to bind it correctly in that case?
d
It's up to you. For example for this module
Copy code
@Module
@ComponentScan("com.example")
class MyModule
All definitions from com.example will be included into this module. If you have two modules with this annotation and package name, then definitions will be included into both modules. But usually I wouldn't recommend doing this.
❤️ 1
t
I understand it now. Thank you for explaining it. Quite helpful.
❤️ 1
d
Just in case you can include modules one into another. For example have one module with ComponentScan that is included into another module without ComponentScan. You can experiment and share your findings 🙂
👍 1