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.
using namespace std;
class PrimeNumber {
    int number;
    PrimeNumber(int x);
    bool isPrime();
#include "PrimeNumber.h"
PrimeNumber::PrimeNumber(int num) {
    PrimeNumber::number = num;
bool PrimeNumber::isPrime() {
    bool isPrime = true;
    for (int i = 2; i <= number / 2; i++) {
        if (number % i == 0) {
            return false;
        } else {
            isPrime = true;
    return isPrime;

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()

add_library(<name> [STATIC | SHARED | MODULE]
            source1 [source2 ...])

Hear, we seare PrimeNumber to native-lib.

cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
             # Sets the library as a shared library.
             # Provides a relative path to your source file(s).
find_library( # Sets the name of the path variable.
              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )
target_link_libraries( # Specifies the target library.
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-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.

#include <jni.h>
#include <string>
#include "PrimeNumber.h"
extern "C"
/*function name : Java_+_{package name}_+{class name}_+{function name}  */
JNIEXPORT jboolean JNICALL Java_com_app_androidkt_nativeapp_MainActivity_isPrime(JNIEnv *env, jobject,jint num) {
    PrimeNumber primeNumber(num);
    return primeNumber.isPrime();

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.

 // Used to load the 'native-lib' library on application startup.
static {

● 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.

public native boolean isPrime(int t);

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

int no = Integer.parseInt(number.getText().toString());
if (isPrime(no)) {
    Toast.makeText(MainActivity.this, "Prime Number", Toast.LENGTH_SHORT).show();
} else {
    Toast.makeText(MainActivity.this, "Not Prime Number", Toast.LENGTH_SHORT).show();

Download this project from GitHub.