Adding dependency of one library ( Module ) to another library ( Module ) in Android : Creating AAR library for Android Part 3

In our first part we seen “Creating AAR library for Android apps using Android Studio” and in second part we tried for “Accessing Library functions from Application

With this post we will continue from Part 2 and tried to create another library (mymathlib) which will be used by first library (mylibrary) which finally will be used by actual application. This would be something like below,

Android Library

Now, first create the math library as mentioned in our part 1 post, we have used the package name for this math library as “com.example.mymathlib” and we will create a simple Addition class as below,

$ vim mymathlib/src/main/java/com/example/mymathlib/MyMathLibrary.java 
package com.example.mymathlib;

import android.util.Log;

public class MyMathLibrary {
        public static String TAG = "MyLibrary:Math";

        public MyMathLibrary () {
        }

        public int simpleAddition(int a, int b) {
                int sum = a+b;
                Log.d(TAG, "This call reached to : MyMathLibrary:simpleAddition:: " + sum);
                return sum;
        }
}

Now, since we want this math library to be used by our first library “mylibrary” we will have to add the dependancy of mymathlib to mylib by modifying first library’s “mylibrary” mylibrary/build.gradle as,

$ vim mylibrary/build.gradle
dependencies {
    implementation project(":mymathlib")
} 

Now, we will modify the first library’s class to access this second library functions and return the additions of two numbers to first library. The modified code in first library will look like as below,

$ git diff
diff --git a/mylibrary/src/main/java/com/example/mylibrary/MyLibrary.java b/mylibrary/src/main/java/com/example/mylibrary/MyLibrary.java
index 0476598..00c6383 100644
--- a/mylibrary/src/main/java/com/example/mylibrary/MyLibrary.java
+++ b/mylibrary/src/main/java/com/example/mylibrary/MyLibrary.java
@@ -1,14 +1,22 @@
 package com.example.mylibrary;
 
 import android.util.Log;
+import com.example.mymathlib.MyMathLibrary;
 
 public class MyLibrary {
        public static String TAG = "MyLibrary";
 
+       private MyMathLibrary myMathLib = null;
+
        public MyLibrary () {
        }
 
        public void simpleFunctionInLibrary() {
+               int sumReturned = 0;
                Log.d(TAG, "This call reached to : simpleFunctionInLibrary");
+               Log.d(TAG, "Calling Addition Function from Another Math Library");
+               myMathLib = new MyMathLibrary();
+               sumReturned = myMathLib.simpleAddition(10, 23);
+               Log.d(TAG, "sumReturned from Math Library: " + sumReturned);
        }
 }

You will also need to verify that top level settings.gradle is modified to add math library as ,

diff --git a/settings.gradle b/settings.gradle
index 9c982a3..e21761e 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -1 +1 @@
-include ':app', ':mylibrary'
+include ':app', ':mylibrary', ':mymathlib'

Now, if we compile the application using “./gradlew clean build” and install apk and open app on mobile, you will see the logs showing successful linking of libraries and accessing the functions as below,

$ adb shell
$ logcat | grep MyLibrary
MyLibrary: This call reached to : simpleFunctionInLibrary
MyLibrary: Calling Addition Function from Another Math Library
MyLibrary:Math: This call reached to : MyMathLibrary:simpleAddition:: 33
MyLibrary: sumReturned from Math Library: 33

The complete source code is available at https://github.com/lynxbee/android_libraries

Leave a Comment