Dependency injection in AndroidGabriel TannerBlockedUnblockFollowFollowingMar 3Photo by Luca Bravo on UnsplashToday, we are going to discuss in-depth about dependency injection in Android and why you should be using it.
Definitions and advantagesDependency injection is a technique where objects are created by an external entity or object.
This means an object doesn’t have to create the instances of its dependent classes on his own but rather gets the dependencies from another object or static method.
This technique has many benefits here I’m just listing the most important ones:Reduces the boilerplate codeMakes our code reusable and cleanMakes it easy to replace our dependencies with fake implementations which make testing easierHelps us enable loose couplingNow that we know about the basic concepts of dependency injection we will take a look at the different solutions and frameworks for dependency injection in Android.
KodeinKodein is a very simple and yet very useful dependency framework which is easy to configure and use.
It’s easy to learn and provides some great benefits, with the most important being:It proposes a very simple and readable declarative DSLIt handles dependency initialization orderIt easily binds classes and interfaces to their instance or providerDeclaring Bindings:To make our dependencies accessible we need to make our class Kodein aware by implementing the KodeinAware interface.
After that, we can define bindings using Kodeins dead simple DSL.
Here we bind an example dice class using the singleton bind function.
Retrieval:After that, we can start getting our dependency by making our activity KodeinAware.
Then we just need to override the kodein variable and get our dependency by instance.
In this example, we use closestKodein which returns the Kodein of the closest parent layer.
Create modules:Now let’s look at how we can split our bindings into modules.
Here we create two different modules with dependencies.
After that, we can import the modules we want to use in our Kodein container.
Bind Functions:Factory:The factory bind function allows you to pass an argument of a certain type and return an instance of another type.
A factory can take up to five arguments.
Here we create a dice with the Int representing the number of sides.
Provider:The provider function takes no argument and returns the bound type.
It will be called every time you need an instance of the bound type.
Here we create a dice with a fixed number of sides.
KoinKoin is a lightweight but still powerful dependency injection framework for Kotlin developers which is purely written in Kotlin.
Here are some of its benefits:Provides an easy to understand DSLSupports Android Architecture ViewModelHas built-in testing features using JUnitDeclaring Bindings in a module:First, we need to define a module using the module keyword.
After that, we can declare our binding using a binding function.
Here we bind a repository using the singleton pattern to make sure it only gets instantiated once.
After the first request, koin will just return the same instance over and over again.
We also bind a presenter using the factory pattern which creates a new instance on each request.
Start Koin:Before we can start retrieving our bindings we first need to start Koin in our application class.
Also, don’t forget to set the name to your application class in your AndroidManifest file.
Retrieval:Now we can easily get our dependencies.
Here we are using a lazy inject to get our presenter dependency.
We can also directly inject our dependency using the get keyword.
Testing:Koin provides test functionality which allows you to easily inject dependencies in your JUnit test.
To use Koin in your Unit-test you just need to tag your class with KotlinTest.
Here we inject two instances of a fake presenter and check if they are not the same instance.
In the test above we use the checkModules function to check if all definitions are bounded.
ConclusionNow you should know how the dependency injection works and why it’s important to use it.
We also looked at two big dependency injection frameworks and why they are a great choice for dependency injection in Kotlin.
If you have found this useful, please consider recommending and sharing it with other fellow developers.
If you have any questions or critics, you can reach me in the comment section.
.. More details