Can't seem to figure out why sometimes I need `pri...
# koin
c
Can't seem to figure out why sometimes I need
private val foo by inject<Foo>(Foo::class._java_)
but sometimes I can have
private val foo by _inject_<Foo>()
anyone know? (sorry koin noob)
a
wrong import?
c
Yeah you should never need the former.
c
Weird. I'm in the same gradle module. so it shouldnt be a missing dependency issue. i tried transplanting the import from one file to another but it can't resolve
inject
ill keep debugging
j
the dispatch receiver (
this
) is correct? if not you aren’t in an activity or fragment, and you must extend from KoinContext (or even better using constructor injection)
c
So for example, private val api: MyApi by inject(MyApi::class.java) is in an android viewModel class MyVM : ViewModel() { private val api: MyApi by inject(MyApi::class.java)
ah. so if I make my ViewModel a KoinComponent then I can use the shortened inject method
j
pass it via constructor
c
k. wonder if it'll "just work"
j
it would work, but it is a bad practice, the only reason to use
inject
is when you are not able to instantiate the classes, if not, they should be injected via parameters not sure if Koin is addressing this issue in Android, but, nowadays, there is no reason to not inject everything via constructors, even in Activities and Fragments except due some legacy limitation
đź’Ż 1
image.png
it should be "trivial" for Hilt or Koin to codegen a custom
AppComponentFactory
which you can put on the manifest
Copy code
<application android:appComponentFactory=".HiltOrKoinCodegenComponentFactory" />
Which should allow to inject everything via constructor
The main limitation can be
Starting from API Level 28
c
thanks. i think my original question is now resolved due to the fact that I didn't know that I should implement the KoinComponent interface. then the short inject method works. Ill try to look up the best practices for koin and AAC VMs
thanks all