Alright, using CMake with NDK because who doesn’t love a good mix of acronyms in their life? If you’re like me, you probably spend your days staring at screens and muttering “CMake” under your breath. But no need to get all worked up, bro, for I am here to guide you through the wonders of this magical tool that will make your Android development dreams come true!
Before anything else what is CMake? Well, it’s a fancy build system that helps us manage our projects and automate some of the more tedious tasks. It’s like having a personal assistant who can handle all the grunt work for you while you sit back and sip your latte (or whatever beverage floats your boat).
Now, NDK which stands for Native Development Kit. This is a toolset that allows us to develop native C/C++ applications on Android devices using the Java Virtual Machine (JVM) or the Android Runtime (ART). It’s like having a secret weapon in your arsenal that lets you write code in languages other than Java, which can be pretty ***** useful if you ask me.
So how do we use CMake with NDK? Well, it’s actually quite simple just follow these easy steps:
1. First, make sure you have both CMake and the Android NDK installed on your machine. You can download them from their respective websites (https://cmake.org/download/) and https://developer.android.com/ndk).
2. Create a new project in your favorite IDE or text editor, and add some code to it. For example:
“`c++
// This script is used to add two numbers in a C++ project for Android using the Android NDK and CMake.
// Import necessary libraries for the script to run
#include
#include
// Declare an external “C” function to be used in the Java code
extern “C” {
// Declare a JNIEXPORT function with the correct return type and parameters
JNIEXPORT jint JNICALL Java_com_example_MainActivity_addNumbers(JNIEnv *env, jobject thiz, jint x, jint y) {
// Add the two numbers passed in as parameters and return the result
return (x + y);
}
}
3. Create a new CMakeLists.txt file in your project directory and add the following lines:
cmake
# Set the project name and version
project(MyProject VERSION 1.0 LANGUAGES CXX)
# Set the target architecture to ARM v7a (you can change this if needed)
set(CMAKE_ANDROID_ARCH_ABI “armeabi-v7a”)
# Find the Android NDK package
find_package(AndroidNdk REQUIRED)
# Include the necessary toolchain file for Android
include(${ANDROID_NDK}/build/cmake/android.toolchain.cmake)
# Set the source files for your project
set(SOURCE_FILES main.cpp)
# Create a static library called “mylib” using the source files
add_library(mylib STATIC ${SOURCE_FILES})
# Use Gnu STL instead of Android’s default STL (optional)
set(ANDROID_STL “gnustl_static”)
# Include the necessary directories for your target architecture and API level
include_directories(${ANDROID_NDK}/platforms/android-${ANDROID_API}/arch-arm/${CMAKE_ANDROID_ARCH_ABI})
# Set the Android platform directory (optional, but recommended)
set(ANDROID_PLATFORM ${ANDROID_SDK_ROOT}/platforms)
4. Run CMake in your project directory by opening a terminal or command prompt and running:
bash
# This script is used to run CMake in a project directory.
# The following line uses the “cmake” command to run CMake in the current directory.
# The “.” indicates the current directory.
# This will generate the necessary build files for the project.
cmake .
5. Once CMake has finished generating the build files, navigate to the "build" folder that was created (or whatever name you chose for it) and run:
bash
# This script is used to compile the project using the make command.
# The -j flag allows for parallel compilation, with the number after it indicating the number of cores to use.
# It is optional, but can speed up the compilation process.
# Replace the number 4 with the number of cores on your machine for optimal performance.
make -j4 # Replace 4 with the number of cores on your machine (optional)
6. Copy the resulting .so file from the "libs/armeabi-v7a" folder to your Android project's "src/main/jniLibs/armeabi-v7a" folder, and you're done! You can now use this library in your Java code by adding:
java
// Load the library “mylib” into the current context
System.loadLibrary(“mylib”); // replace “mylib” with the name of your library (without .so)
// Call the function “addNumbers” from the loaded library and store the result in the variable “result”
int result = addNumbers(5, 10);
// Log the result to the console using the “Log” class and the “d” method
Log.d(“MainActivity”, String.valueOf(result));
“`
And that’s it! You now have a fully functional C/C++ library running on Android devices using CMake and NDK. Who needs X-ray vision when you have CMake?