Hi all, I tried many approaches but they don’t wor...
# kodein
p
Hi all, I tried many approaches but they don’t work. I try to write simple code with KodeIn and I get:
Copy code
Unable to instantiate activity ComponentInfo{uptop.me.testcoroutine/uptop.me.testcoroutine.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
Activity code:
Copy code
class MainActivity : AppCompatActivity(), KodeinAware {

    override val kodein by closestKodein(context = this)

    private val viewModel: MainViewModel by kodein.instance<MainViewModelImpl>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        viewModel.doSomething()
    }

}
Dependencies:
Copy code
fun appModule(context: Context) = Kodein.Module("appModule") {
    bind<MainInteractor>() with provider { MainInteractorImpl() }
    bind<MainViewModel>() with provider { MainViewModelImpl(instance()) }
}
App:
Copy code
class App: Application(), KodeinAware {
    override val kodein: Kodein by Kodein.lazy {
        import(androidXModule(this@App))
        import(appModule(this@App))
    }
}
Thanks in advance!
r
Hi, on your activity you shouldn’t pass any context into closestKodein() fun
Copy code
override val kodein by closestKodein(context = this)
In your Application declaring your own module doesn’t have to receive any context, or only if you need it in your module declaration.
Copy code
import(appModule())
p
@romainbsl Hello. I resolved that problem yesterday and i didn’t provide any context into modules. About
Copy code
override val kodein by closestKodein(context = this)
I writed that once because i tried different cases. About context, i didn’t find any information that we need to provide any context into modules. And why if it doesn’t need? Maybe i have wrong information, tell me please if i am wrong. My last code is:
Copy code
class MainActivity : AppCompatActivity(), KodeinAware {

    override val kodein: Kodein by closestKodein()

    private val viewModel: MainViewModel by instance<MainViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        viewModel.doSomething()
    }

}
And
Copy code
fun appModule() = Kodein.Module("appModule") {
    bind<MainInteractor>() with provider { MainInteractorImpl() }
    bind<MainViewModel>() with provider { MainViewModelImpl(instance()) }
}
By the way, I tried to write
Copy code
private val viewModel: MainViewModel by instance<MainViewModelImpl>()
but it doesn’t work. I thought, we can provide specific implementation into interface, but how i can see we can’t. Thank you for your help!
r
There is no need to provide context for your modules if you don’t need it. When using our modules like
androidXModule
you need to provide the context as we need it to resolve some android instances.
you should either be injecting your objects like
private val viewModel: MainViewModel by instance()
or
private val viewModel by instance<MainViewModel>()
p
ide tells me that i need to give information about my class in generic. It doesn’t work correctly.
Copy code
private val somethingClass: SomethingClass by instance()
////////////////////////// But this code
Copy code
private val somethingClass by instance<SomethingClass>()
works okay. Thank you for information.
r
for the first case, this is due to an error with the last version of kotlin, but the code compiles
p
Sorry for offtop, but where i can find best practises related with inheritance in kodein or i can import all dependencies in App and it’s enough? I couldn’t find any information about it.
r
you can extend or overrides your container dependencies, dependeing on your usecase https://kodein.org/Kodein-DI/index.html?latest/core#_extension_composition
p
Yeah, i understand but i don’t understand precisely how i can write more correct code. Is it okay if I initialize my dependencies in my activity/fragment so:
Copy code
private val _parentKodein by closestKodein()
    override val kodein by subKodein(_parentKodein) {
        import(activityModule())
    }
Or i can just import everything in kodein global and it’s enough? I asked about that 😃
👍 1
r
It really depends on what dependencies do you need where and when
do you need them to be scoped for a specific activity?
do you need them across your application?
those questions will help you decide where to put your dependencies
👍 1
p
yeah, thanks. You are right. I’ll think about that deeper. Thank you for spending your time and helping to me!