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
kodein
  • k

    kluck

    04/12/2018, 7:07 AM
    I ended up doing something like that, but I doubt it's the idiomatic way…
    class BatteryBroadcastReceiver : KodeinAware, BroadcastReceiver() {
    
        override lateinit var kodein: Kodein
    
        private val myBinding: Stuff by instance()
    
        override fun onReceive(context: Context?, intent: Intent?) {
            if (!::kodein.isInitialized) {
                kodein = (context as KodeinAware).kodein
            }
            ...
        }
    }
    e
    • 2
    • 1
  • s

    salomonbrys

    04/12/2018, 12:23 PM
    So... here we go! ANNOUNCING KODEIN-DI 5.0.0!!! First of all, some acknowledgments: - Huuuuge thank you to @olonho and his amazing team of Kotlin/Native. For creating the technology of course, but also for being ultra-reactive! Most of the bugs & crashes that I reported were fixed within 72 hours. Some missing pieces were prioritized to allow Kodein 5.0 to be compatible with Kotlin/Native. I have never worked with such a great team and it has been an honor to contribute, as little as I did, to this great endeavour! - Great thanks to all of you who annimate this channel while I'm away. I've never been a social community guy, and it is because you answer here on Slack as well as on other medium that this community lives and this library finds it audience. So thank you to @Andreas Sinz, @streetsofboston, @jdorleans, @kailan, @dave08 and everyone that I forgot to mention here 😉 - Thanks to all of you who come with new ideas. 80% of Kodein-DI's features are because of your dissatisfaction in the current state of Kodein. Features such as subtype bindings, argument uptype handling, multiton, all matching retrieval, set binding, and much more are included because someone came and ask "How can I do that?" Now, for the annoucements themselves. First and foremost, Kodein-DI 5.0.0 stable is live in JCenter. It took some time because of a bug in JCenter (only half the packages were released). There are two main differences from the previous beta 5 version: - The packages have changed again (sorry about that): they are now inside
    org.kodein.di.*
    - Introducing subtype bindings, which allow to register a parent type and the a binding factory that will provide a binding for each new subtype of this parent type. As usual, everything is in the documentation (http://kodein.org/Kodein-DI/) and changelog (https://github.com/Kodein-Framework/Kodein-DI/blob/master/CHANGELOG.md). Second, announcing the Kodein Framework (http://kodein.org). It does really not exist yet: Kodein-DI is (at the moment) the only released component of the framework. This framework will propose various components that are focused on multi-plateform development (JVM/Android/JS/Native/iOS). Note that each component will be usable by its own and that Kodein-DI, as well as other components, will continue to also be focused on the JVM and Android. Being multi-platform does NOT mean sacrificing JVM only goodies. Kodein-DI 5 includes multiple features that are available only to JVM & Android, and those features will continue to be supported. Some components of the framework (such as Kodein-JSON) will be focused exclusively on multi-platform and some other (such as Kodein-DI) won't. The choice is made based on the usefulness of the component outside of the multi-platform ecosystem. The framework itself will aim to provide a full ecosystem to easily create business code, modules and architecture to be shared between platforms. In term, applications that are developed with the Kodein Framework should contain 60% to 80% of shared code between platforms. Finally, to support all of that, I'm very happy to announce the Kodein company: http://kodein.net . This company, based in France but active in all Europe, develops three major axes: - Kotlin consultancy & development services. - Kotlin training & migration programs. - Kodein framework development & support. If you would like to work or contract with us, feel free to contact me at salomon@kodein.net. Note that the Kodein framework in general and the Kodein-DI library in particular will remain fully Open-Source and that the community will continue to be supported. I am Open-Source at heart and the company we are building is too. We plan to monetize through services: consultancy, SLA support, training, development, etc. All of this is still in early stages. In the meantime, I hope you enjoy Kodein-DI 5.0.0 😉 Salomon.
    :kotlin-flag: 5
    👍 5
    🎉 13
    👏 9
    :kotlin: 4
    a
    • 2
    • 3
  • s

    streetsofboston

    04/16/2018, 4:40 PM
    @salomonbrys What do you think of this reddit? https://www.reddit.com/r/androiddev/comments/8ch4cg/dagger2_vs_koin_for_dependency_injection/dxf4o08/?context=2
    a
    • 2
    • 1
  • d

    dave08

    04/22/2018, 5:17 PM
    @salomonbrys Can one imported
    Kodein.Module
    access the bindings in another imported module, or only the importing
    Kodein
    has access to all the module's bindings? If the latter then a module will have to be a
    fun
    that accepts all dependencies needed from other modules, or does it also have to import the module to use it...?
    s
    • 2
    • 7
  • p

    poohbar

    05/02/2018, 6:12 PM
    Do I have to use interfaces for my services? I strongly disagree with this line from the Kodein docs:
    In DI, each business unit will have dependencies. Those dependencies should (nearly almost) always be interfaces.
    So I am wondering if Kodein is a good choice for me if I don't have silly interfaces for every single dependency.
    s
    d
    a
    • 4
    • 3
  • l

    leosan

    05/03/2018, 11:37 AM
    Kodein 4.1 What am I doing wrong/forgetting here? I have a concrete class
    class Foo(view:ViewFoo)
    And a Kodein.Lazy with this binding
    bind<Foo>() with scopedSingleton(androidFragmentScope) {
                Foo(it)
            }
    and on my `MyFragment(), ViewFoo`I’m retrieving like this
    val foo by kodein.with(this).instance<Foo>()
    But then it throws this error
    No factory found for bind<Foo>() with ? { MyFragment -> ? }
      
    Registered in Kodein:
       bind<Foo>() with scopedSingleton(androidFragmentScope) { Fragment -> Foo }
    a
    • 2
    • 10
  • s

    salomonbrys

    05/09/2018, 8:16 AM
    @hide You need to create a new Kodein instance:
    override val kodein = Kodein.lazy {
        extend(closestKodein())
        import(menuViewModelModule(this@MenuFragment))
    }
    h
    • 2
    • 1
  • k

    kluck

    05/18/2018, 5:45 AM
    I ran into a similar problem when I first discovered about scopes. Here, inside your activity, your kodein scope is not defined, meaning that your
    viewModel: MainViewModel by kodein.instance()
    will try to retrieve an unscoped MainViewModel (and none are defined in your bindings!)
    a
    • 2
    • 1
  • s

    streetsofboston

    05/22/2018, 8:29 PM
    Either use
    val myInstanceOfT by kodin.instance<T>(tag)
    or
    val myInstanceOfT = kodein.direct.instance<T>(tag)
    j
    • 2
    • 2
  • a

    avolkmann

    05/25/2018, 5:55 PM
    Has anyone tried testing a kodein project with travis? My tests run fine (gradle test), but on travis I get null pointers when calling kodein.instance()
    • 1
    • 1
  • b

    bdavisx

    05/27/2018, 10:02 PM
    Is there a way to get an instance if you only have a Java or Kotlin class but can't supply a reified type?
    s
    • 2
    • 1
  • d

    dave08

    05/30/2018, 1:27 PM
    @salomonbrys I have:
    bind<ProgressNotifier>() with factory { id: Int, iconRes: Int, title: String ->
            ProgressNotifierImpl(instance(), id, iconRes, title)
        }
    
        bind() from singleton { InstallationNotifier(factory() as ((Int,Int,String) -> ProgressNotifier)) }
    But idea tells me that type inference failed for
    A
    on
    factory()
    call in the
    InstallationNotifier
    constructor... how could I acheive this? the constructor takes:
    val progressNotifierFactory: (id: Int, iconRes: Int, title: String) -> ProgressNotifier
    a
    s
    • 3
    • 48
  • s

    streetsofboston

    06/12/2018, 12:12 PM
    @salomonbrys Thanks for reporting that issue! Let us know how you work around this issue.
    s
    • 2
    • 2
  • u

    uli

    06/12/2018, 7:45 PM
    Does kodein support injecting platform specific objects into multiplatform common code?
    s
    • 2
    • 9
  • y

    yccheng

    06/21/2018, 4:15 PM
    Hi guys, I posted a question on StackOverflow regarding managing the lifecycle of Kodein context/container. Would be great if you guys have some thoughts on it. Thanks. https://stackoverflow.com/questions/50973181/shutting-down-closing-kodein-context
    s
    • 2
    • 2
  • e

    eygraber

    07/06/2018, 9:21 PM
    @salomonbrys In v4 there was a way to specify a provider for an argument for a scope (https://github.com/Kodein-Framework/Kodein-DI/blob/4.1/shared/erased/kotlin/com/github/salomonbrys/kodein/erased/EInjected.kt#L169). This was helpful when injecting a value from a scope in a class where the scope argument wasn't ready at initialization (e.g. a Fragment that needed something from an Activity scope to be injected). Is there any way to do something similar in v5 with contexts?
    abstract class MyFragment: Fragment, KodeinAware {
      private val parentKodein: Kodein by closestKodein()
    
      override val kodein = Kodein {
      }
    
      // contrived example, but whatever
      // doesn't work because activity is null
      private val presenter: MyActivityPresenter by on(context = activity).instance()
    
      // used to be able to do:
      // private val presenter: MyActivityPresenter by with { activity }.instance()
    }
    s
    • 2
    • 5
  • v

    Vinicius Carvalho

    07/10/2018, 2:18 PM
    Hi all, I'm new to Kodein, and relatively new to Kotlin. I saw one can get all instances by calling
    kodein.allInstances
    , is there a way to find them by name? I'm a 14 yr old Spring user, so I'm kinda used to that 😉
    a
    • 2
    • 1
  • r

    rocketraman

    07/13/2018, 4:10 PM
    Does Kodein have any workarounds for the lack of Kotlin support for SAM conversion on Kotlin interfaces? I want to define a simple function-based injection but the type signature isn't enough to avoid ambiguity, so it seems either tags or an interface is required. I prefer the interface, but with no SAM conversion the binding becomes messy.
    a
    • 2
    • 1
  • j

    judrummer

    07/18/2018, 6:49 PM
    Hi guys! Is Kodein have future plan to verify dependencies graph at compile time?(same as dagger2) or can you suggest any alternative solution to do something like that in Kodein?
    s
    • 2
    • 1
  • a

    Andreas Sinz

    07/19/2018, 7:36 PM
    @judrummer yes, thats fine. Maybe Kodein could provide a method that you can call inside a unit-test that tries to retrieve instances of every bound Type. What would you say @salomonbrys?
    s
    j
    • 3
    • 2
  • j

    Jorge Castillo

    08/17/2018, 5:30 PM
    Hi! I've been working on a Kodein course mostly thought for Android apps and based on nested scopes (+testing). It'll be completely free during a whole week. Please don't hesitate to give feedback. You'll probably like it @salomonbrys. I'd love to get some feedback from you 😊 https://twitter.com/JorgeCastilloPr/status/1030486221091491840?s=19
    s
    • 2
    • 9
  • e

    eygraber

    08/24/2018, 1:36 AM
    @salomonbrys is it not advised to bind function types? I have a binding for
    (Long) -> String
    , but when I inject it, I get a
    (String) -> Request)
    which is obviously not good.
    d
    • 2
    • 1
  • s

    salomonbrys

    10/02/2018, 4:00 PM
    So, here we are. Just in time for KotlinConf, Kodein-DI 5.3.0 has been released 🙂 It features: - A few bugfixes for Android - New Kotlin/Native plugin usage - Non-synced singletons & multitons (http://kodein.org/Kodein-DI/?5.3/core#_non_synced_singleton) Also Kodein-DI now has: - a new logo - a new website : https://kodein.org/di/ Finally: Kodein Koders, the company that I created to develop & support the Kodein Framework is partner at the KotlinConf. Please do come by to say hello if you happen to be in Amsterdam thursday or friday.
    🔥 7
    s
    • 2
    • 1
  • s

    salomonbrys

    10/17/2018, 5:58 PM
    Hi @streetsofboston The pleasure was mine 🙂 So, this really depends on the
    AppSessionScope
    and
    UserSessionScope
    implementation. I suppose they are using
    MultiItemObjectRegistry
    ? There's a
    clean
    method on it, to "clear" the registry, which makes all objects go out of scope. However, this might not be necessary if the registry is only "held" by an object that goes out of scope. GC can take care of that. Using a
    WeakContextScope
    can also be a way to not manually clean the registry.
    s
    • 2
    • 6
  • d

    dave08

    10/25/2018, 4:33 PM
    Is there a way to know if there are unused dependencies in Kodein-DI? It's a big more difficult when everything is
    instance()
    and
    factory()
    in the bindings... it greatly reduces typing but sometimes leaves dangling dependencies...
    r
    a
    • 3
    • 4
  • s

    salomonbrys

    11/12/2018, 5:57 PM
    Hey everybody, I've released version 6.0.0 of Kodein. Note that It is NOT available on JCenter yet, due to sync issues. I've contacted JFrog to work the issue. So, in order to use it, you need to manually add Kodein-DI repository : https://bintray.com/kodein-framework/Kodein-DI. I'll post here once the issue is resolved and Kodein-DI 6.0.0 is in JCenter. So, what's new in this version? - As promised, it is built with Kotlin/Native 1.0.1, so it is fully compatible with Kotlin/Multiplatform projects. - If you want to use Gradle Metadata 0.4 (Gradle 4.8+), you can: use version 6.0.0-LGM in the dedicated repo (will NOT be synced to JCenter, so you always need to add it manually): https://bintray.com/kodein-framework/Kodein-DI-LGM - Scopes were too complicated in Kodein 5.x, this release entirely rethinks the
    Scope
    interface, so it is much (much) more easier to create your own scopes. More in the doc: http://kodein.org/Kodein-DI/?6.0/core#scope-creation Of course, the changelog is here: https://github.com/Kodein-Framework/Kodein-DI/blob/6.0/CHANGELOG.md Also, if you want to see what's in my mind for the future, you can do so here: https://github.com/Kodein-Framework/Kodein-DI/blob/6.0/TODO.md Finally, please note that due to this sync issue with JCenter, I haven't updated master yet. The default documentation is therefore still showing Kodein 5.3. The updated documentation for version 6.0.0 is here: http://kodein.org/Kodein-DI/?6.0
    🎉 12
    d
    • 2
    • 7
  • j

    judrummer

    11/19/2018, 7:55 AM
    @salomonbrys Hi, Is Kodein support multiplatform project already? I didn't see kodein common module in document. Do I need to import all of this (https://github.com/Kodein-Framework/Kodein-DI/tree/master/core) in correct multiplatform sourceSet?
    s
    • 2
    • 3
  • d

    David Fallah

    11/21/2018, 9:21 AM
    Hey, I don't suppose anyone could help me out with this issue: https://stackoverflow.com/questions/53408628/how-can-i-create-di-bindings-for-generic-types-in-kotlin TL;DR: need to be able to register
    Foo<T>
    and resolve
    Foo<Int>
    or
    Foo<String>
    .
    a
    • 2
    • 7
  • i

    igor.wojda

    11/22/2018, 11:06 PM
    Hey I have issue with Kodein versioning. Github newest version is
    5.3.0
    while all docs are referring to
    6.0.0
    .
    5.3.0
    works fine. When I try to use
    6.0.0
    dependencies are resolved properly but some of the stuff is missing eg.
    KodeinAware
    interface. should I stick with
    5.3.0
    or I should use
    6.0.0
    and do something differently? My dependencies
    import "org.kodein.di:kodein-di-generic-jvm:5.3.0"
    import "org.kodein.di:kodein-di-framework-android-x:5.3.0"
    • 1
    • 1
  • i

    igor.wojda

    11/24/2018, 7:00 PM
    I have album detail view witch displays album based on Id passed by intent extra to my Activity. Normally I would inject AlbumDetailsViewModelFactory into Activity, however this time this factory needs additional parameter - id (what is the best way to achieve this? How can we inject this factory into Activity or ViewModel and pass the id?)
    s
    • 2
    • 4
Powered by Linen
Title
i

igor.wojda

11/24/2018, 7:00 PM
I have album detail view witch displays album based on Id passed by intent extra to my Activity. Normally I would inject AlbumDetailsViewModelFactory into Activity, however this time this factory needs additional parameter - id (what is the best way to achieve this? How can we inject this factory into Activity or ViewModel and pass the id?)
s

streetsofboston

11/24/2018, 11:04 PM
Pass the
id
using the
Intent
that starts the Activity. Then when creating an instance of your AlbumDetailsViewModel, provide the
id
by doing
intent.getStringExtra
i

igor.wojda

11/25/2018, 9:01 AM
Let’s me be clear: To obtain VievModel with arguments we have to use custom factory (class extending
ViewModel.Factory
). It would be preferable to inject this factory into activity, however factory requires a bit of configuration (album Id) before it is constructed 🤔
s

streetsofboston

11/25/2018, 5:25 PM
That is true. In our app, we created a extension function on an Activity and Fragment, called
bindViewModel
, that takes a
KodeinAware
as a second receiver, a lambda as a parameter, and uses a custom
ViewModel.Factory
for its implementation. The lambda will return a new
ViewModel
when it is called. This allows for arguments (eg the intent extra) to be provided.
i

igor.wojda

11/26/2018, 9:21 AM
Sounds good @streetsofboston. Can you please share code of these extensions and usage in the Fragment?
View count: 2