Implementing Dagger HILT in Android Login Activity with Kotlin — Part 2: Retrofit 2 Integration

Arif Khan
3 min readAug 10, 2020

In Part 1: Implementing Dagger HILT in Android Login Activity with Kotlin we learned about How to integrate and use Dagger Hilt in our application. In this part, as promised, we will learn how we can integrate Retrofit 2 using dagger HILT in our application.

Prerequisites: You must be aware of Retrofit 2 and Moshi JSON converter libraries.

Step 1: Add the following libraries to your project by writing the following code into your project’s build.gradle (app level)

  1. Retrofit 2: It is a type-safe HTTP client for Android and Java.
  2. Moshi: It is a modern JSON library for Android and Java. It makes it easy to parse JSON into Java objects:

Hit Sync Now!

Step 2: Create a Network Module in the modules package.

NetworkModule.kt

Write the following code in this file:

Note: Maybe you will get an error if you try to build your project at this point add the following lines to app-level build.gradle file

android {

...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

Here in NetworkModule “provideRetrofitClient “ is a method that is responsible for creating a RetrofitClient. And by declaring the argument in its definition it is asking Dagger HILT for dependencies like okHttp and MoshiConverterFactory Instance, which is again provided by Dagger HILT with the help of other member functions of this object. And you can observe the similar approach followed in the other methods of this Kotlin object.

Till now we have integrated Retrofit 2 in our project successfully. Run your application it should be working fine up to this point.

Now we will consume the retrofit dependency in our login API.

Step 3: Create a new package called “network”.

Network package

Step 4: Create a Kotlin interface UserServices in the network package. And paste the following code inside it.

Note: Your code must be unhappy at this point because we haven't created LoginRequest yet. Don’t worry that is our next step.

Step 5: Create a new package requests inside the network package and create a new data class LoginRequest inside it.

LoginRequest.kt
data class LoginRequest(val email: String, val password: String)

Now import this class inside UserServices.kt to fix the error.

LoginRequest.kt is just a simple Kotlin data class and we are using it to structure the login request i.e. before calling the Login API the data must be an instance of LoginRequest class.

Step 6: Create a service builder that will create a working retrofit client to call different APIs. Create a class called ServiceBuilder inside the network package and write the following code in it:

Step 7: Create a Kotlin object ServicesModule inside the modules package and write the following code in it:

Note: Import required classes.

Step 8: Update DataSrouceModule declare the dependency the UserServices in the providesLoginDataSource method:

@Provides
@Singleton
fun providesLoginDataSource(userServices: UserServices) = LoginDataSource(userServices)

Step 9: Update the LoginDataSource.kt file to consume UserServices.

Now run your app it should be working fine.

The UserServices Instance is now available inside our LoginDataSource class now we can call the login inside the login function of this class.

You can find the source code of this part on Git Hub.

What’s Next?

We will integrate Login API and handle the response.

I hope you enjoyed the article and learned something useful.

Thanks for reading.

--

--

Arif Khan

My specialties include quickly learning new skills and programming languages, problem-solving. Besides, I love circuit training 🤸 and weight lifting 🏋️