Create an Android Library - Androhub

Create an Android Library

Create an Android Library

Android libraries play a vital role in Android application development. We use them in our day-to-day development. Every Android project consists lot of libraries which are nothing but our dependencies. Have you ever thought about how to create an Android library?

Today we will learn to Create an Android library.

What is Android Library?

Android libraries are just like our normal Android app module which includes everything needed to build an app like source code, resource files, an Android manifest, and build.gradle file.

Benefits of Android Library:

  1. Easy to scale the project.
  2. Code maintenance becomes easy as the code becomes independent.
  3. Eliminate code redundancy as the library can be utilized in multiple apps or projects.
  4. Easy to test.

VIDEO TUTORIAL

Create a Library Module

1. Click on File => New => New Module.

Create Library Module

2. On the next window select Android Library as a template and enter your module name as shown in the image and click on Finish.

Enter Module Name

3. When you click on Finish the gradle build will start and add the newly created module to your project.

The below image shows the project structure before creating the library module and you can see there is only an app module.

Structure before creating library module

The below image shows the structure after creating the library module and you can see that the new module is visible ie. networkmodule.

Structure after creating library module

4. The Below image shows the structure of the library module and it is similar to a normal Android app module.

Library Module Structure

Under the hood

Now let’s see what happened under the hood when we created the library module.

1. Firstly the following plugin has been added to your root level build.gradle.kts.

This is how your gradle file will look after adding the plugin:

2. Now navigate to settings.gradle.kts there at the bottom you will see the following code has been added. This will include the module in the project.

Full view of settings.gradle.kts:

3. If you open build.gradle.kts file of networkmodule, you will see a slight difference with the main app module.

  1. com.android.library plugin instead of com.android.application. This tells us that the current module is a library and not the main module.
  2. There is no applicationId and versioning available in the library module. Because there can only be one applicationId in a project and versioning will be followed by the main app module.

Apart from these two changes rest are all the same.

Example

Let’s create a sample project that has Retrofit to handle API calls and we will be printing the base URL in Log. We will be moving Retrofit code to the library module to make it independent and can be used in other projects.

1. Add networkmodule as a dependency to the main app module. To do that just copy and paste the below code and paste it under the dependencies section inside your app-level build.gradle.kts file.

2. Now sync the project and check if the sync is a success. The module will only be a success if we have included the module in settings.gradle.kts as we did earlier. By doing this we can access the classes from the networkmodule.

3. Move code from the main app module to networkmodule by dragging or using copy-paste. In our case, we will be moving RetrofitClient.kt class and cut-paste the Retrofit dependencies in networkmodule gradle file.

4. You can create new files under networkmodule also and can access them in your main app module.

In this way, we can create our own library module and make the project modularized.

In the next article, we will learn how to Publish this library and fetch the same library from a remote repository instead of a local one.

Happy Coding. :)




Post comment

Your email address will not be published. Required fields are marked *