https://kotlinlang.org logo
Join SlackCommunities
Powered by
# kodein
  • d

    Daniel Weidensdörfer

    06/22/2023, 7:52 PM
    Hello, where is the right spot to ask about mockmp?
    r
    • 2
    • 2
  • a

    Arjan van Wieringen

    06/23/2023, 1:07 PM
    Is this slack channel dead? I am wondering, because we’re looking to KodeinDi for our next project but the lack of response on Slack and GitHub has us doubting this
    r
    • 2
    • 1
  • d

    Daniel Weidensdörfer

    06/23/2023, 6:47 PM
    How to make mockmp work for custom modules? I created a separate module in my app as follows:
    Copy code
    val commonClientMain by creating {
        dependsOn(commonMain)
        dependencies ...
    }
    val commonClientTest by creating {
        dependsOn(commonTest)
        dependencies {
            implementation(kotlin("test"))
            implementation("org.kodein.mock:mockmp-runtime:1.14.0")
            implementation("org.kodein.mock:mockmp-test-helper:1.14.0")
        }
        kotlin.srcDir("build/generated/ksp/jvm/jvmTest/kotlin")
    }
    I'm not able to successfully make ksp generate the injectMocks function for my custom module. I've tried it with gradle plugin as well as manual configuration. Also it works on the default commonTest. Any ideas?
    i
    • 2
    • 3
  • d

    Daniel Weidensdörfer

    07/03/2023, 11:18 AM
    How would you go about mocking a library class that has no interface, i. e. HttpClient of ktor?
    i
    r
    • 3
    • 4
  • m

    Meika

    07/06/2023, 6:43 AM
    hi folks, anyone tried latest kodein (7.20.2) and able to run with obfuscation? currently adding proguard rules provided in https://github.com/kosi-libs/Kaverit/issues/6 and still showing error
    java.lang.RuntimeException: Invalid TypeToken; must specify type parameters
    at our end
    r
    • 2
    • 2
  • d

    Daniel Weidensdörfer

    07/08/2023, 9:10 AM
    Can we add support for getting an instance from DI directly, without delegate?
    c
    • 2
    • 2
  • o

    Olivier Patry

    08/17/2023, 1:20 PM
    Hi, I'm facing a lifecycle issue using
    bindFactory
    . FTR: I'm in the context of a Jvm Desktop application, unrelated to Android. I try to keep alive an object bound to a screen, this object is created with
    bindFactory
    taking a parameter specific to the screen being navigated to. So far, I had no issue, but when moving my window from one screen to another, the whole screen logic was re-triggered (Compose for Desktop context), and thus calling again
    bindFactory
    , how can I reuse the previously created object?
    c
    • 2
    • 3
  • e

    Evgeniy Zhelenskiy

    08/17/2023, 7:02 PM
    When is Kotlin 1.9.0 going to be supported?
    r
    • 2
    • 1
  • d

    Drew

    09/06/2023, 4:30 PM
    Is there a reliable way to check if any dependencies are missing at build time rather than at runtime? Koin uses
    verify
    and
    checkModules
    , was trying to see if something similar existed for Kodein.
    i
    r
    • 3
    • 2
  • s

    Stephen Bellanger

    09/28/2023, 6:34 AM
    Hey im new to
    kodein
    and
    kmm
    somebody here that has a repo or example i could check out for setting up
    kodein
    for Android and iOS
  • r

    rocketraman

    10/25/2023, 5:40 PM
    I have a fatal exception stack trace from our android app that looks like this -- it looks like some android event is triggering the type system in Kodein, and causing this error -- but I have no idea what the root cause might be. This is with Kodein 7.18.0 which appears to be using kaverit-jvm 2.3.0. Anyone have any ideas?
    Copy code
    java.lang.StringIndexOutOfBoundsException: length=2; index=10
    	at java.lang.String.substring(String.java:2060)
    	at vg.l.g1(Unknown Source:31)
    	at myapp.GlobalApplication$di$2$invoke$lambda$0$$inlined$bind$default$1.i(typeTokensJVM.kt:36)
    	at myapp.GlobalApplication$di$2$invoke$$inlined$singleton$default$7.invoke(typeTokensJVM.kt:15)
    	at androidx.compose.foundation.text.selection.SelectionMagnifierKt$rememberAnimatedMagnifierPosition$1$2.i(SelectionMagnifier.kt:94)
    	at bh.g0.i(Unknown Source:169)
    	at ge.b0.l0(Unknown Source:126)
    	at kotlinx.coroutines.flow.internal.ChannelFlowTransformLatest$flowCollect$3$1$emit$1.invokeSuspend(Merge.kt)
    	at kotlin.enums.EnumEntriesList.resumeWith(EnumEntries.kt:7)
    	at yg.m0.run(Unknown Source:113)
    	at androidx.compose.ui.platform.AndroidUiDispatcher.performTrampolineDispatch(AndroidUiDispatcher.android.kt:81)
    	at androidx.compose.ui.platform.AndroidUiDispatcher.access$performTrampolineDispatch(AndroidUiDispatcher.android.kt:41)
    	at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.run(AndroidUiDispatcher.android.kt:57)
    	at android.os.Handler.handleCallback(Handler.java:883)
    	at android.os.Handler.dispatchMessage(Handler.java:100)
    	at android.os.Looper.loop(Looper.java:214)
    	at android.app.ActivityThread.main(ActivityThread.java:7405)
    	at java.lang.reflect.Method.invoke(Native Method)
    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:502)
    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)
    	Suppressed: dh.g: [l1.m1@82d6cff, f0.e@ec74acc, y1{Cancelling}@96d3115, p0@3b07c2a]
    If the line number is right, the exception is thrown from code called by https://github.com/kosi-libs/Kaverit/blob/bbb588d24a686199b0671d0bf5befc5c47797347/kaverit/src/jvmMain/kotlin/org/kodein/type/typeTokensJVM.kt#L36, but that doesn't seem to make much sense.
    r
    • 2
    • 17
  • r

    romainbsl

    11/18/2023, 5:01 PM
    Hi everyone 👋 The long overdue *Kodein 7.21.0* is finally out! 👉 This is mainly an upgrade of Kotlin (1.9.20) and dependencies. We are also upgrading other KOSI libraries, like our KMP logging library *Canard 1.1.0* As usual do not hesitate to create issues or ping us for any question / suggestion. Happy Kotlin K
  • a

    Arjan van Wieringen

    11/22/2023, 6:11 PM
    Hello, is it possible to get some sort of injection graph out of KodeinDI?
    r
    • 2
    • 1
  • k

    kluck

    12/06/2023, 4:11 PM
    Hello, I'm trying to use KodeinDI alongside FlowMVI (https://github.com/respawn-app/flowMVI). In their sample, they provide extension functions for injecting the view model via Koin, and I tried to the best of my capability to reproduce what they made. Here's their code:
    Copy code
    @Composable
    inline fun <reified T : Container<S, I, A>, S : MVIState, I : MVIIntent, A : MVIAction> storeViewModel(
        viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
            "No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
        },
        key: String? = null,
        extras: CreationExtras = defaultExtras(viewModelStoreOwner),
        scope: Scope = getKoinScope(),
        noinline parameters: ParametersDefinition? = null,
    ): StoreViewModel<S, I, A> = getViewModel(qualifier<T>(), viewModelStoreOwner, key, extras, scope, parameters) // -> This is a koin method
    What I came up with:
    Copy code
    @Composable
    fun <S : MVIState, I : MVIIntent, A : MVIAction> storeViewModel(
        viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
            "No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
        },
        tag: Any? = null,
    ): StoreViewModel<S, I, A> = with(localDI()) {
        getViewModel(viewModelStoreOwner = viewModelStoreOwner, tag = tag, di = this)
    }
    
    @Composable
    inline fun <reified T : ViewModel> getViewModel(
        viewModelStoreOwner: ViewModelStoreOwner,
        tag: Any?,
        di: DIAware,
    ): T = resolveViewModel(T::class, viewModelStoreOwner, tag, di)
    
    
    inline fun <reified T : ViewModel> resolveViewModel(
        vmClass: KClass<T>,
        viewModelStoreOwner: ViewModelStoreOwner,
        tag: Any?,
        di: DIAware,
    ): T {
        val factory = di.viewModelFactory(tag)
        val provider = ViewModelProvider(
            store = viewModelStoreOwner.viewModelStore,
            factory = factory,
            defaultCreationExtras = defaultExtras(viewModelStoreOwner),
        )
        return provider[vmClass.java]
    }
    
    
    fun DIAware.viewModelFactory(tag: Any?): ViewModelProvider.Factory = object : ViewModelProvider.Factory {
        override fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T =
            di.direct.Instance(type = erased(modelClass), tag = tag)
    }
    It kind-of works, as I manage to retrieve my first store view model (
    val store = storeViewModel<MainState, MainIntent, MainAction>(tag = MAIN_STORE_TAG)
    ). But as soon as I try injecting a second one, it retrieves the first one… I use tags to distinguish the two, but I guess there is some erasing stuff that I don't get correctly… Any idea what I could change in my code? Thanks!
    r
    v
    • 3
    • 6
  • x

    Xad Kile

    12/18/2023, 8:28 AM
    Hi, I have a quick question about Kodein: are errors caught in compile time or runtime? Thank you
    i
    • 2
    • 2
  • e

    electrolobzik

    01/17/2024, 11:46 AM
    What are the main advantages of Kodein over Koin in context of multiplatform project? What made you choose it over Koin?
    r
    i
    a
    • 4
    • 36
  • m

    Mike Dawson

    01/26/2024, 12:39 PM
    Has anyone tried using Kodein-Di with progaurd on Desktop/JVM? I'm trying to use it with a Compose Desktop app. I added the same config as per the docs (which works on Android)
    Copy code
    -keep, allowobfuscation, allowoptimization class org.kodein.type.TypeReference
    -keep, allowobfuscation, allowoptimization class org.kodein.type.JVMAbstractTypeToken$Companion$WrappingTest
    
    -keep, allowobfuscation, allowoptimization class * extends org.kodein.type.TypeReference
    -keep, allowobfuscation, allowoptimization class * extends org.kodein.type.JVMAbstractTypeToken$Companion$WrappingTest
    Still getting the runtime exception about an invalid type token when using the version processed via proguard. Works fine without obfuscation. Proguard reduces the package size from 160MB to 80MB, so definitely want to use proguard with KodeIn on JVM if possible
    e
    • 2
    • 13
  • e

    electrolobzik

    01/27/2024, 4:59 PM
    What I need to import to make this work?
    Copy code
    class MainActivity : ComponentActivity(), DIAware {
    
        override val di by di()
    Right now getting
    Type checking has run into a recursive problem. Easiest workaround: specify types of your declarations explicitly
    and looks like AS doesn’t know what is
    di()
    I am using v7.21.2 with main dependency in the common module and
    kodein-di-framework-android-x
    in the androidMain.
    r
    • 2
    • 2
  • e

    electrolobzik

    01/27/2024, 5:53 PM
    Can anyone share some example of using Kodein v7 with scopes? I didn’t find any in the repository and documentation is confusing sometimes. It looks like I am missing something very basic, since I don’t follow this code in the doc:
    Copy code
    val user by di.on(session).instance()
    I understand that this should do and what it means. But in real life the
    session
    will be nullable and I can’t get user before I get a new session. Looks like I can’t just “open” the scope once the user is authorized, I have to specify the
    session
    each time which is very inconvenient if it’s nullable.
  • d

    diego-gomez-olvera

    02/02/2024, 2:18 PM
    hello! is anyone here using Kodein LevelDB API? I see that its development was stopped but it seems interesting having KMP support for LevelDB
    r
    • 2
    • 7
  • d

    Dmitry Danilau

    02/26/2024, 6:21 PM
    Hello, I’m using mocKMP, want to say ty for nice plugin. I have a question, is it possible to override mock return value in the concrete unit test?
    r
    • 2
    • 5
  • o

    Olaf Hoffmann

    03/07/2024, 10:02 AM
    Hey everyone, I need a bit of help with Kodein in the shared code of our KMP app, which is causing crashes for some of our iOS users. While our iOS devs can't reproduce the issue, Firebase logs show it happens almost daily. Our Android users are not experiencing this problem, even though both platforms use the same shared code. The problem concerns Kotlin Native's
    HashMap
    throwing a
    NullPointerException
    when Kodein's
    StandardScopeRegistry
    attempts to retrieve a dependency reference from its
    _cache
    inside a
    synchronizedIfNull
    predicate. For native platforms, the
    newConcurrentMap
    function used to create the
    _cache
    returns a Kotlin Native HashMap. The implementation of this Map shows the code making a non-null assertion on the Map's internal value array. When creating the Map, this array is initialized to `null` and is only allocated when writing a value. Hence, the Exception in this place seems to happen because the Map was never written before reading from it. What's still puzzling to me is that the only place where
    _cache
    is being written is when the predicate in the registry returns
    null
    . Given that a fresh HashMap should always throw, I'm not sure how the predicate is not always leading to the NPE. Since this issue occurs on iOS only, I looked into Kodein's platform implementations of
    maySynchronized
    , which is being called internally by
    synchronizedIfNull
    . In the native implementation of that function, the
    block
    passed in is being called immediately without checking the
    lock
    parameter. I assume that's intentional due to missing synchronization facilities in native, but to be fair, it looks suspicious when considering the possibility of a race condition. While I'm reasonably confident about my findings, this last assumption is merely a guess. Also, it doesn't explain how the `_cache`'s internal array manages to get initialized in the first place when the code suggests that a
    get
    is always the first operation performed on that Map. I appreciate any help figuring out what's happening here, how we may fix it on our end, or whether this is a problem with the Kodein implementation. For completeness, I've attached the Crashlytics stack trace exported from Firebase so you can investigate it if desired. Thanks a lot in advance for all your assistance!
    crashlytics_stack_trace_kodein_npe.txt
    s
    e
    r
    • 4
    • 10
  • w

    William Reed

    05/21/2024, 5:07 PM
    does kodein support providing objects that are created via suspend functions?
    r
    • 2
    • 3
  • r

    rocketraman

    09/16/2024, 2:26 PM
    I have a singleton that implements two interfaces, one of which needs to be bound to a
    Set
    . Is this a good pattern, or is there a cleaner approach?
    Copy code
    singleton { new(::Foo) }.let {
      bind<IFace1>().inSet() with it
      bind<IFace2>() with it
    }
    Looks pretty clean to me but want to make sure I'm not missing something.
    i
    • 2
    • 1
  • m

    Mari-Mai HENAFF

    09/17/2024, 8:20 AM
    Hi, I’m migrating my android project to compose navigation. I’m using org.kodein.dikodein di framework compose7.22.0 for dependency injection. According to documentation https://kosi-libs.org/kodein/7.22/framework/compose.html I can use rememberViewModel() to retrieve a ViewModel in a @Composable, but it is marked as deprecated. I switched to org.kodein.dikodein di framework compose android7.22.0 but still have deprecation message... Also according to documentation I can use NavBackStackEntry.navGraphViewModel(navHostController), but this method seems not to exist... What am I doing wrong ?
    ➕ 1
    r
    • 2
    • 26
  • d

    Daniel Weidensdörfer

    10/12/2024, 2:11 PM
    Hello, given I have several singleton usecases defined in a module. How to scope their creation to the value of a variable, like the current user? I read the documentation, but couldnt figure it out.
    r
    • 2
    • 4
  • h

    Hans van Dodewaard

    11/08/2024, 8:53 AM
    Hello, We are a happy user of Kodein-DI and Compose for a compose-web application. Question: are there any plans to support compose 1.7 in kodein?
    r
    • 2
    • 8
  • f

    filipebaptista

    02/03/2025, 3:36 PM
    Hi 👋 I have an issue in MockMP, similar to the one opened https://github.com/kosi-libs/MocKMP/issues/84 I saw that there is an open MR, can someone from the MockKMP team take a look? Thanks 🙏
  • r

    rocketraman

    04/01/2025, 2:11 PM
    I have a system which can have plugins at runtime, and these plugins need to expose DI modules that should be loaded into the application. What is the best way of doing this? One approach I was thinking about was using a Java
    ServiceLoader
    but is there a "native" way?
  • v

    Vaizin Nikita

    05/01/2025, 4:43 PM
    Hi @romainbsl did you have any chance to update deps and maybe release the constructor DSL changes? We're having an issue where because of the old version of CMP used in Kodein, our native app builds fail unless we disable caching.
    r
    h
    • 3
    • 7