Creating AAR Libraries for Android: From Scratch (Part 3)

Abou ZuhayrAbou Zuhayr
4 min read

Introduction

In the second installment of our series, we delved deeper into the world of Android development, focusing on the creation and utilization of pure Java libraries, also known as JAR libraries. We guided you step-by-step, from initiating a new project to integrating the library into your Android projects using platforms like JitPack.io.

As we gear up for the third part of this series, we are set to explore the dynamic realm of Android Libraries or AARs. These libraries not only allow us to perform basic operations like the ones we achieved using JAR libraries but also take a step further by facilitating the integration of Android-specific classes such as Context, enhancing the functionality and efficiency of our Android applications.

The idea

The idea for today's exercise is to create a library that can access Android-specific classes like Context, Activity etc, and show a toast on UI whenever we call one of its methods.

Step 1

Create a new project (or you can use the same project directory that we had created for our last post), and inside that project go to file and create a new module.

Step 2

Now, select Android library as the module type, and name the module whatever you'd like the library to be called, I've named it as testlibrary, next go ahead and create a class named TestClass inside this module.

Step 3

Let's add the following code to our TestClass In the module, this class would need two integers and a context which will in turn be used to show a toast for each method call.

public class TestClass {

    private int a, b;
    private Context context;

    public TestClass(int a, int b, Context context){
        this.a = a;
        this.b = b;
        this.context = context;
    }

    public void setAB(int a, int b){
        this.a = a;
        this.b = b;
    }

    public int add(){
        int c = a+b;
        Toast.makeText(context, "Result is " + c, Toast.LENGTH_SHORT).show();
        return c;
    }

    public int subtract(){
        int c = a-b;
        Toast.makeText(context, "Result is " + c, Toast.LENGTH_SHORT).show();
        return c;
    }

    public int multiply(){
        int c = a*b;
        Toast.makeText(context, "Result is " + c, Toast.LENGTH_SHORT).show();
        return c;
    }
}

Step 4

If we build our project and go the the output directory to check the library generated, you should now be able to locate an AAR file generated there as shown below

Step 5

  1. The AARs can also be provided to a project by locally copy-pasting into the library folder as discussed in the previous post here.

  2. The steps to push the code and generate a release using jitpack.io are also similar which can also be referenced from the previous post.

And that's it!

We should now be able to implement and access our library with a toast message as shown below.

class MainActivity : AppCompatActivity() {
    var TAG = this.javaClass.canonicalName
    override fun onCreate(savedInstanceState: Bundle?) {
        var result = MyCalculator().addNumbers(1,1)
        val testClass = TestClass(1,1, this).add() //new AAR library
}

Important Points

  1. You can release more than one library (AARs & JARs) in a single release.

Jitpack resolves all the libraries that are present in the repository branch that you try to look up. So if you have multiple libraries implemented, all of them will be built for the release.

For e.g, the project directory that is referenced in this post has both the calculator library that we created in our previous post as well as the new testlibrary which we created today. And in the new release, you can see on jitpack.io site that both the libraries have been built and provided.

  1. Handling Build Errors

    Since jitpack.io rebuilds the project before providing it to us, it's obvious that such rebuilds can cause build crashes at some point (if the released version has some bugs or could be due to a mismatched gradle version). In such cases, the build logs can be found next to the release version which can be used to analyze the reason behind the crash.

As you can see from the screenshot above, v1.0.1 & v1.0.3 were faulty builds, Upon analyzing the generated build file, the apparent reason was a mismatched JVM target version.

Conclusion

This tutorial should have equipped us all with the knowledge to create an Android library that can access Android-specific classes and showcase a toast on the UI. Through a step-by-step approach, we have demystified the process of setting up and integrating an AAR library into an Android project, highlighting the pivotal role of platforms like JitPack.io in facilitating this process. As we ventured into the nuances of handling build errors and releasing multiple libraries efficiently, we hope to have fostered a deeper understanding and appreciation for Android development.

In our next and final post on this series, we'll try to understand the principles involved in creating libraries and what should and should not be done to keep the code architecture clean and scalable.

As we move forward, we'll try to leverage this knowledge to enhance the functionality and efficiency of Android applications, exploring the dynamic capabilities that AAR libraries bring to the table. Till then happy coding!

0
Subscribe to my newsletter

Read articles from Abou Zuhayr directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abou Zuhayr
Abou Zuhayr

Hey, I’m Abou Zuhayr, Android developer by day, wannabe rockstar by night. With 6 years of Android wizardry under my belt and currently working at Gather.ai, I've successfully convinced multiple devices to do my bidding while pretending I'm not secretly just turning them off and on again. I’m also deeply embedded (pun intended) in systems that make you question if they’re alive. When I'm not fixing bugs that aren’t my fault (I swear!), I’m serenading my code with guitar riffs or drumming away the frustration of yet another NullPointerException. Oh, and I write sometimes – mostly about how Android development feels like extreme sport mixed with jazz improvisation. Looking for new challenges in tech that don’t involve my devices plotting against me!