Home » Android » Include an External precompiled AAR library Using Gradle and Android Studio – Part 4

Include an External precompiled AAR library Using Gradle and Android Studio – Part 4

As we have seen in our previous posts Part 1, Part 2, Part 3, we created a library module, which using another library as module and those has been used by our application.

Now, since most of the time we come across where we only get the precompiled library ( something like when developed proprietary applications ) and we have to use that precompiled AAR library to develop our own application. With this post we will describe how you can link the already build aar with our main application.

Importing the precompiled AAR using Android Studio

Open your Android application source where you want to add external library and create a “New Module” from File -> New as shown below,

Android Library

Now, from opened window, select “Import .AAR / .JAR Package” option and click Next as shown below,

AndroidNewModule

From the next opened Window, select the precompiled .aar file from “File Name” and give the preferably all small letter directory name for the new module which uses precompiled library as shown below and click “Finish”

Android

After this, android studio creates a top level directory module with name as you gave in studio eg. precompiledlib with copied library and newly created build.gradle as below,

$ tree precompiledlib/
precompiledlib/
├── app-release.aar
├── build
├── build.gradle
└── precompiledlib.iml
$ vim precompiledlib/build.gradle
configurations.maybeCreate("default")
artifacts.add("default", file('app-release.aar'))

as you can see above precompiledlib/build.gradle contains the name of the external library we just now imported using android studio.

Now, we will just show the modifications we did in our Main Application to use this precompiled library module,

First you will need to modify your applications app/build.gradle to inform it using external library as,

$ vim app/build.gradle 
dependencies {
implementation project(":precompiledlib")

And the modifications did in our Main Application are as below,

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

+import com.example.apptolibrary.AppToLibrary;
 
 public class MainActivity extends AppCompatActivity {
 
        private MyLibrary myLib = null;
+       private AppToLibrary appToLibrary = null;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -17,5 +19,9 @@ public class MainActivity extends AppCompatActivity {
        myLib = new MyLibrary();
        //call simple function from library
        myLib.simpleFunctionInLibrary();
+
+       appToLibrary = new AppToLibrary();
+       appToLibrary.HelloFromAppToLibraryClass();
+
     }
 }

Now, if you compile, install and open this application, you will see the logcat messages as below,

$ 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
MyLibrary:AppToLibrary: called function: HelloFromAppToLibraryClass

All the source code is available at https://github.com/lynxbee/android_libraries

Leave a Comment