https://kotlinlang.org logo
Docs
Join the conversationJoin Slack
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by Linen
koin
  • v

    voben

    03/30/2020, 8:32 PM
    If I have a viewmodel which I define like so in my fragment.
    private val viewModel by viewModel<MyViewModel>() { parametersOf(myParam) }
    How do I pass a parameter when getting an instance of myViewModel in my test?
    val vm = get<MyViewModel>()
    a
    a
    • 3
    • 2
  • j

    james

    04/02/2020, 8:39 AM
    how can I reference a list of modules, from inside another module? inside my application
    onCreate
    , I've got the following:
    startKoin {
        androidContext(this@MyApplication)
        modules(listOf(
            newsModule
        ))
    }
    and then my
    newsModule
    looks like this:
    val newsModule = module {
        listOf(
            networkModule,
            uiModule
        )
    }
    unfortunately, this doesn't work.. it builds, but everything provided inside
    networkModule
    and
    uiModule
    isn't accessible. how can I achieve this?
    v
    p
    • 3
    • 3
  • p

    pavi2410

    04/03/2020, 8:48 PM
    I have a confusion regarding ViewModel scopes I have several Fragments and each of them have their own ViewModel. My module look like this
    module {
        viewModel { ... }
        ...
    }
    My question is that whether the ViewModel is automatically scoped to the lifecycle of Fragment or not? If not, then do I need to scope ViewModel to the Fragment like this?
    module {
        scope<MyFragment> {
             viewModel { ... }
        }
    }
    v
    • 2
    • 3
  • k

    krtko

    04/05/2020, 6:15 PM
    Any chance Koin 3.0 will get published to maven anytime soon?
    a
    • 2
    • 3
  • m

    Matthieu Stombellini

    04/06/2020, 4:51 PM
    Hi, I'm getting a
    Instance creation error : could not create instance for [Single:'<http://class.that.should.be|class.that.should.be>.injected.ClassName']: java.lang.IllegalStateException: No Koin Context configured. Please use startKoin or koinApplication DSL.
    when using checkModules like this :
    @Category(CheckModuleTest::class)
    class ModuleCheck : AutoCloseKoinTest() {
        val config = ...
        val rules = ...
        @Test
        fun checkEpilinkModules() {
            with(ClassThatProvidesModules(config, rules)) {
                checkModules {
                    modules(moduleOne, moduleTwo, moduleTwo)
                }
            }
        }
    }
    and launching the class with IntelliJ IDEA or
    gradle test
    . Any ideas?
    a
    • 2
    • 3
  • k

    kenkyee

    04/07/2020, 11:18 PM
    Did you initialize Koin somewhere? Not just the test rule...the test rule just overrides stuff for testing...
    a
    • 2
    • 4
  • a

    amanda.hinchman-dominguez

    04/08/2020, 2:21 AM
    I'll let you know what I find!
    m
    • 2
    • 1
  • y

    yodgor777

    04/10/2020, 6:57 PM
    Guys I hope all of you are doing great. I am developing news app I have implemented Koin depenendcy injection with viewmodel but I am getting exception I have posted my question stackoverflow maybe anyone had the same issue before https://stackoverflow.com/questions/61145791/could-not-create-instance-for-typefactory-primary-typeyodgorbek-komilov-muso any help will be appreciated
    p
    • 2
    • 1
  • y

    yodgor777

    04/11/2020, 4:00 PM
    guys anyone life in the group
    a
    • 2
    • 1
  • r

    Rohil Chodankar

    04/16/2020, 7:19 AM
    @arnaud.giuliani We have been using Koin 2.0.1 in our Android SDK library. In our use case, the consuming application need to maintain two different instance of the SDK (based on different config passed). In koin 1.0, there was issue of scoping wherein the latest config passed would override the previous one. That was fixed in Koin 2.0.1 by implementing KoinComponent and overriding the
    getKoin() : Koin {
     return //custom koin instance maintained as a HashMap
    }
    but now once we have updated to 2.1.5 , we are again facing the issue. Is there a particular change in scoping in the latest release ?
    a
    • 2
    • 20
  • g

    ghosalmartin

    04/17/2020, 3:42 PM
    inline fun <reified T> Module.networkService(
        qualifier: Qualifier? = null,
        createdAtStart: Boolean = false,
        override: Boolean = false,
        crossinline networkDefinition: () -> Class<T>
    ) = single(qualifier, override, createdAtStart) {
        get<Retrofit>().create(networkDefinition.invoke())
    }
    t
    • 2
    • 2
  • y

    yodgor777

    04/19/2020, 6:23 PM
    I have posted my question to stackoverflow I am trying to figure out last 10 days could not find possible solution to my problem
    h
    • 2
    • 1
  • y

    yodgor777

    04/19/2020, 6:46 PM
    ???
    d
    • 2
    • 1
  • q

    quver

    04/21/2020, 2:35 PM
    Hello guys I have a question about
    scope
    and
    view models
    I found https://github.com/InsertKoinIO/koin/issues/440#issuecomment-489583308 this solution but still have questions. Could you please hellp me. I see that recommended way is to use.
    private val viewModel: TabViewModel by viewModel { parametersOf(lifecycleScope.id) }
    but what if I need global VM in many fragments with different
    lifecycle
    What is best practice for this case. how I should create scope and that it between fragments? UPD: googled laso
    sharedViewModel
    but it shared VM based on Actiity. and I need to share based on main fragment.
    a
    • 2
    • 12
  • t

    Tucker Barbour

    04/26/2020, 11:47 AM
    Does Koin support module hierarchy similar to Guice? I'm only familiar with Spring and Guice from Java projects and coming to Kotlin looking to use Koin as a pure Kotlin DI framework. I'd like to compose various modules rather than having the top-level application entry point have to know every module in the project. I.e. passing modules to
    startKoin { modules() }
    . Rather I'd like modules to be able to "install" other modules. This is how I'm used to structuring a project with Guice and maybe I'm just trying to apply this pattern where another might be better. Any help or guidance would be appreciated.
    :yes: 1
    a
    • 2
    • 2
  • f

    Fatih

    04/30/2020, 4:04 PM
    I want to scope a dependency as same life time as a `sharedviewModel`which I will inject in other short-living fragment `viewModel`s or maybe into `fragment`s. When shared model is descoped /destroyed this dependency should also be destroyed. Any ideas?
    • 1
    • 1
  • s

    sewon

    05/03/2020, 3:03 AM
    Hi, there. Is there any good tips for connecting two koin instances? My application contains many feature gradle modules. And I want to segregate each feature module’s own koin modules. Easies way to do this is to create each module’s own Koin instances. But I want to provide some shared dependencies among them. ( like instance of common modules)
    Feature Module A: create Koin for A
    Feature Module B: create Koin for B
    Common Feature Module: create Koin for Common. But Koin A, Koin B cannot get instances from Common Koin
    Another way is each module define it’s own scope and define their own dependencies in there scope. By this way, they can use instances of shared library module and segregate their own dependencies. But to force feature module to use scope is not convenient. I should put scope before every
    get()
    ,
    inject()
    call in modules.
    Common Feature Module: create Koin
    Feature Module A: create scopeA inside Common Koin
    Feature Module B: create scopeB inside Common Koin
    What I want is introducing parent - child Koin. In this concept, I can use
    KoinComponent
    easily and can segregate each feature module.
    Common Feature Module: create Koin for Common
    Feature Module A: create Koin for A. set Koin for Common as parent Koin
    Feature Module B: create Koin for B. set Koin for Common as parent Koin
    Is this reasonable feature? Or is there any other better solution for this?
    k
    • 2
    • 4
  • c

    Chintan Soni

    05/08/2020, 4:14 AM
    Hey everyone, I want to use koin with ViewModelState. Do we have documentation about how to use? I see koin now supports it.
    v
    • 2
    • 1
  • y

    Yamko

    05/08/2020, 5:35 PM
    Hi everyone. Is there any option to run
    checkModules
    in instrumentation test? I tried to do it, but constantly getting "No tests found". Mocking
    Context
    isn't an option for me.
    v
    • 2
    • 2
  • a

    amazingustav

    05/08/2020, 10:40 PM
    Hi everyone! Could someone help me? I’m getting in trouble when trying to declare a module with
    ApplicationContext
    as parameter… That’s my class:
    class Foo(environment: ApplicationEnvironment) { ... }
    That’s the class which calls
    Foo
    :
    class Bar(private val foo: Foo) { ... }
    And that’s my
    KoinModuleBuilder
    :
    object KoinModuleBuilder {
        fun modules() = listOf(module {
            single {
                Foo(get())
                Bar(get())
            }
        })
    }
    And finally, that’s the error:
    org.koin.error.NoBeanDefFoundException: No compatible definition found for type 'ApplicationEnvironment'
    a
    a
    • 3
    • 13
  • n

    Nikola Milovic

    05/14/2020, 10:32 AM
    Quick question, I couldn't really find a solution to this if I have a coroutine context that I want to be injected into my ViewModel, how would I go about separating the IO, Main, Default and so on?
    class DrillsSelectionViewModel(
        private val repository: DrillRepository,
        private val io: CoroutineContext
    ) : ViewModel() {
    
    
    val dispatchersModule = module {
    
      //  single<CoroutineContext>{Dispatchers.Main}
    
        single<CoroutineContext>{<http://Dispatchers.IO|Dispatchers.IO>}
    }
    Now if I call
    get()
    on the viewmodel, how can I specify whether I want the IO or the Main?
    a
    • 2
    • 6
  • m

    Manuel Lorenzo

    05/15/2020, 7:52 AM
    maybe it’s not a good idea and should rethink my testing strategy 🤔
    a
    • 2
    • 5
  • r

    roberto

    05/18/2020, 7:20 AM
    Hello, I’m working on an Android Library and I would like to use Koin on it. I’ve read few questions on koin’s github issue tracker but I feel I’m missing few concepts so I wonder if there’s a sample project somewhere or a tutorial where I can follow. I’ve been googling already and I couldn’t find anything so far. To be more concrete, the question I’m reading is this one https://github.com/InsertKoinIO/koin/issues/224 And Arnaud talks about creating a dedicated koin instance. I guess I’ve done something like that for testing but still, not sure how to use the CustomKoinComponent he defines on the comment with date Oct 27
    a
    • 2
    • 3
  • a

    Archie

    05/18/2020, 10:36 AM
    Hi, I was wondering how to scope viewModel to its parent fragment? in previous version we could do something like:
    val viewModel by sharedViewModel<SomeViewModel>(from = { parentFragment!! })
    but the
    from
    seems to be removed in the current
    2.1.5
    version. Any reason why was it removed? and what to do instead?
    a
    • 2
    • 6
  • d

    Deepti M

    05/18/2020, 6:47 PM
    Can anyone confirm what is the minimum Android version supported by Koin
    r
    • 2
    • 2
  • a

    Archie

    05/19/2020, 9:52 AM
    Hi, I was wondering how to properly scope dependencies. Let say I have
    SomeDependency()
    scoped to the lifecycle of the activity like so:
    val someActivityModule = module {
    
        scope<SomeActivity> {
            scoped<SomeDependency> {
                SomeDependencyImpl()
            }
        }
    }
    and then I have a fragment which needs that
    SomeDependency()
    like so:
    val someFragmentModule = module {
        fragment {
            SomeFragment(someDependency = get())
        }
    }
    I was wondering how I would be able to get the depency correctly? Can anyone guide me? Thank you very much.
    y
    h
    • 3
    • 2
  • n

    Nikola Milovic

    05/25/2020, 1:07 PM
    Is it recommended/ wise to use Koin with multimodule/ larger project? Or would sticking to dagger be a better idea?
    k
    a
    • 3
    • 2
  • y

    Yamko

    05/27/2020, 8:13 AM
    Hi all. Maybe someone can help me with this issue ? Thank you in advance. https://stackoverflow.com/questions/62038412/koin-nobeandeffoundexception
    a
    • 2
    • 3
  • a

    Andrew Steinmetz

    06/02/2020, 6:16 AM
    Hoping this is a good place to ask this, I just recently read an article about Koin 3.0 hitting alpha with support for Kotlin Multiplatform which I was happy to hear since I originally started using Koin for Android. In Android, I was always used to grabbing a dependency from Koin by marking the class with
    KoinComponent
    interface and using the
    get<SomeDependency>()
    function. Now that I have compiled my multiplatform project for iOS with the latest Koin dependency, I was wondering since I am new to swift if there was any equivalent to doing the same thing to grab a dependency from swift? This was the article I found with an example which seems to use property injection and doesn't actually create the repository as a single in iOS? https://johnoreilly.dev/posts/kotlinmultiplatform-koin/
    z
    • 2
    • 1
  • i

    igor.wojda

    06/02/2020, 7:45 PM
    Considering that I would like to have a custom
    NavManager
    class that requires
    NavController
    (Android Navigation Component) as collabolator. What would be the best way to define Koin binding for
    NavManager
    class? How instance to
    NavController
    could be retrieved (from current Activity?)
    class NavManager(private val navController: NavController) {
    
        fun navigate(navDirections: NavDirections) {
            navController?.navigate(navDirections)
        }
    }
    t
    • 2
    • 2
Powered by Linen
Title
i

igor.wojda

06/02/2020, 7:45 PM
Considering that I would like to have a custom
NavManager
class that requires
NavController
(Android Navigation Component) as collabolator. What would be the best way to define Koin binding for
NavManager
class? How instance to
NavController
could be retrieved (from current Activity?)
class NavManager(private val navController: NavController) {

    fun navigate(navDirections: NavDirections) {
        navController?.navigate(navDirections)
    }
}
t

tynn

06/03/2020, 5:57 AM
The
NavController
is bound to the Android lifecycle of its host. I’d suggest to not provide it as a dependency. Instead you could provide a
LiveData
of navigation destinations. Then you just observe it within the scope of the navigation and provide data from whereever you need to.
i

igor.wojda

06/03/2020, 7:26 AM
I am also considering this alternative, but it also has certain downsides
View count: 3