Home » Android » Creating AAR library for Android – Accessing Library functions from Application – Part 2

Creating AAR library for Android – Accessing Library functions from Application – Part 2

In our last post “Creating AAR library for Android apps using Android Studio – Part 1” we had created framework for building library using Android Studio which created a simple aar library but didn’t had any code written in it which can be used by Application.

In this post, we will write a simple function in library and demo it how you can use this library to access the function in application created from “Refer How to develop first android Application/App in Android Studio

First create an directory with package name of the library in library module as,

$ mkdir -p mylibrary/src/main/java/com/example/mylibrary/

Now, we will write a simple class in library which can be accessed as library from Application.

$ vim mylibrary/src/main/java/com/example/mylibrary/MyLibrary.java
package com.example.mylibrary;

import android.util.Log;

public class MyLibrary {
        public static String TAG = "MyLibrary";

        public MyLibrary () {
        }

        public void simpleFunctionInLibrary() {
                Log.d(TAG, "This call reached to : simpleFunctionInLibrary");
        }
}

As you can see above, our class package is “com.example.mylibrary” as we used during creation of library in last part 1 post, and then written in simple function “simpleFunctionInLibrary” which we will try to access from main activity.

Linking Library with Main Application and Using library functions

Now, in the main application source code, we will have to link our library from build.gradle, hence modify app/build.gradle to add below line,

$ vim app/build.gradle
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(":mylibrary")
}

Now, we will modify the main Activity source code to try and use the library as below,

package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.mylibrary.MyLibrary;

public class MainActivity extends AppCompatActivity {

        private MyLibrary myLib = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myLib = new MyLibrary();
        //call simple function from library
        myLib.simpleFunctionInLibrary();
    }
}

in Above MainActivity.java the code which we added is as below showing with “plus” + sign,

$ git diff app/src/main/java/com/example/myapplication/MainActivity.java

@@ -3,11 +3,19 @@ package com.example.myapplication;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 
+import com.example.mylibrary.MyLibrary;
+
 public class MainActivity extends AppCompatActivity {
 
+       private MyLibrary myLib = null;
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
+
+       myLib = new MyLibrary();
+       //call simple function from library
+       myLib.simpleFunctionInLibrary();
     }
-}
\ No newline at end of file
+}

Now, if you compile and install the APK, once you opened MyApplication app on your mobile, you should see following message in Logcat as,

$ adb shell
$ logcat | grep MyLibrary 

08-29 18:32:45.526 11478 11478 D MyLibrary: This call reached to : simpleFunctionInLibrary

Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

1 thought on “Creating AAR library for Android – Accessing Library functions from Application – Part 2”

Leave a Comment