Developing Android Library

Why is there a need to develop a library? Well, developing libraries help use their feature in many applications. You must have seen its live example in forms of payment gateway, chat library, retrofit, butterknife and many more. Creating a library in Android is effortless. Android library compiles into AAR file which also provides you with a user interface. In this blog, we will dive into learning how to create a library. We evaluate four key issues: 

  • How a library can be built (what should be kept in mind)
  • Include libraries for a project
  • Initializing a library.
  • Publishing a library.

Creating the Android library:

Create a project in Android Studio. Create a module inside your project, then select the Android Library option and click Finish. Add your code inside the library module. What should be kept in mind while writing code: there should be an entry point (or how to initialize the library), how to get the callbacks of the user events(optional) and how to exit the library. 

Include the library as a dependency: 

In order to use library code, you can go with any of the following two ways:

1. Add .aar file or .jar file 

  • Click File > New> New Module
  • Click import .aar/.jar Package and then hit the Next button.
  • Enter the location of the library and click Finish.

 While following these steps Android Studio creates build.gradle file for the library which looks like:    

configurations.maybeCreate("default")
      artifacts.add("default", file('libraryName'))

 2. Add the source code of the library as a module in your project:

  •  Click File > New > Import module
  • Enter the location of the module and then click Next.

Now we should add the library as a dependency in the app level gradle file:

dependencies {
    implementation project(":my-library-module")
}

3. After that, sync the project.

Initializing a library:

After adding the module to your project, the next thing which comes to our mind is how to use it? Well to initialize or enter into the library, you must write initialization code or pass values if required to the library. And this is mostly done inside the Application class of the app keeping in mind one-time initialization throughout the app. Below Snippet of code is an example of initializing a library inside the Application class:

public  class MyApplication extends Application {
@override
public  void onCreate() {
super.onCreate();
...
P2PLibrary.init(new P2PLibrary.Options(this, "Username","Authkey"));

}
}

Publishing your Library:

Create a GitHub repo for your app and library. One thing to keep in mind is to add the android-mavin plugin in project/build.gradle.

dependencies {
...
classpath 'com.github.decendants:android-maven-gradle-plugin:1.5'
}

Push the code in Github. And then create the release tag. Open JitPack and lookup for your library. After this, you can get your library from Jitpack.Then add the maven plugin and group setting inside library/build.gradle.

Conclusion

In this blog, every possible aspect of creating a library is covered, whereas some topics are discussed briefly. So for understanding how to create your own library with a small example, see Android toast library

Leave a Reply