Android Native Development Kit (NDK) allow you to embed C/C++ code (native code) into your applications. You can use it to either build from your own source code or use existing pre-built native libraries.

NDK-build is a shell script that launches the NDK build scripts. It generates the binaries and copies the binaries into your application’s project path.

Android NDK

NDK provides a tool that allows Java applications to invoke native code and vice versa.

Android NDK example

To compile and debug native code for your app, you need the NDK, CMake, and LLDB.


It is default build tool for native libraries is CMake inAndroid Studio.It’s an external build tool that works with Gradle to build your native library.


It is a debugger for Android Studio to debug native code.

After installation create a new android project

NDK Project

Project structure


Create new c++ source files

  1. Right-click on the cpp directory and select New > C++ class.
  2. Enter a name for your source file, such as PrimeNumber.


CMake build script

CMake build script is a plain text file.Now configure your build script by adding CMake commands. To instruct CMake to create a native library from native source code, add the add_library()commands to your build script.

When you add a source file or library to your CMake build script using add_library()

Hear, we seare PrimeNumber to native-lib.


when making changes to your CMake  script file after you have already linked it to Gradle, you should sync Android Studio with your changes by selecting Build > Refresh Linked C++ Projects from the menu bar.

Finally, we can implement the native source code, which contains the native function. We need to include the header file jni.h . We can see in the example that the native function returns a  jboolean(JNI data type).

The name of the function is composed of: The Java_  string  +  file path relative to the top level source directory that contains the Java source code (with underscore instead of slash) +  the name of the Java source code (without the java extension) + the native function name

We have three arguments: 1 JNIEnv * is a pointer to the VM, also called interface pointer  2.jobject is a pointer to the implicit “this” object passed from the Java side.3 Integer number.

The Java source code includes 3 lines

● First of all, you must have a static initializer in which we load the native library.This code will be executed before any other method.

● Then we must declare the native function. We will use the native keyword which tells the virtual machine that the function implementation is found in a shared native library.

● Finally, we can use the native function directly, in this example will check prime number.


Download this project from GitHub.


1 thought on “Android NDK”

  1. When tried to run application it gives me following error:

    Error:(19) undefined reference to ‘PrimeNumber::PrimeNumber(int)’
    Error:(20) undefined reference to ‘PrimeNumber::isPrime()’
    Error:error: linker command failed with exit code 1 (use -v to see invocation)

Leave a Reply