This tutorial lets you write an Android Java application and use Koin inject and retrieve your components.
Get the code
Add the Koin Android dependency like below:
Our components (Java & Kotlin)
Let's create a HelloRepository to provide some data:
Let's create a Java presenter class, for consuming this data:
Writing the Koin module
module function to declare a module. Let's declare our first component:
we declare our MySimplePresenter class as
factory to have a create a new instance each time our Activity will need one.
Now that we have a module, let's start it with Koin. Open your application class, or make one (don't forget to declare it in your manifest.xml). Just call the
Injecting dependencies into Java Activity
MyJavaPresenter component will be created with
HelloRepository instance. To get it into our Activity, let's inject it with the static
inject()function allows us to retrieve a lazy Koin instances, in Android components runtime (Activity, fragment, Service...)
get()function is here to retrieve directly an instance (non lazy)