Android Library Creation and Usage

Sneha Bhat
YML Innovation Lab
Published in
5 min readMay 17, 2022
Photo by Iñaki del Olmo on Unsplash

As developers, we often come across situations where the same feature is used in multiple apps and hence we would have to rewrite the same code again and again. Can’t we convert the repeated code as a reusable component and use it? Yes. So then the next question that would arise is how do we do it in Android?

There are many ways of tackling this concern, one of them is by creating a Library. We use a lot of libraries in Android development and it’s not a new concept. In this blog let’s look into building an Android library and possible ways to consume it locally in other apps. Also I will be explaining the problems I faced while creating and publishing the Android library.

Note: The Android library in this example was created in Android Studio Arctic fox(2020.3.1) version. Based on the studio version, the new library creation and usage steps might vary a little. Refer official documentation in case the steps differ.

Create the library:

To create an Android library, first we need to create a new project which would by default include an app module like we usually do. After successfully creating the project, create a new module and name it based on what the module is going to contain.

Here in this example, I am creating a network library specific to my requirement. So I have named it networklib. After the module is successfully created, add the code as per your need.

Prepare the library to be consumed:

The library module created can be used as is in the form of a module which will be explained in the next section. We can also convert the library into an Android archive file(.aar) and share it. But the recommended way of sharing the libraries is by publishing it to the maven(remote or local) and using it as a dependency.

To generate the .aar file, click on the gradle tab in the Android Studio and run the ‘assemble’ command. This generates both the debug and release version of the .aar file of the library. We can find those files under the package shown below.

As mentioned earlier, the library can also be published in Maven and then consumed as a dependency. To publish the library locally,

  1. Install maven in your system. The steps to be followed to download and install maven can be referred from here
  2. After successful maven installation, create a new gradle file with name ‘publishLocal’ and add the following script inside the file

Here the apply plugin: ‘maven-publish’ adds the maven publish plugin to publish the library in the maven repo created during the maven installation.

3. In the module level build gradle file, add the following

4. Now run gradle sync, then clean and build the project. After the successful gradle build, run the command publishToMavenLocal.

This will create the required aar and pom files and publish them into the local maven repository i.e., in /.m2 directory in your system.

Consume the library:

If you want to consume this library in some other project, then there are different ways of doing it as mentioned below:

  1. Directly add in the entire module file in the project:

This is as simple as importing the entire library as a module into the project as shown in below image.

And then add it as a dependency in the module level app build gradle file.

implementation project(path: ‘:networklib’)

Or we can also add this as dependency from the project structure dialog. Here replace the “networklib” with the name of the library module that you have created.

This approach can be used when you need to have access to the source code of the library.

2. Use the .aar file version of the library module:

The .aar file is added in the libs directory of the project and then add the dependency in the app module level build gradle file as follows

implementation files(‘libs/networklib-release.aar’)

In this approach, the source code of the library will not be accessible. Also a disadvantage of this approach is that the dependencies used by the library needs to be added manually in the consumer app as well as the dependency information will not be available in the .aar file. If not, the app will still build and run, but the library will not function as expected.

3. Use the library by publishing in local maven:

To use the locally published library like any other standard library dependency we can add it in the app level build gradle using the library name and version as shown below:

 implementation ‘com.yml.networklib:networklib:1.0’

Also add mavenLocal() repository location in the settings.gradle file inside dependencyResolutionManagement

In this method, the pom file will also be generated along with the .aar file, so adding the dependency again explicitly in the consumer app will not be required.

If we want to make the library public, uploading the library to a remote repo like Jitpack etc would be the best option. To publish the library to a remote repository, the following blog can be a good reference.

--

--