A library is nothing but a set of files that can be used in other projects. A library cannot be directly runnable. We can consider Facebook SDK or Google SDK as a library.

Why do we need a library?

This question arises in everyone's mind, because no one wants to waste his/her time developing something that is never useful. Is a library really a waste of time, or is it an investment for the future?

Let me give you a real example that I have seen. We had a client for whom we developed one application (ex. Chat app). The application was successful, and the client was happy with our work. Now, the client needs another chat app with only a color change, or we can say that another app needs a chat function within.

Now we have two options.

Option 1

We can make standard source code for both apps (which works as a library).

Option 2

We can just copy and paste the chat functionality into another app.

We can use either approach for this app because, at this time, it doesn't have any effect on efficiency.

Now let's see the future; after the success of the second app, the client wants the third app with minor updates. We can again clone the app so it does the same thing. Every clone and update consumes time and impacts the efficiency of experts. But, let's see how much it can be flexible through the use of a library.

Benefits of library

How does the Library Work?

Library Module > App Module > App

We can create a library as another module in the same project, or publish our library separately and import it into our project. We need to pass a few parameters if the library requires them, and we are ready to go. It sounds simple, right? But, it is not as simple as it sounds. It takes time to set up a library.

"SharedPreference” wrapper in Android or "DateTimeFormatter" library is commonly helpful in almost every app.

Step to Create Library Module for Android

Creating a library is like creating a new project. One can create a new module from File > New > New Module.

library module

Check out this image that will help you create a new module as a library module.

library module menu

Now what we have to do is write code inside the library module. Just for the demo, create “Helper.kt” object file. write a function that will log details:

JavaScript
fun writeLog(logText: String) {
    var modifiedLogText = "$currentTime - $logText"
    if (debugBuild) {
        Log.d("AppLog", modifiedLogText);
    } else {
        writeLogInFile(modifiedLogText);
    }
}

Now it is time to use it in a project. Add module dependency to the current project module, and we are ready with our library, which will log text in our format. This module is easy to copy and paste into any project. Or, we can publish this library and use it just like a material library.

Here is an example of single-line code that we can use to import the library once it is available on some common platforms like GitHub:

Conclusion

It is better to work once and use the outcome each time required, rather than cloning every time. We can save time for development by creating a library module or creating a utils file. Never copy and paste inside the same project or similar project. Always look for space where you can put the code and use it repeatedly.