Importing Existing C++ Libraries (.a or .so) into NDK Android Projects

Importing Existing C++ Libraries (.a or .so) into NDK Android Projects

This article explains how to integrate pre-built C++ libraries, provided as static (.a) or shared (.so) files, into your Android Native Development Kit (NDK) project.

Understanding .a and .so Files

  • .a (Static Library): Contains object files, linked directly into the final executable during compilation. Changes to the library require recompiling the entire application.
  • .so (Shared Library): Contains object files loaded at runtime, allowing for code reuse and dynamic linking. Updates to the library can be made without recompiling the entire application.

Project Setup

Before you start, ensure you have:

  • An Android Studio project with NDK support.
  • The pre-built C++ library file(s) (.a or .so).

Using Pre-built Libraries

1. Including the Library in the Build System

Add the library file(s) to your project’s build system. The specific steps depend on the build system (e.g., CMake, ndk-build):

CMake

# Add the library to the CMakeLists.txt file
target_link_libraries(your_app_name
  ${CMAKE_SOURCE_DIR}/your_library_path/libYourLibrary.a
)
# (or)
target_link_libraries(your_app_name
  ${CMAKE_SOURCE_DIR}/your_library_path/libYourLibrary.so
)
ndk-build

# Add the library path and name to the Android.mk file
LOCAL_STATIC_LIBRARIES += your_library_name
# (or)
LOCAL_SHARED_LIBRARIES += your_library_name

2. Linking the Library in C++ Code

Import the library into your C++ code to use its functions and classes:


#include <your_library_header.h>

int main() {
  // Use functions and classes from your_library_header.h
}

Example: Integrating a .so Library

1. Create the Library

Assume you have a shared library called “my_library.so” in the “libs” directory of your project, containing a function called add().

2. Include the Library in the Build System

Add the following to your CMakeLists.txt file:


target_link_libraries(your_app_name
  ${CMAKE_SOURCE_DIR}/libs/libmy_library.so
)

3. Link and Use the Library in Your Code


#include "my_library.h" // Assuming the library header file

int main() {
  int sum = add(2, 3);
  printf("2 + 3 = %d\n", sum);
}

4. Run the Application

Build and run your application. You should see the output:


2 + 3 = 5

Comparison: .a vs. .so

Feature .a (Static Library) .so (Shared Library)
Linking Linked directly into the executable Loaded at runtime
Updates Requires recompilation of the application Can be updated without recompiling the application
File Size Larger executable size Smaller executable size
Memory Higher memory consumption Lower memory consumption
Performance Potentially faster execution May have a slight performance overhead

Conclusion

By following these steps, you can effectively integrate pre-built C++ libraries into your Android NDK project. The choice between .a and .so files depends on your project’s specific requirements. Always consider the trade-offs between size, memory usage, and performance when selecting the appropriate library type.


Leave a Reply

Your email address will not be published. Required fields are marked *