-
-
Notifications
You must be signed in to change notification settings - Fork 719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[question] Best way to isolate Koin modules/singles when building an Android library module? #224
Comments
+1 for this question. I'm running in the same issue and currently I'm having to put name parameters on everything that client app might be using such as all Android framework classes and common libraries stuff such as OkHttp / Retrofit. Furthermore I would like to expand a bit the question regarding the namespaces for the modules. Quick example of what I mean is:
and then if the app tries to declare the
it crashes with:
|
You can declare several modules with the same name, to share the same namespace. For example: val myLibraryModule = module("com.our.company.package.lib.name") {
.... declarations...
}
val appModule = module("com.our.company.package.lib.name") {
single { awesomeLibrary(context) }
} second thing is to request dependencies via their names, to avoid request it globally: This is for your SDK. But it won't avoid your underlying app resolve against your definitions. Isolation should be better. :/ |
Best way to isolate your use of Koin, is doing it with a dedicated Koin instance. You have to make it more "manually":
From your KoinContext, you can use any feature. And you can reuse it in a custom component, using
API has been enhanced in |
@arnaudgiuliani as far as I see, Should we use |
mmh docs can't even build. This line is totally incorrect: |
@NicoloParolini
internal object SharedModuleKoinHolder {
var koinApplication = koinApplication { }
}
abstract class SharedKoinComponent : KoinComponent {
override fun getKoin() = SharedModuleKoinHolder.koinApplication.koin
} Finally override value of internal object DiInitializer {
internal fun setupDi(isDebug: Boolean) {
SharedModuleKoinHolder.koinApplication = koinApplication {
printLogger(getLoggerLevel(isDebug))
modules(getModules(isDebug))
}
}
} All your injections happen after you initialize your modules so it should work properly, then with use of given abstract implementing internal class KoinInteractorProvider : SharedKoinComponent() {
internal inline fun <reified Type : Interactor> create(): Type = get()
} It's all build according to the docs, expect the first part of init initialization with default empty |
This part of your code does not even make any sense. I mean I wanna keep all library implementations in module where |
@mecoFarid Nah, |
the the idea is to keep your |
Currently in my project I have an android app module and an android library module for an SDK I'm building.
Both Android modules are using Koin. The app module is using koin-android-viewmodel and the SDK module is using koin-core with its own KoinComponent.
I'm running into issues since both the app and SDK have a dependency on a Koin single OkHttpClient. After calling loadKoinModules() It looks like these two Android modules end up sharing the same pool of Koin singles and it crashes because it doesn't know which OkHttpClient to pick when I call get().
I've got it working by using the name parameter for both OkHttpClient Koin singles and passing it in the get('name') whenever I want the OkHttpClient in either Android Module. I don't want to have to force my SDK user to have to use these named params if they are using Koin with a OkHttpClient as well.
I would prefer if I could just reduce the visibility of my OkHttpClient in my SDK to only be visible inside the SDK module and also so it can not see the other OkHttpClient in my app module.
Is there a better way to do this than using the single name param?
Is it possible to have two Koin instances using startKoin() instead of loadKoinModules() without crashing?
Thanks :)
The text was updated successfully, but these errors were encountered: