Importing Existing C++ Libraries (.a or .so) into NDK Android Projects
This article guides you on how to integrate pre-built C++ libraries (.a or .so) into your Android NDK projects. This process is essential for leveraging existing codebases and enhancing your app’s functionalities.
Understanding the Process
Integrating C++ libraries into an Android NDK project involves a multi-step process:
1. Prepare Your Library
- Static Libraries (.a): Ensure your static library is built for the target Android architecture (ARM, x86, etc.).
- Shared Libraries (.so): Your shared library needs to be compiled for the specific Android architecture you intend to support. Cross-compilation is often necessary for this step.
2. Configure Your NDK Project
- Add Library Files: Place the library files (.a or .so) in the appropriate directory within your project’s structure. A common location is
jni/libs/
, where/ represents the target architecture (e.g., armeabi-v7a). - Modify CMakeLists.txt: Include the libraries in your CMake build process. Add target_link_libraries to your application target to link against the external library.
3. Utilize the Library in Your C++ Code
- Include Headers: Ensure you have the necessary header files for your library. Include them in your C++ code using the standard include directives.
- Call Functions: You can now use the library’s functions and classes directly within your C++ code.
Step-by-Step Guide
1. Create a New Project
mkdir my_app cd my_app
2. Initialize a CMake Project
cmake -B build -S .
3. Create a JNI Folder
mkdir jni
4. Add Library Files
Assuming your library files are in the libs
folder, copy the corresponding files to the jni/libs/
directory:
mkdir jni/libs/armeabi-v7a cp libs/armeabi-v7a/libMyLibrary.so jni/libs/armeabi-v7a/
5. Modify CMakeLists.txt
cmake_minimum_required(VERSION 3.10) add_library(my_app_native SHARED jni/my_app_native.cpp) target_link_libraries(my_app_native log # Link to your external library jni/libs/armeabi-v7a/libMyLibrary.so )
6. Define Your Native Code
Create a file named jni/my_app_native.cpp
:
#include#include #include "MyLibrary.h" // Assuming 'MyLibrary.h' is included in the external library. extern "C" JNIEXPORT void JNICALL Java_com_example_my_app_MainActivity_nativeMethod(JNIEnv *env, jobject thiz) { // Use functions from your library MyLibrary::someFunction(); }
7. Build Your Application
cd build cmake --build .
Example: Using a Basic Library
Let’s assume you have a simple library named “MyLibrary” containing a function to print “Hello World!”
MyLibrary.h
#ifndef MY_LIBRARY_H #define MY_LIBRARY_H class MyLibrary { public: static void printHelloWorld(); }; #endif
MyLibrary.cpp
#include "MyLibrary.h" #includevoid MyLibrary::printHelloWorld() { std::cout << "Hello World!" << std::endl; }
Building and Linking the Library
After building the library and placing the .a or .so files, you would add the following to your CMakeLists.txt
:
target_link_libraries(my_app_native log jni/libs/armeabi-v7a/libMyLibrary.so )
Calling the Function in Native Code
#include#include #include "MyLibrary.h" // Include the header file extern "C" JNIEXPORT void JNICALL Java_com_example_my_app_MainActivity_nativeMethod(JNIEnv *env, jobject thiz) { MyLibrary::printHelloWorld(); }
Conclusion
Integrating existing C++ libraries into your NDK projects is a powerful way to extend functionalities and improve code reusability. By following the steps outlined in this article, you can successfully incorporate these libraries and leverage their capabilities to create robust and feature-rich Android applications.