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/ 
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/ b/mylibrary/src/main/java/com/example/mylibrary/
index 0476598..00c6383 100644
--- a/mylibrary/src/main/java/com/example/mylibrary/
+++ b/mylibrary/src/main/java/com/example/mylibrary/
@@ -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

Online Library Management System in... x
Online Library Management System in PHP Source Code 2021 | PHP Projects Free Download

Leave a Comment

Sign up to our newsletter!

ADB Commands Android Android Applications Android Build system Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development, Debugging and Performance Tools Development and Build Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia NDK / Middleware / HAL Operating System Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web Design and Development Website Hosting Wordpress Yocto Embedded Linux