Cross Compiling “Hello World” on Mac for Android
This tutorial guides you through the process of cross-compiling a simple “Hello World” program on a Mac for an Android device. Cross-compilation allows you to build software for a different architecture and operating system than the one you’re currently using.
Prerequisites
- macOS operating system
- Android NDK (Native Development Kit) – download from the Android Developer website
- A text editor or IDE for writing C code (e.g., Xcode, VS Code)
- A basic understanding of C programming
Setting Up the Environment
- Install the NDK: Download the Android NDK from the official website and extract it to a convenient location. Update your environment variables to include the NDK path.
export ANDROID_NDK=/path/to/android-ndk-r21
- Create a Project Directory: Create a new directory for your project.
mkdir hello-world-android cd hello-world-android
- Create the C Source File: Inside your project directory, create a C source file named “hello.c”. Add the following code:
#include
int main() { printf("Hello, World!\n"); return 0; }
Building the “Hello World” Application
- Configure the Build Script: Create a file named “Android.mk” in the same directory. This file tells the NDK how to build your application:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := hello LOCAL_SRC_FILES := hello.c include $(BUILD_EXECUTABLE)
- Compile the Code: Run the following command from the project directory to compile the code:
$NDK/ndk-build
Running the Application
- Transfer to Android Device: Copy the compiled executable file (e.g., “libhello.so”) to your Android device. You can use tools like ADB (Android Debug Bridge) for this purpose.
adb push libhello.so /sdcard/
- Execute the Application: Use a terminal emulator on your Android device to navigate to the directory where you copied the file and run the executable:
./libhello.so
Output
Hello, World!
Explanation
- Android.mk: This file configures the build process, specifying the source files, the target module name, and the type of build (in this case, an executable).
- NDK-build: This command calls the NDK build system, which uses the “Android.mk” file to compile your C code for the Android platform.
- libhello.so: The NDK produces a shared library file (with the .so extension) that can be executed on Android devices.
Table: Cross-Compilation vs. Native Compilation
Feature | Cross-Compilation | Native Compilation |
---|---|---|
Build Environment | Different from target system | Same as target system |
Target Architecture | Can target different architectures | Targets the same architecture |
Use Cases | Building software for embedded devices, mobile platforms, or different operating systems | Building software for the same system |
Conclusion
You have successfully cross-compiled a “Hello World” program on your Mac for an Android device. This process demonstrates the power of cross-compilation, enabling you to develop and run software on various platforms. As your projects become more complex, you can further explore the NDK to utilize advanced features and libraries for Android development.