[Android development] Import .a library into Android project

Thế Dũng
4 min readJul 6, 2021

--

Another topic that not to many people have to face on. Today’s article is how I import a .a file into android project. I don’t know why but it seems to be very rare article that write about this. And some of them are useless for me. So I think I have to write this down.

To solve this thing, I choose CMake to be the tool for config library into project. First thing first, we check the CMake and NDK have been installed into our Android Studio or not.

Go to Tool → SDK Management then, choose SDK Tools tab then make sure you have 2 boxes below in checked status (NDK and CMake):

When you found that nothing is wrong right here. It’s ok to move to next step: import the .a file.

At the first time I saw the request import .a library, I’m quite confuse that why they gave me a lot of files. But I realize that it have to be a lot of files. More specific info, each Android device have it own architecture. We have here 4 big architecture that most of Android Devices use:

  • arm64-v8a
  • armeabi-v7a
  • x86
  • x86_64

So, you should have enough .a file to contribute to each architecture.

In this article, I’ll use .a library from google dev hub. I mean this hub. You see a lot of small project right? So you need to focus on the on name hello-libs. I have take these .a file and zip it for you to use. Take this! And one more file, the header file (.h file).

After have .a and .h file in you computer, let’s put it in right place.

You can put these above file wherever you want. But I will follow how Android Development does:

  • Choose to view project as project view from Android Studio
  • Right-click on root folder and create new directory name distribution then, inside it, create new dir name gmath in case this library name gmath. You can put any name you want. I don’t care.
  • Create 2 more directories name include and lib inside gmath.
  • Put the .h file inside include folder and list of folder contain .a file into lib folder.
  • Finally, you have this folder tree

After import enough file, it’s our turn to create some files write some code.

  • Go to app → src → main and create a directory name cpp
  • Create a CMake file name CMakeLists.txt and another file gmath-bridge.cpp (The .cpp file’s name can be anything you want. But the CMake file must be that name)

We have enough file now.

Now go to build gradle file and add some necessary line of code:

android {
compileSdkVersion 30
ndkVersion '22.1.7171670'
...
kotlinOptions {
jvmTarget = '1.8'
}

externalNativeBuild {
cmake {
version '3.18.1'
path 'src/main/cpp/CMakeLists.txt'
}
}

}

We have defined ndk version use and pointed out where is the CMake file and which version of CMake we use. Then sync project’s gradle to apply the change.

Next, open the CMakeLists.txt file and read the code. I put some explanation inside the code snippet.

What we have done is add .a library as static library and config gmath-bridge file to be our second library. Then link the .a lib to our .cpp file. For further purpose, this cpp file will be the channel that we use to communicate with .a library.

Now, let’s edit gmath-bridge.cpp file

But for more convenient, choose to view project structure as android view.

Create MainActivityViewModel to make the call to gmath-bridge.cpp

Inside MainActivityViewModel, create a function that use to link to gmath-bridge.cpp later.

Remember to call System.loadLibrary first, then all other thing can work.

After prepare for cpp file, we can finally write code in gmath-bridge.cpp file

Again, I have explained code inside code snippet.

At this point, everything seems to be ok.

Now, go to the MainActivity and make a call to MainActivityViewModel, then function in gmath-bridge.cpp can be called and execute and return a string, then we log it and see the result.

Now, run the project and enjoy the result.

It works. Thank God!

Complete source code here.

Thanks for reading! Bye!

--

--

Thế Dũng
Thế Dũng

Written by Thế Dũng

Software engineer - Mobile developer

No responses yet