Introduction
Hi friends, In this article, we will learn one of the best concepts in Android. When we work on large projects such as e-commerce, social media, etc., we have to make sure several things, such as the project has good architecture so that it can be scalable, memory has been utilized, and many other things. Dagger2 dependency injection and RxJava play a vital role in making your Android project scalable and utilizing memory.

Prerequisites
- Basic knowledge of Android
- Basic knowledge of Java
- Having the latest version of Android Studio
What is Dagger2 Dependency Injection?
Dagger 2 is a popular dependency injection framework for Android that helps manage the creation and dependency injection of objects in a clean and efficient way. Dependency injection is a design pattern used to improve code modularity and maintainability by reducing the coupling between components of an application. It helps you manage dependencies, improve code quality, enhance maintainability, and optimize performance in your Android applications. This makes it easier to understand, maintain, and extend your codebase.

What is RxJava in Android?
RxJava is a popular library for asynchronous and event-driven programming in Android. RxJava is commonly used for managing network requests and responses. Libraries like Retrofit integrate well with RxJava, enabling you to perform HTTP requests in a reactive and concise manner. RxJava simplifies handling and composing complex asynchronous operations, making it highly useful in Android development. RxJava allows you to easily cache and memoize results, reducing redundant work and improving app performance.
Let's Start With Android Studio
Step 1. Create a new project and select the empty activity.
Step 2. Add the required dependencies in the build.gradle
// Retrofit and Gson for API calls
implementation ("com.squareup.retrofit2:retrofit:2.9.0")
implementation ("com.squareup.retrofit2:converter-gson:2.9.0")
implementation ("com.google.code.gson:gson:2.8.9")
implementation ("com.squareup.okhttp3:okhttp:4.3.0")
implementation ("com.squareup.okhttp3:logging-interceptor:4.3.0")
implementation ("com.squareup.retrofit2:converter-scalars:2.9.0")
implementation ("com.squareup.retrofit2:adapter-rxjava2:2.4.0")
// Dagger 2
implementation ("com.google.dagger:dagger:2.37")
annotationProcessor ("com.google.dagger:dagger-compiler:2.37")
// RxJava
implementation ("io.reactivex.rxjava2:rxjava:2.2.21")
implementation ("io.reactivex.rxjava2:rxandroid:2.1.1")
We have to add these dependencies to our Android project to make it easier to work with network requests, JSON data, dependency injection, and asynchronous programming.
Step 3. Create a network package, and inside this, create a retrofit service interface named ApiService. java, where we define our API endpoints.
package com.example.apicallusingdagger2andrxjavaexample.network;
import com.example.apicallusingdagger2andrxjavaexample.model.ResponseModel;
import java.util.List;
import io.reactivex.Observable;
import retrofit2.Response;
import retrofit2.http.GET;
public interface ApiService {
String BASE_URL="https://cat-fact.herokuapp.com/";
@GET("facts")
Observable<Response<List<ResponseModel>>> fetchData();
}
This interface is used for making network requests to a specific API. It uses Retrofit and RxJava to handle the asynchronous and reactive nature of network calls.
Step 4. Inside the network package, create a new class named NetworkModule.java. This class is part of a Dagger 2 setup used in Android for managing network-related dependencies, particularly for making API calls.
package com.example.apicallusingdagger2andrxjavaexample.network;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
@Module
public class NetworkModule {
@Provides
@Singleton
OkHttpClient provideOkHttpClient() {
// Create an OkHttpClient with a logging interceptor
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
// You can also add other interceptors, headers, timeouts, etc., here
.build();
}
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.baseUrl(ApiService.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create()) // For access to the response body as a string
.client(okHttpClient) // Set the OkHttpClient instance
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
}
This Dagger module provides a pre-configured OkHttpClient and a Retrofit instance that's ready to make network requests to a specific API. It's set up to log network interactions, use GSON for JSON parsing, and support RxJava for handling asynchronous data streams in an Android app. The @Singleton annotation ensures that only one instance of these objects is created and reused throughout the app.
Step 5. Create a new package named model, and inside this, create your ResponseModel.java class
package com.example.apicallusingdagger2andrxjavaexample.model;
import com.google.gson.annotations.SerializedName;
public class ResponseModel {
@SerializedName("source")
String source;
}
This model class is used to parse JSON into the model class.
Step 6. Create a new package named di, and inside this, we have configured our dagger2 setup, so we need to create a class named MyApp.java
package com.example.apicallusingdagger2andrxjavaexample.di;
import android.app.Application;
import com.example.apicallusingdagger2andrxjavaexample.network.NetworkModule;
public class MyApp extends Application {
private static AppComponent appComponent;
@Override
public void onCreate() {
super.onCreate();
appComponent = DaggerAppComponent.builder()
.networkModule(new NetworkModule())
.apiModule(new ApiModule())
.build();
}
public static AppComponent getAppComponent() {
return appComponent;
}
}
MyApp is a class that extends, which is a fundamental class in Android apps. It's a good place to initialize things when your app starts up.


Join the conversation! Your thoughts help the community grow.