Mihai Voicescu
11/12/2021, 5:45 PM::class.getFullName()
as the qualifier. This seems to have a major drawback: if 2 classes are named the same they will count as the same one, even though they are different
Why not use ::class.js
as the qualifier? This grants access to the actual JS implementation so even if 2 classes are named the same they will count as different.
If you really want the Map.Key of the instances to be String you can also use ::class.js.name
. This will provide the actual name of the class which should be unique given a module at least.
Maybe even use the typeOf<T>()
as the key? This should also solve template collision.russhwolf
11/16/2021, 6:15 PMShalom Halbert
11/27/2021, 1:28 PMloadModules
and unloadModules
, which worked…until I found out it that the module didn’t load before tests started. That yielded test failures that were difficult to debug.
The solution, that doesn’t require test code in production code, was to:
1. Create a new class that extends the production Application
class, let’s call the child InstrumentedTestApplication
2. In InstrumentedTestApplication
, override the list of modules provided in `startKoin`’s modules
declaration to include a testModule
3. Create a new class that extends AndroidJunitRunner
, let’s call it InstrumentedTestJunitRunner
that will instantiate InstrumentedTestApplication
4. Point testInstrumentationRunner
in the app-level build.gradle to InstrumentedTestJunitRunner
Is this something that is in the Koin documentation, and I missed it? If not, is there interest in having it added?Marcello Galhardo
12/14/2021, 11:01 AMarnaud.giuliani
01/07/2022, 4:45 PMsendoav
01/19/2022, 10:06 PMVivek Modi
01/22/2022, 4:50 PMinject()
and viewModel()
? I used both in when injecting in viewmodel and both works fine. I didn't figure out what the d/w in both. Which one need to use for viewModel. Any suggestions? Thanksarnaud.giuliani
02/09/2022, 3:17 PMarnaud.giuliani
02/09/2022, 3:18 PMarnaud.giuliani
02/09/2022, 3:19 PMincludes
contribution. Seeing some perf impact, trying to see if I can optimise it a bit before releasing first 3.2.0-beta-1Pedro Francisco de Sousa Neto
02/15/2022, 7:58 PMMarcello Galhardo
02/16/2022, 5:59 PMRuben Quadros
03/25/2022, 5:19 PMViktor Orlyk
04/21/2022, 12:08 PMimplementation("io.insert-koin:koin-ktor:$koin_version")
implementation("io.insert-koin:koin-logger-slf4j:$koin_version")
```import io.ktor.server.application.*
import org.koin.ktor.ext.Koin
fun Application.configureDependancyInjection() {
install(Koin) {
slf4jLogger()
modules(helloAppModule)
}
}
```Type mismatch: inferred type is Koin.Feature but Plugin<TypeVariable(P), TypeVariable(B), TypeVariable(F)> was expected
Pedro Francisco de Sousa Neto
05/26/2022, 3:04 PMMark Malik
06/18/2022, 8:32 PMRuben Quadros
06/25/2022, 6:27 AMmbonnin
09/19/2022, 2:31 PMLukas Anda
12/19/2022, 11:46 AMPedro Francisco de Sousa Neto
12/19/2022, 1:26 PM3.1.6
to 3.3.0
.
When I run app I’m getting this error.
java.lang.NoSuchMethodError: No static method binds(Lkotlin/Pair;[Lkotlin/reflect/KClass;)Lkotlin/Pair; in class Lorg/koin/dsl/DefinitionBindingKt; or its super classes (declaration of 'org.koin.dsl.DefinitionBindingKt' appears in /data/app/~~aqqlOmEpOuZRMkiV16sAeA==/XXX.YYY.MYPACKAGE.AAA.BBBB-Wnq8EpEHG1vbVsljdmNEow==/base.apk!classes24.dex)
I’ve already cleaned, rebuild, restarted etc and it isn’t working.
Someone as getting this error too? How can I fix it?arnaud.giuliani
12/19/2022, 1:43 PMPedro Francisco de Sousa Neto
12/19/2022, 2:56 PMkoin_android = '3.3.1'
koin_core = '3.3.0'
This can happen If any of my libraries are using a old Koin version (like 3.1.6)? 🤔Emanuel Moecklin
12/23/2022, 1:02 PMfragmentFactory()
to inject Fragments into Activities and that works like a charm. I was wondering if I could use an AppComponentFactory
to do the same for Activities and made a quick prototype:
val componentFactoryModule = module {
single<AppComponentFactory> { KoinComponentFactory() }
}
fun KoinApplication.componentFactory() {
koin.loadModules(listOf(componentFactoryModule))
}
class KoinComponentFactory : AppComponentFactory(), KoinComponent {
override fun instantiateActivity(
cl: ClassLoader, className: String, intent: Intent?
): Activity {
val clazz = Class.forName(className).kotlin
val instance: Activity? = getKoin().getOrNull(clazz)
return instance ?: super.instantiateActivity(cl, className, intent)
}
}
add to startKoin:
startKoin {
componentFactory()
...
and register the factory in the manifest:
tools:replace="android:appComponentFactory"
android:appComponentFactory="mypackage.KoinComponentFactory"
This also works like a charm and I would love this to be part of the library itself but I'm not sure if this is the way to go and how this needs to be changed to play nicely with scopes?Pedro Francisco de Sousa Neto
12/23/2022, 7:25 PM1 - Activity using a koin module scope tied
Pedro Francisco de Sousa Neto
12/23/2022, 7:33 PMscope<ActivityProfile>
was unloaded by ActivityProfile#2
?
1 - A activity class to see users profile, like ActivityProfile
2 - ActivityProfile has a module tied to it and this activity load and unload this module
3 - Using the app, ActivityProfile is called twice with distinct parameters like userIDs (so it's a expected behaviour)
4 - ActivityProfile#1 is in navigation stack and ActivityProfile#2 is in top (user is looking for it)
5 - User destroy the activity using back button so ActivityProfile#2 calls onDestroy. When this activity is closed user start to look ActivityProfile#1
6 - ActivityProfile#1 try to access Koin dependencies and ??????
Elio Maroun
01/03/2023, 9:08 AMElio Maroun
01/03/2023, 9:08 AMChristian Krueger
01/10/2023, 12:54 PMSharedViewModelCompat
passed the wrong ViewModelStore (see bug issue)
2. The ViewModelCompatgetViewModel
always sets extras = CreationExtras.Empty
, which causes a missing SAVED_STATE_REGISTRY_OWNER_KEY
I can fix both issues + writing some unit tests.harshil shah
01/24/2023, 2:40 AMPedro Francisco de Sousa Neto
02/01/2023, 2:08 PMprivate val presenter: MyFeaturePresenter by scope.inject()
Sometimes I got the crash
Caused by: java.lang.IllegalStateException: Fragment MyFeatureFragment{e397a0b} (4be986d3-729a-430d-86ee-ee1f657be059) not attached to an activity.
at androidx.fragment.app.Fragment.requireActivity(Fragment.java:995)
at org.koin.androidx.scope.FragmentExtKt.createFragmentScope(FragmentExt.kt:38)
at org.koin.androidx.scope.FragmentExtKt$fragmentScope$1.invoke(FragmentExt.kt:50)
at org.koin.androidx.scope.FragmentExtKt$fragmentScope$1.invoke(FragmentExt.kt:50)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
Why?
Because this value val
is initializing in init
from Kotlin fragment class.
How to avoid that
private val presenter: MyFeaturePresenter by lazy { scope.get() }
Is it the expected scenario to Koin lazy injection? 🤔Pedro Francisco de Sousa Neto
02/01/2023, 2:08 PMprivate val presenter: MyFeaturePresenter by scope.inject()
Sometimes I got the crash
Caused by: java.lang.IllegalStateException: Fragment MyFeatureFragment{e397a0b} (4be986d3-729a-430d-86ee-ee1f657be059) not attached to an activity.
at androidx.fragment.app.Fragment.requireActivity(Fragment.java:995)
at org.koin.androidx.scope.FragmentExtKt.createFragmentScope(FragmentExt.kt:38)
at org.koin.androidx.scope.FragmentExtKt$fragmentScope$1.invoke(FragmentExt.kt:50)
at org.koin.androidx.scope.FragmentExtKt$fragmentScope$1.invoke(FragmentExt.kt:50)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
Why?
Because this value val
is initializing in init
from Kotlin fragment class.
How to avoid that
private val presenter: MyFeaturePresenter by lazy { scope.get() }
Is it the expected scenario to Koin lazy injection? 🤔fun Fragment.createFragmentScope(): Scope {
if (this !is AndroidScopeComponent) {
error("Fragment should implement AndroidScopeComponent")
}
// if (this.scope != null) {
// error("Fragment Scope is already created")
// }
val scope = getKoin().getScopeOrNull(getScopeId()) ?: createScopeForCurrentLifecycle(this)
val activityScope = requireActivity().getScopeOrNull()
if (activityScope != null) {
scope.linkTo(activityScope)
} else {
scope.logger.debug("Fragment '$this' can't be linked to parent activity scope")
}
return scope
}
I think the root cause is the usage of requireActivity()
inside createFragmentScope()
.Marcello Galhardo
02/01/2023, 5:37 PMrequireActivity
, after all the Fragment scope is a child from the Activity
(see scope.linkTo
in the code snippet you posted). Ignoring that would break the relationship between these two scopes.
Your proposed solution seems off. Aren’t inject
lazy by default? Wrapping it inside a lazy
should not change the behaviour. Check the last line of you stack trace SynchronizedLazyImpl.getValue
.
What do you mean by “val is initialized in init”? Do you have a code sample for this statement?arnaud.giuliani
02/06/2023, 10:56 AMScopeFragment
classPedro Francisco de Sousa Neto
02/06/2023, 1:59 PMScopeActivity
and ScopeFragment
.
I don’t know yet how can reproduce this error, but I’m getting him sometimes in Firebase Crashlytics.
My guess: sometimes fragment is initializing “too fast” and is not attached to activity yet, so requireActivity() throws an exception.arnaud.giuliani
02/06/2023, 5:30 PMPedro Francisco de Sousa Neto
02/06/2023, 5:43 PM3.3.1
, what version has it fixed?arnaud.giuliani
02/06/2023, 5:49 PMPedro Francisco de Sousa Neto
02/06/2023, 5:57 PMMarcello Galhardo
02/06/2023, 8:31 PMonCreate
and that is where your Fragment initialization logic should go to ensure your Fragment is in a safe state.
If you use lazy
, you should ensure it is only called "after" onCreated
state is reached or you likely will crash.Pedro Francisco de Sousa Neto
02/06/2023, 8:40 PMinject
in Koin usage is lazy and can handle this kind.
The problem is when you trigger a combination of createScope()->requireActivity()
because you just added a fragmentScope()
. And you’re not expecting that 😅.Is it the expected scenario to Koin lazy injection?I think it’s not expected get a crash when you’re using
scope.injection
.
Maybe we need to improve docs, or annotate it with as throwable.
I really don’t know arnaud.giuliani
02/07/2023, 12:54 PMMaybe we need to improve docs, or annotate it with as throwable.yes documentation can be improved on this point 👍
Pedro Francisco de Sousa Neto
02/07/2023, 1:01 PMarnaud.giuliani
02/07/2023, 1:39 PM