https://kotlinlang.org logo
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

    kailan

    02/16/2017, 12:04 PM
    I wrote a library for injecting Kodein bindings into class constructors: https://github.com/kailan/kodeinject
    👍 1
    d
    • 2
    • 4
  • s

    salomonbrys

    07/28/2017, 9:54 AM
    Also, if JetBrains is looking, I've tried to port Kodein to Kotlin Native, but unfortunately, Kotlin Native is not advanced enough to support Kodein 😞 I've documented the needed missing features here: https://github.com/SalomonBrys/Kodein/tree/master/kodein-native . I hope to be able to propose Kodein to Kotlin Native soon 🙂
    b
    o
    • 3
    • 4
  • j

    jdorleans

    09/15/2017, 4:50 PM
    I’m trying to use
    erased
    similar to what’s suggested in the docs:
    erased<Set<String>> will yield a TypeToken representing Set<*>.
    In my case, I’m trying to do like:
    bind<MyClass<*>>() with multiton { p: Obj<T> -> MyClass<T>(p) }
    and
    kodein.with(p).Instance(erased<MyClass<T>>())
    Basically, what I’m trying to do is to create a bind for MyClass regardless the generics T it must receive. So, I’m binding it with
    bind<MyClass<*>>
    . However, it seems that
    erased<MyClass<T>>
    generated `MyClass`instead of
    MyClass<*>
    as mentioned on the docs for the case with
    Set
    . Is there anything I can do to make this right?
    s
    • 2
    • 5
  • d

    dave08

    09/24/2017, 4:50 PM
    I ditched the scopedSingleton, and tried binding the verticle instance straight in and it works, but I still have a mysterious:
    com.github.salomonbrys.kodein.KodeinInjector$UninjectedException: Value has not been injected
    , with no indication to what hasn't been injected...
    • 1
    • 1
  • d

    dave08

    09/26/2017, 1:08 PM
    So far I have:
    inline fun <reified T: Any> Kodein.Builder.singleton(noinline creator: suspend NoArgBindingKodein.() -> T) = SingletonCoroutineBinding(generic(), creator)
    
    class SingletonCoroutineBinding<T : Any>(override val createdType: TypeToken<T>, creator: suspend NoArgBindingKodein.() -> T) : ASingleton<T>(creator) {
    	override fun factoryName() = "singleton"
    }
    But I'm getting that
    init
    is internal in
    ASingleton
    ...?
    • 1
    • 2
  • k

    kristofdho

    10/04/2017, 3:37 PM
    hmm, not as clean as I would have liked. I'll try something with it.
    a
    s
    • 3
    • 5
  • p

    passsy

    10/21/2017, 8:27 PM
    I have only one. multiple Kodein instances results in multiple singleton instances.
    d
    • 2
    • 8
  • i

    ian.shaun.thomas

    10/28/2017, 9:23 PM
    I don't think build times with dagger ever caused me any issues, it's the debugging that sucks. Kodein is massively better in that regard imo.
    d
    • 2
    • 6
  • k

    kristofdho

    10/29/2017, 7:23 PM
    i'm still undecided if i should go with Dagger 2 or Kodein
    d
    • 2
    • 1
  • e

    eygraber

    10/29/2017, 9:31 PM
    https://kotlinlang.slack.com/archives/C0BLU9K96/p1509295265000003 @ian.shaun.thomas it's not as much convincing them to use it as it is correcting misconceptions. I don't think it's helpful to leave stuff like that unanswered in public. Personally I think Dagger2 is great, but it's insulting to @salomonbrys and all the folks who put in hard work to see people dumping on Kodein.
    i
    • 2
    • 1
  • p

    passsy

    11/15/2017, 10:55 AM
    Sometimes this happens on some machines. It’s unpredictable
    • 1
    • 1
  • i

    ian.shaun.thomas

    01/10/2018, 1:37 PM
    if it works for you @rocketraman 👍 but daggers generally useless errors are why I'm done using it. I would rather type more code or do anything else if it means getting useful errors. The other major smell is almost every project I have seen in the real world using dagger is using it wrong. They almost always have a god class for the injections (all definitions in one module) and in multiple places they have some work around because they couldn't figure out how to get dagger to do what they needed. Not saying dagger is bad just that I have seen the same issues from multiple teams of devs so that seems like a pattern to me that the library isn't very good.
    r
    • 2
    • 1
  • m

    mariodroid

    01/16/2018, 7:05 AM
    Hi guys simple question, when I import the android module how can I retrieve, for instance, a
    SharedPreferences
    ?
    bind<Foo>() with provider {Foo(instance()) }
    where my
    Foo
    class requires in the constracture shared preferences I add the module in this way:
    kodein.addConfig {
                import(androidModule)
                BaseAppComponent()
            }
    a
    • 2
    • 18
  • d

    dimsuz

    01/26/2018, 10:49 AM
    I also figured a particular
    bind
    which causes this, but I do not understand what other
    Object
    does it override. If someone could advise how to debug this, will be helpful
    a
    • 2
    • 4
  • n

    neworldlt

    01/31/2018, 1:02 PM
    Are someone of you using kodein on real project with large number of binds? How it performs compared to Dagger, which has statically compiled code? I am planning to have ~300-500 bindings on ~10 modules and injecting about 5000 times/second on single core.
    i
    • 2
    • 5
  • m

    Marat Iskhakov

    02/01/2018, 9:09 AM
    Hi, I'm trying to declare autoActivityScope in my kodein file. I've registered necessary lifecycle callbacks in Application class. But when I try to retrieve dependency by KodeinInjector, throws exception:
    Caused by: com.github.salomonbrys.kodein.Kodein$NotFoundException: No provider found for bind<MyModel>() with ? { ? }
    Is my kodein file with bindings should be in Application class and Application class must implement KodeinAware? I'll appreciate for some help. Thanks.
    • 1
    • 1
  • s

    salomonbrys

    02/06/2018, 5:30 PM
    @neworldlt The reasoning behind the copy during extension is explained in the third comment of this issue : https://github.com/SalomonBrys/Kodein/issues/100
    n
    • 2
    • 3
  • d

    david.pacheco

    02/07/2018, 10:58 AM
    @salomonbrys thanks, I assumed that since I was using the
    androidContextScope
    it would provide the context. However, I tried your suggestion and now I get
    com.github.salomonbrys.kodein.Kodein$NotFoundException: No factory found for bind<PersistenceService>() with ? { Object -> ? }
    s
    • 2
    • 4
  • a

    allanhasegawa

    02/15/2018, 6:08 PM
    Hey guys, had a few issues injecting ViewModel into my Activity, so decided to implement my own thing. I published it here: https://github.com/AllanHasegawa/kodein-viewmodel-injector
    👍 1
    k
    • 2
    • 2
  • j

    Joe

    02/16/2018, 4:10 PM
    Can you use destructured declarations, something like this (but not this since it doesn’t compile 🙂):
    bind<OkHttpClient>() with factory { (socketFactory, standardHeaderInterceptor) ->
                OkHttpClient
                    .Builder()
                    .socketFactory(socketFactory)
                    .addInterceptor(standardHeaderInterceptor)
                    .addInterceptor(instance<ICrashlyticsLoggingInterceptor>())
                    .addInterceptor(instance<HttpLoggingInterceptor>())
                    .build()
            }
  • a

    apatrida

    02/19/2018, 7:24 PM
    @salomonbrys was gonna update to Kodein 4.x for my dependent libraries, but some breaking changes are there: https://github.com/SalomonBrys/Kodein/issues/113
    s
    • 2
    • 15
  • j

    Joe

    02/26/2018, 3:52 PM
    I haven’t looked much at Scopes, so I’ll have to check that out. Thanks!
    s
    • 2
    • 6
  • n

    neworldlt

    02/26/2018, 4:34 PM
    I am just wondering, what is a case for clearing singleton on every request?
    j
    s
    • 3
    • 5
  • h

    hamutarto

    03/04/2018, 2:13 PM
    hey guys! I started to get "Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.github.salomonbrys.kodein.KodeinAware" exception in my application, however I'm using kodein for a while now and without any issue. Do you have any idea what that might be? I haven't touched any of the DI part of the application
    s
    • 2
    • 2
  • n

    neworldlt

    03/08/2018, 3:45 PM
    it works on my machine 😕
    s
    • 2
    • 6
  • h

    hamutarto

    03/11/2018, 1:23 PM
    any idea?:)
    a
    • 2
    • 42
  • l

    leosan

    03/13/2018, 4:10 PM
    I partially solved my problem doing this
    bind<Job>(MyJob.TAG) with singleton { MyJob(instance()) }
    bind<Job>(MyJob2.TAG) with singleton { MyJob2() }
    class JobCreator(private val kodein: Kodein) : JobCreator {
        override fun create(tag: String): Job? {
            return kodein.instance(tag)
        }
    }
    But I wonder if there’s a way to create this dependency in a transitive way without passing the kodein instance to the job creator…
    a
    • 2
    • 22
  • m

    mate.herber

    03/13/2018, 10:28 PM
    I'm using 5 beta, trying to make retainedKodein work but it crashes for me:
    Caused by: java.lang.IllegalStateException: Fragment org.kodein.android.KodeinFragment must be a public static class to be  properly recreated from instance state.
            at android.app.BackStackRecord.doAddOp(BackStackRecord.java:429)
            at android.app.BackStackRecord.add(BackStackRecord.java:409)
            at org.kodein.android.RetainedKt$retainedKodein$1.invoke(retained.kt:26)
            at org.kodein.android.RetainedKt$retainedKodein$1.invoke(Unknown Source:0)
            at kotlin.SynchronizedLazyImpl.getValue(Lazy.kt:131)
    ...
    s
    s
    • 3
    • 18
  • e

    edwardwongtl

    03/26/2018, 5:23 AM
    Is there a way to bind generic types like
    bind<Subject<*> with singleton { ... }
    and get
    val subject by instance<Subject<String>>
    ?
    a
    • 2
    • 2
  • a

    apatrida

    04/01/2018, 1:21 AM
    @salomonbrys I'm not a huge fan of this
    direct
    thing, making a bunch of my injection via default constructor parameters really long for no added value.
    s
    s
    • 3
    • 5
Powered by Linen
Title
a

apatrida

04/01/2018, 1:21 AM
@salomonbrys I'm not a huge fan of this
direct
thing, making a bunch of my injection via default constructor parameters really long for no added value.
s

streetsofboston

04/01/2018, 11:16 PM
I think Kodein is trying to guides us to provide a
KodeinAware
to our constructors (and maybe making our constructor's classes
KodeinAware
as well). Then you can use
by kodein.instance()
or
by instance()
to define class' properties.
a

apatrida

04/03/2018, 4:07 AM
Well, our model differs. We use constructor default parameters because it allows or dependencies to be declarative, classes to be used without an injection module, and still have full dependency injection. We only inject two things from inside the class itself and that is logging and typically Jackson which most of our code uses in some place or another. We don't want our dependencies hidden inside the class, we don't want to create a model in ever test case , and we surely don't want long
kodein.direct.instance()
in the middle of everything, including the bindings themselves which now get hit by this. Sure, we can create a new extension function to put this method back on top, .... we'll work around it.
s

salomonbrys

04/03/2018, 9:07 AM
@apatrida You can use a
DKodein
in the entire application and never have to use
direct
, simply create the Kodein object as such:
val kodein = Kodein.direct { /* bindings */ }
Or, if you use injection via constructor parameter, I'd encourage you to use this syntax:
val myValue = kodein.newInstance { Whatever(instance(), instance()) }
a

apatrida

04/03/2018, 5:42 PM
thanks @salomonbrys, I'm less grumpy today so this helps!
View count: 3