Skip to content

Repository files navigation

AndroidDaggerSample

Simple example showing how to implement dagger for dependencies injection

Getting Started

The code has three packages

  • di (for dependecy injection modules and Components)
  • model (Contains my UserModel which i intend to inject to my activity)
  • view (Contains Android Activity Class)
  • viewmodel (to hold logic of my Activity class)

MyApp Class (Creates the dagger component and exposes it through public method)

Add Dagger 2 to your build.gradle

Open your app gradle file and add to your dependencies

implementation 'com.google.dagger:dagger:2.15'
annotationProcessor 'com.google.dagger:dagger-compiler:2.15'

Dagger Annotations

  • @Module - Defines a class that provides dependency and the annotation is placed on top of the class.
  • @Provides - Tells dagger that the dependency is provided by given function.
@ModulepublicclassUserModule {
@ProvidesUserprovidesUser() {
returnnewUser();
}
}
  • @Component - An interface contain methods that specify to Dagger where to inject the dependencies. It is added on top of the class and includes modules providing the dependencies.
@Component(modules = UserModule.class)
publicinterfaceUserComponent {
voidinject(MainActivitymainActivity);
}
  • @Inject - Used to request for dependency in classes which requires to use them.

Creating DaggerComponent

Once the module and Component has been created, build the app and dagger will automatically generate DaggerComponent that you will use to initialize you component.

UserComponentuserComponent = DaggerUserComponent.builder()
.userModule(userModule)
.build();

Injecting Your Dependencies into the class

For Easier implementation, create a class extending Application class and define your component which you initialize on its onCreate Create a public method to provide the component You can also create a public static method to give you the instance of the class such that you will not have to create its object from your activity but instead call it directly

publicclassMyAppextendsApplication {
privatestaticMyAppapp;
privateUserModuleuserModule;
privateUserComponentuserComponent;
@OverridepublicvoidonCreate() {
super.onCreate();
app = this;
userModule = newUserModule();
userComponent = DaggerUserComponent.builder()
.userModule(userModule)
.build();
}
publicstaticMyAppapp() {
returnapp;
}
publicUserComponentuserComponent() {
returnuserComponent;
}
}

Add the class to your manifest

 android:name=".MyApp"

On your Activity's oncreate, call the component and access it method specifying injection target and give it the activity context use @Inject to request for your dependencies provided by the module

@InjectUsermUser;
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
MyApp.app().userComponent().inject(this);
}

About

Simple example showing how to implement dagger for dependencies injection

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages