How To Improve Coding Time Using Library Project

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

  • Reusability
    Cloning the app is not a good solution because it is not reusable. Once you have created a common module for chatting and published it as a library, anyone and everyone can plug the library in and use it without affecting other code or experiencing package-related issues. It can be reusable as many times as needed.
     
  • Time-Saving
    Is a library time-saving or not? We need to create one standard module for all apps. So, if there are any changes, we need to worry about just one place. If we add new features, all connected apps will be updated automatically.
     
  • Easy Maintenance
    Maintaining one codebase is easier and faster than maintaining multiple apps. It is easy to maintain because we can update or modify the code from one place.
     
  • Easy debugging and Testing
    A library makes debugging and testing easy. We have only one codebase, so we need to test only once.
     
  • Faster build time
    For example, a client wants to develop an app that we divided into four-part modules. On the other hand, we already had a library for three desired modules. So, we need to work on only one module, as others are ready to use with us as a library. This way, we can decrease the app build time.

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:

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:

  • Implementation 'com.google.android.material:material:1.5.0' 
  • implementation 'com.myownlibrary:1.0.0'

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.