NDK-BUILD ANDROID

Anmol Sehgal
3 min readJun 18, 2018

--

The Native Development Kit (NDK) is a set of tools that allows you to use C and C++ code with Android and provides platform libraries you can use to manage native activities.

STEP 1: To use the ndk-tools with android, you need to download and install “NDK tools” from the SDK-manager.

  1. From an open project, select Tools > Android > SDK Manager from the main menu.
  2. Click the SDK Tools tab.
  3. Check the boxes next to NDK, as shown in figure

Now after the ndk-tools are installed, you can start using the native code in your android project.

STEP 2: If you have native code in C or C++ you can import that code inside the android studio, or you can yourself write C, jniC++ code, to do so:
make JNI” directory inside src > main, parallel to java directory.
Insert all the native code inside the JNI folder.

NOTE: the native methods inside the C, C++ classes must be declared as:

JNIEXPORT jint JNICALL
Java_packagename_classname_methodname(JNIEnv *env, jobject instance, jint variablename) {
return variablename;
}

i.e. the methods inside C, fllyC++ must be fully qualified with the classpath of the java class, where the native methods are to be used.

STEP 3: Now your android code has native code inside it, but it is not yet linked to your android code.
To glue your C and C++ source files to the Android NDK, we use a file “android.mk”.

Android.mk :

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES := c-class-name.c
LOCAL_MODULE := module_name

This is a sample of android.mk file which tells that
1. A Android.mk file must begin by defining the LOCAL_PATH variable:

LOCAL_PATH := $(call my-dir)

This variable indicates the location of the source files in the development tree. Here, the macro function,my-dir provided by the build system, returns the path of the current directory (the directory containing the fileAndroid.mk itself).

2. clear_vars

include $(CLEAR_VARS)

The CLEAR_VARS variable points to a special GNU Makefile that clears many LOCAL_XXXvariables for you

3. The variableLOCAL_MODULEstores the name of the module that you wish to build. Use this variable once per module in your application.

LOCAL_MODULE := module_name

STEP 4: Then the final step is to link this native module to the Gradle,
To do so,
include

ndk {
moduleName "module_name"
}

in the build.gradle(app) as shown:

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
...
}
buildTypes {
release {
...
}
}
ndk {
moduleName "module_name"
}
}

Now your native code is linked to android, and we can now call the native methods inside our java code.

USAGE: Now the C, C++ code will compile and a module (here named “module_name” ) will be created, and to use the native code inside java, just load the module using

static {
System.loadLibrary("module_name");
}

and declare the native methods as:

public native int mathodname(int variablename);

Now simply call this method anywhere inside the java code, passing in the required arguments and getting the required result.

NOTE: this method “methodname” should be used in the class “classname” inside the package “packagename”, as qualified inside the native code method.

--

--

No responses yet