Android dagger 2 simple example

Dagger 2 is very good library which is used to implement dependency injection in android. You can find many tutorials on dagger 2. Most of them will be complex implemenation in which it will be very hard to understand the concept. In this tutorial i will explain the concept with a simple example. Please let me know your feedback via comments if this helped you.

For Dagger 2 implementation , first you have to understand what is dependecy injection.

what is dependency injection?

In simple it is providing an instance of a java class to another class.

Usage of dependecny injection

Suppose we have a data fetching class and want to use it in two different activities, You can just create one instance and inject it into diffrent activities. In simple you can reuse the instace of class across activities.

What is Dagger 2?

Dagger 2 is a dependency injection framework which generates the codes based on annotations.

Annotations Used

The important annotations used for dagger 2 dependency injection are

  • @Modules – Class annotated with @Modules are responsible for providing the methods required for dependecny injection
  • @Provides – Use in the method which has to be injected.
  • @inject -Used for requesting the dependency
  • @Component – Enable modules for dependency injection
  • @Singleton – This annotation will help to make single only single instance of proivded object and shared.

Example for understanding the concept

Now let us try implementing a simple example to understand the concept of dagger 2.


  • Add the required libraries to gradle file.
  • Create a simple class with a function with a return value.
  • Implement @Module class with @provides methods.
  • Create component interface for injection.
  • Inject in your activity.

Add required libraries to gradle file

The first step is to add the required libraries for dagger 2 implemetation.

implementation ''
implementation ''
annotationProcessor ''
annotationProcessor ''

Create a simple class

In this step let us have a simple class with a function returning one string. We will create the instance of this class in the class which we are going to inject.

public class MyDataClass {

    public String GetName(){

        return "CodesFor";


Implement @Module class with @provides methods

We already learned the purpose of module and provides annotation. Here in this step we will create a module class and a provides method .The provide method will return the instace of the above created class,

import javax.inject.Inject;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;

public class MyDagger {

    MyDataClass myDataClass;

    public MyDagger(){


    public MyDataClass MyDaggerFunction(){

        myDataClass = new MyDataClass();
        return myDataClass;

Create component interface for injection.

This is an important step in dagger implementation. Here we will create an interface with component annotation which will enable the modules required for injection.

import javax.inject.Singleton;
import dagger.Component;

@Component (modules = {MyDagger.class})
public interface DataComponent {

    void inject(SimpleDaggerActivity activity);

Inject to activity

If you have created a new project, an activity might be automatically created. If not create a new activity. Now in this final step we will inject our module class and use the provided method in our activity.

public class SimpleDaggerActivity extends AppCompatActivity {

    DataComponent component;
    MyDagger myDagger;

    protected void onCreate(Bundle savedInstanceState) {

        component = DaggerDataComponent.builder().build();

        //using provided function
        String output = myDagger.MyDaggerFunction().GetName();


Now let us just conclude what we impleneted. In the first step we synced the gradle with necessary libraries. After syncing the libray we created a class with a function returning a string. In your project this can be a repository class which will fetch data and has to be reused across acitivity. Next we created a module class with a provided function . The provided function will return the instance of the above created class. In final step we injected module class in our activity and accessed provided functions which is an object of data class which finally get access to our data class function.

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

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