How to add Retrofit library in Android Studio

Congratulations! Your search for “How to add Retrofit library to your Android Studio project” has landed you in the perfect place. This blog will be looking in detail at the same starting with some formal definitions required during the how-to and then finally moving on to the procedure itself. However, in case you are already familiar with the terms, feel free to skip to the last part of the blog. Now let’s get started without further ado.

What is the Android Studio?

Android Studio is the official Integrated Development Environment (IDE) for Android app development, based on IntelliJ IDEA. On top of IntelliJ’s powerful code editor and developer tools, Android Studio offers even more features that enhance your productivity when building Android apps.

Android Studio uses a Gradle-based build system, emulator, code templates, and Github integration to support application development within the Android operating system. Every project in Android Studio has one or more modalities with source code and resource files. These modalities include Android app modules, Library modules, and Google App Engine modules. Hence, libraries make up an integral part of an Android Studio project.

What is an external library file in Android Studio?

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest. But, instead of compiling into an APK that runs on a device, an Android library compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module.

We can use two types of libraries in our Android Studio project:

  • Android Library Module: Android Library Module compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module. It allows you to add Android-specific components like resources and manifest files, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods.
  • Java Library: Java library builds a Java ARchive file (JAR file). A JAR file is useful for many projects especially when you want to share code with other platforms. But it does not allow you to include Android resources or manifest files, which is very useful for code reuse in Android projects. So when you do not need any android specific resources in a library you should create a java library.

Benefits of using external libraries in your Android Studio project

Using external libraries in an Android Studio project makes things easier. The following are some of the advantages of doing so:

  • When you’re building multiple apps that use some of the same components, such as activities, services, or UI layouts, you can simply copy-paste the same libraries in your multiple apps.
  • When you are using a service whose code has already been written, you can simply add its library as opposed to writing the entire code by yourself.
  • When you’re building an app that exists in multiple APK variations, such as a free and paid version and you need the same core components in both, you can simply add the same libraries for both.

What is the Retrofit library and what does it do?

Retrofit is a type-safe REST client for Android, Java and Kotlin developed by Square. The library provides a powerful framework for authenticating and interacting with APIs and sending network requests with OkHttp. This library makes downloading JSON or XML data from a web API fairly straightforward. Once the data is downloaded then it is parsed into a Plain Old Java Object (POJO) which must be defined for each “resource” in the response. In a nutshell, it is a very easy to use and fast library to retrieve and upload the data via Rest based web service.

How to add the Retrofit library to your Android project?

Now that we have an idea of what Retrofit is and what can it do, let’s have a look at how can we add it to our Android Studio project. We will be looking at two ways of doing so. The Retrofit library can be added as a simple Gradle dependency or as an Android library. Although adding it as a Gradle dependency is far more quick and efficient, we shall look at both the methods.

Adding the Retrofit library as a Gradle dependency

Ever since Retrofit was officially made available on the JCenter, adding it to an Android project has become quite easy. Here’s how to do so:

  • Open the Android Studio project where you want to add the Retrofit library. In case you want to create a new project, create one by selecting Empty Activity.
  • Once you are inside your Android Studio project, navigate to your build.gradle file of your app.
  • To navigate to your app’s build.gradle file, make sure that your project view is set to Android and go to

    Gradle Scripts->build.gradle(Module:YourProjectName.app).

    Navigate to build.gradle file of your app

  • Add the following dependency:

    implementation ‘com.squareup.retrofit2:retrofit:2.9.0’

  • Sync your Gradle project after making the changes.
  • As Retrofit uses internet permission, you need to declare it in your manifest file to use Retrofit.
  • Navigate to your AndroidManifest.xml file in app->manifests->AndroidManifest.xml.

    navigate to AndroidManifest.xml file

  • Add the following permission:

    <uses-permission android:name=”android.permission.INTERNET” />

  • You have now successfully added the Retrofit library to your Android project.

Adding Retrofit as a library by copying it to the libs folder

In this method, we will be adding the Retrofit external library by copying and pasting the JAR file into the libs folder.

How to:

  • The first step is to of course open the Android Studio project that you are working on. If you want to create a new project, you can do so by selecting an Empty Activity and naming your project.
  • Once you are in your project, change the project view from Android to Project Files.

    Change project view to Project Files

  • Copy the JAR file of the Retrofit library by downloading it from here.
  • Once you have copied the JAR file, navigate to the libs folder by going to app->libs in the Project Files view.

    Navigate to libs folder

  • Right-click on the libs folder and hit Paste.

    Select Paste

  • Right-click on the pasted JAR file and select Add As Library… from the drop-down menu.

    Choose Add As Library

  • You have now successfully added the Retrofit library to your Android Studio project.
  • You can change back the project view to Android and navigate to your build.gradle file to see that your external library is now included in it.

    The library will be added

  • It’s that simple.

In conclusion, adding the Retrofit library to your project helps you make networking calls faster and easier and now we know two of the easiest ways of doing so. Hope you try them out.