Build native code with ndk-build, Android.mk, and Application.mk files
The ndk-build system is a collection of GNU Make scripts that simplify building native code for Android. It uses Android.mk files to define modules and Application.mk for project-wide settings.
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)# Module name (without lib prefix or .so suffix)LOCAL_MODULE := mymodule# Source filesLOCAL_SRC_FILES := file1.cpp file2.cpp# Module typeinclude $(BUILD_SHARED_LIBRARY) # or BUILD_STATIC_LIBRARY
Always use $(call my-dir) to set LOCAL_PATH at the beginning of your Android.mk. This ensures paths are correct regardless of where ndk-build is invoked.
# List source files explicitlyLOCAL_SRC_FILES := main.cpp utils.cpp# Use wildcards (not recommended for large projects)LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)/*.cpp)# Prebuilt librariesLOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libprebuilt.so
# Add include directoriesLOCAL_C_INCLUDES := $(LOCAL_PATH)/includeLOCAL_C_INCLUDES += $(LOCAL_PATH)/../external/headers# Export includes to dependent modulesLOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
# C++ flagsLOCAL_CPPFLAGS := -std=c++17 -Wall -Wextra# C flagsLOCAL_CFLAGS := -O2 -DNDEBUG# Preprocessor definesLOCAL_CPPFLAGS += -DMY_DEFINE=1
# Link with shared librariesLOCAL_SHARED_LIBRARIES := libdep1 libdep2# Link with static librariesLOCAL_STATIC_LIBRARIES := libstatic# Link with system librariesLOCAL_LDLIBS := -llog -landroid
# Target ABIs (builds for all if not specified)APP_ABI := arm64-v8a armeabi-v7a# Minimum API levelAPP_PLATFORM := android-21# C++ standard libraryAPP_STL := c++_shared# Build mode (release or debug)APP_OPTIM := release# C++ featuresAPP_CPPFLAGS := -std=c++17 -frtti -fexceptions
# Build for specific ABIsAPP_ABI := arm64-v8a armeabi-v7a# Build for all supported ABIs (not recommended)APP_ABI := all# Build for 64-bit onlyAPP_ABI := arm64-v8a x86_64
Google Play requires 64-bit support for all apps with native code. Always include arm64-v8a and x86_64.
# Get current directory$(call my-dir)# Import module from NDK_MODULE_PATH$(call import-module,android/native_app_glue)# Import all modules from directory$(call import-add-path,$(LOCAL_PATH)/../external)