Dagger 2 Android Example using Retrofit
Dagger 2 Android implementation is easier and it is based on Dependency Injection Architecture. Dependency Injection is a design pattern, which is a concept of Object-Oriented Programming, where we don’t create an object of another class inside a class using the new keyword (for Java). Instead, we supply the needed object from the outside. It is a fully static, compile-time dependency injection framework for both Java and Android. It is an adaptation of an earlier version created by Square and now maintained by Google. Note that we are going to implement this project using the Java language.
The Theoretical Part About Dagger 2
- Dependency Provider: Dependencies are the objects that we need to instantiate inside a class. We cannot instantiate a class inside a class. The person who will provide us the objects that are called dependencies is called Dependency Provider. dagger2 is the class that you want to make a Dependency Provider, It needs to annotate it with the @Module annotation.
- Dependency Consumer: Dependency consumer is a class where we need to instantiate the objects. Dagger will provide the dependency, and for this, we just need to annotate the object declaration with @Inject.
- Component: The connection between our dependency provider and dependency consumer is provided via an interface by annotating it with @Component. And the rest of the thing will be done by Dagger.
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Before going to the coding section first you have to do some pre-task
Before going to the coding section first you have to do some pre-task. Go to the app > res > values > colors.xml section and set the colors for your app.
Go to the Gradle Scripts > build.gradle (Module: app) section and import the following dependencies and click the “Sync Now” on the above pop up.
// these two lines are added for retrofit
// these two lines are added for dagger
Go to the app > manifests > AndroidManifests.xml section and allow “Internet Permission“. Below is the code for the AndroidManifests.xml file.
<!– the internet permission , then only api calls can be retrieved –>
<uses-permission android:name=”android.permission.INTERNET” />
Step 3: Designing the UI
Below is the code for the activity_main.xml file. This will be under the app > src > main > res > layout folder. Depends upon different resolutions, sometimes we may need to have it under layout > hdpi or layout > xhdpi folders, etc.
Step 4: Working with the Java file
App Module.java file:
In Retrofit(Retrofit is a REST Client for Java and Android), we need the context object. To supply the objects, so here we will create this module that will give us the Context.
With the dagger, we need to annotate @Singleton when we want a single instance only.
For Retrofit, we need a bunch of things. Cache, Gson, OkHttpClient, and the Retrofit itself. So we will define the providers for these objects here in this module.
- Gson: It is a Java library that can be used to convert Java Objects into their JSON representation.
- Okhttp: works with Retrofit, which is a brilliant API for REST.
Building Component: ApiComponent.java
We will inject in the MainActivity. We also define all the modules using the @Component annotation as we can see in the code.
https://restcountries.eu/rest/v2/all will provide the output as shown in the image. We will take the country name from that
The output of the above REST API URL
As we are taking country names only we will get as a list like Afghanistan,…..India etc., On executing the above code, we can able to get the output as shown below.
You can find the source code: https://github.com/raj123raj/dagger-retrofit