Retrofit in Android Made Easy: Step-by-Step Implementation Guide

Hey everyone, welcome to a new post! Today, I’m going to make Retrofit in Android easier and faster to use. If you’ve worked with Android development before, you’re probably familiar with Retrofit—it’s a popular library …

Retrofit in Android Made Easy

Hey everyone, welcome to a new post! Today, I’m going to make Retrofit in Android easier and faster to use. If you’ve worked with Android development before, you’re probably familiar with Retrofit—it’s a popular library for making API calls in Android apps. In fact, if you have even a little experience in Android development, chances are you’ve already used it. But what makes this post special? Stick around to find out!

Even if you’re already familiar with using Retrofit in Android, this post might offer you some fresh insights. Here, I’ll show you how to automate Retrofit in Android, meaning all the necessary Retrofit code will be generated instantly with just a simple Gradle command. Sounds exciting? Let’s dive right in without any further delay!

Retrofit in Android Made Easy

To make Retrofit in Android simpler and faster, I’ve created a custom Gradle plugin called “Netro”. You can check it out here. Instead of following the traditional, documented approach to setting up Retrofit, this post will show you how to leverage Netro to streamline networking in your Android project. For demonstration, I’ll be using DummyJSON as the RESTful API, but you can easily adapt it to work with your own APIs with minimal adjustments.

Getting the Starter Project

Before you begin, you’ll need to set up the starter project. It comes with everything you need to get started, including:

  • Preconfigured dependencies
  • A fully set up Dependency Injection framework
  • Ready-to-use Jetpack Compose UI components

This ensures we can focus on our topic rather than setting up the basics!

To get the starter project go to the following link.

 

The link above will take you to a GitHub repository where you’ll find two ZIP files:

  • starter-project.zip – This is the file you need to download. It contains the starter project with all the necessary setup.
  • final-project.zip – This is provided as a reference in case you run into any issues while following this tutorial.

However, the goal is for you to build the final project yourself—so hopefully, you won’t need to rely on it!

After downloading starter-project.zip, unzip it and open it in Android Studio. Since the dependencies and setup are already in place, you can explore the project on your own. I won’t be covering basic details like adding dependencies or creating UIs. Instead, let’s get straight to the important part—making an API call.

The APIs

In this post, I’ll walk you through two APIs, and by the end, I’m confident you’ll be able to handle any other API on your own. The first one is the login (auth) API. Below, you’ll find the request body, response, and endpoint URL.

Auth

Endpoint

Request Body

Response

Users

Endpoint

Response

I’ve trimmed down the response as the actual one is quite large. However, you can visit DummyJSON to view the full response if needed.

Automate Retrofit in Android: Generate API Interfaces & Models with Netro Plugin

With everything set up—the starter project in place and the API details at hand—it’s time to dive in and integrate the API seamlessly!

The Netro plugin is already integrated into the project. You can verify this by checking your app-level build.gradle file.

For Netro, we need to create a configuration directory to store API details. I’ll define a directory named “netro_configs” inside the project root, but you can name it whatever you like. However, you must inform the Netro plugin where these config files are located. To do this, add the following block to your app-level build.gradle file.

Here, we specify the path where the config files are stored and set the packageAlias, which determines the package name for the generated files. In the code above, the packageAlias matches your current namespace.

You also need to add the following line inside the android block to ensure your project recognizes the generated code.

Creating YAML Config Files

Once your Gradle setup is complete (which is already done in the starter project), you’ll need to follow the same setup whenever using the Netro plugin. After that, simply define your API configuration manually using YAML inside the directory specified in the netroConfig block of your app-level build.gradle file.

Your config files must follow the naming convention: package_name_config.yaml, which consists of two parts:

  1. package_name – Netro uses this to create a package and organize the generated code inside it. Think of it as the service name. For example, if you’re creating an API for users, you should name the file user_config.yaml.
  2. _config.yaml – This suffix is mandatory. If you omit it, your file will not be processed.

Following this structure ensures proper organization and seamless integration with Netro.

Now, let’s create a config file for the Auth Service. I’ve named it auth_config.yaml following the required naming convention.

YAML Structure Explanation

The config file consists of the following key elements:

  1. baseUrl – Defines the base URL of the API.
  2. endpoints – An array containing API endpoint details. Currently, we have one, but multiple endpoints can be added. Each endpoint includes:
    • name – The generated function will use this name, so choose it carefully.
    • path – The specific endpoint appended to the base URL.
    • method – The HTTP method (e.g., GET, POST, etc.).
    • requestModel (optional) – Defines the request payload. Some APIs, like fetching users, may not require it.
    • responseModel – Specifies the expected response structure.

All requestModel and responseModel definitions must be placed inside the models section. If your config file contains multiple endpoints, ensure all request and response models are defined within this section for proper organization.

Similarly, I’ll create another config file named users_config.yaml to define the user-related API.

Once all your config files are ready, you can generate all the Retrofit interfaces and model classes at once with a single command! 🚀

Generating Retrofit Interfaces and Model Classes with Netro

Auto-Generate Retrofit APIs & Models with Netro

Open terminal and run the following command

After running the command, you should see a “Build Success” message, just like in the screenshot below! 🚀

NetroSync for Retrofit in Android

Once you see “Code generation complete!” in the terminal, it means your code has been successfully generated! 🎉 You can find it in the app/build/generated/netro directory.

netroSync generated files

Awesome, right? 🚀 Now, you can effortlessly use these generated services and models in your project! 🎉

Using Auto-Generated Services & Models with Retrofit in Android

The setup is now complete, and the necessary Retrofit interfaces and models have been generated automatically using the Netro plugin. Now, we just need to consume these generated services in our project. To do this, we’ll start by creating a Retrofit service provider. Since all other configurations, such as dependency injection and required setup, are already present in the starter project, we only need to define an AppModule that will provide all the required services for seamless API integration.

This is a Hilt module that sets up dependency injection for API services. First, it builds a Retrofit client with an OkHttp logging interceptor and a JSON converter. Then, using this Retrofit instance, it provides instances of AuthApiService and UsersApiService, which were automatically generated by the Netro plugin. This setup ensures that the API services are easily accessible throughout the project.

Login Screen

Now, let’s use the generated AuthApiService in our project. If you navigate to the project structure, you’ll find AuthView.kt and AuthViewModel.kt inside the ui/auth package.

Since we’ve already set up AppModule to provide the service instances, we can simply inject AuthApiService into AuthViewModel and start using it as shown below.

Now, let’s update AuthView.kt to integrate authentication. Since this is just a sample, we’re not saving the access token. Instead, we’re handling navigation based on the navigationEvent StateFlow. This event triggers a route change when authentication is successful, navigating the user to the appropriate screen. Simply add the following code inside your Composable to complete the integration. (If you have any confusion, feel free to check the repository for reference.)

Now, if you run the application and log in using the username and password below, you will be navigated to the next screen, which will be empty for now.

You can also check the logs to view the API request details. Congratulations! You’ve successfully consumed the first service generated with the Netro plugin. Exciting, right? Now, let’s move on to the next generated service—UsersApiService.

Users Screen

Just like before, you’ll find the ui/users package, which contains UsersView.kt and UsersViewModel.kt. Now, we’ll modify the ViewModel to consume the generated UsersApiService. Since our module is already set up, we can simply inject the service into the ViewModel. Here’s the updated UsersViewModel code.

Now, in the Composable, just collect the user data and display it using LazyColumn—quick and easy!

Now, just run the application, and you’ll see the list of users. No need to write any Retrofit code—everything was auto-generated! Pretty cool, right?

Final Words

That’s it for this tutorial! I hope you learned something new. If you have any questions or run into any issues, feel free to check the repository or leave a comment. If you found this post helpful, don’t forget to like and share it with others. See you in the next one! I hope this method helps you easily integrate Retrofit in Android projects.

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x