Android Dagger 2 Dependency Injection Tutorial

What is Dependency Injection in Android? Dependency Injection is a practice where objects are designed in a manner where they receive instances of the objects from other pieces of code, instead of constructing them internally. This means that any object implementing the interface which is required by the object can be substituted in without changing the code, which simplifies testing, and improves decoupling. It makes the project more easy to maintain and testable. In this tutorial, I will tell you on how to use android dagger 2 dependency injection in your own project.

For example, consider a Car object.

A Car depends on (uses) wheels, engine, fuel, battery, etc. to run.

Without Dependency Injection (DI):

With Dependency Injection (DI):

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “Dagger2Example”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Add new dependency

Open your Gradle file and add dagger 2 library in the dependency section.

Create ToastUtil class

Create a new class file name “ToastUtil” and Edit this class to the sample source code.

Create Application class

After that also create an application class to init the dagger app component to all activity.

Create an AppModule class

Add another new class for AppModule, this class provides the context and ToastUtil instances to the whole app.

Create an interface

Create a new interface “AppComponent”, this interface is to get all the instances provide by the module class.

Create MainModule class

This class is for MainActivity, it provides the static text to the MainActivity.

Create PerActivity annotation

This annotation is to make sure only perform when the activity is running.

Create a new interface

Create an interface “MainComponent”, this component will inject the mainActivity class, so that you can get the helloText from the MainModule.

Edit MainActivity class

Go to this file and edit this file to the following code. It will display 2 difference toast.

Edit AndroidManifest.xml

Add android:name =”.App” so that your project will run this file on the beginning.

Run Your Project

In conclusion, now you can check it out how the dagger 2 works in this project. Run it on your emulator.

(Android Dagger 2 Dependency Injection)

Source Code

(Visited 701 times, 1 visits today)
Advertisements

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *