Skip to main content
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.

Getting started

Project structure

A typical ndk-build project structure:
The jni/ directory is the default location for ndk-build files, but you can customize this in your Gradle configuration.

Basic setup

1

Create the jni directory

Create a jni/ directory in your module’s src/main/ folder:
2

Create Android.mk

Define your native modules in Android.mk:
Android.mk
3

Create Application.mk (optional)

Configure project-wide settings:
Application.mk
4

Configure Gradle

Link ndk-build in your build.gradle:
build.gradle

Android.mk file

The Android.mk file defines one or more native modules using GNU Make syntax.

Required variables

Every Android.mk must set these variables:
Android.mk
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.

Common variables

Module types

Multiple modules

Define multiple modules in a single Android.mk:
Android.mk

Application.mk file

The Application.mk file configures project-wide build settings.

Common settings

Application.mk

STL selection

Recommended. Modern C++ standard library as a shared library.
  • Full C++ standard library support
  • Smaller per-module size (shared across modules)
  • Requires bundling libc++_shared.so with your app
If you use APP_STL := c++_shared, ensure the libc++_shared.so library is packaged with your APK. The Gradle plugin handles this automatically.

ABI configuration

Application.mk
Google Play requires 64-bit support for all apps with native code. Always include arm64-v8a and x86_64.

NDK variables

ndk-build provides many built-in variables:

Path variables

Utility functions

Advanced usage

Conditional compilation

Android.mk

Debug vs release builds

Android.mk

Using NDK modules

Import prebuilt NDK modules like native_app_glue:
Android.mk

Building from command line

You can build directly with ndk-build (without Gradle):
When using Gradle integration, you typically don’t need to run ndk-build directly. Gradle invokes it automatically during the build process.

Common issues

Undefined references

Solutions:
  • Ensure all required source files are in LOCAL_SRC_FILES
  • Add dependencies to LOCAL_SHARED_LIBRARIES or LOCAL_STATIC_LIBRARIES
  • Check that library order is correct (dependencies listed after libraries that use them)

Wrong STL

Solution: Set APP_STL := c++_shared in Application.mk

ABI mismatch

Solution: Ensure you’re building for the correct ABIs in Application.mk or build.gradle

Next steps

Gradle integration

Configure ndk-build in your Gradle build

CMake

Consider migrating to CMake for cross-platform projects

ABIs

Learn about Android binary interfaces

Debugging

Debug your native code