Building Android Application using NDK

29 / Jun / 2015 by Simranjit Kour 0 comments

The Native Development Kit (NDK) is a toolset enables us to implement parts of our app using native-code languages like C and C++. Typically, good use cases for the NDK are CPU-intensive applications such as signal processing, game engines, and physics simulation.

public class MyActivity extends Activity {
  /**
  * Native method implemented in C/C++
  */
  public native void invokeNativeFunction();
}

The use of native code in Android apps are particularly useful to developers who wish to do one of the following:
1. Port their apps between platforms.
2. Reuse existing libraries, or provide their own libraries for reuse.
3. Increase performance in certain cases, particularly computationally intensive ones like games.

Installing the Android NDK:
1. Download the ndk package from the link:
http://developer.android.com/tools/sdk/ndk/index.html
2. Open a terminal window.
3. Go to the directory to which you downloaded the package.
4. Run chmod a+x on the downloaded package.
5. Execute the package. For example:

	ndk$ chmod a+x android-ndk-r10c-darwin-x86_64.bin
	ndk$ ./android-ndk-r10c-darwin-x86_64.bin
  

The folder containing the NDK extracts itself.

Making a Basic NDK App:
Step 1. In the Android project, Make a folder called jni in the root of project (right-click the project node, New – Folder).
Step 2. Create a file called Android.mk within jni folder with the following contents:

LOCAL_PATH:=$(call my-dir)
include $(CLEAR_VARS)
Here we give our module name and source file(s)
LOCAL_MODULE := ndk_test
LOCAL_SRC_FILES := ndk_test.c
include $(BUILD_SHARED_LIBRARY)

The Android.mk file is important for the NDK build process to recognize your NDK modules. In our case we named our module ndk_test and told the build tool that it consists of one source file named ndk_test.c.

Step 3. Create file ndk_test.c in jni folder

#include <string.h>
#include <jni.h>

jstring Java_com_example_ndktest_NdkTestActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
  return (*env)->NewStringUTF(env, "Hello from native code!");
}
  

Step 4. In NdkTestActivity class to use the NDK code:

public class NdkTestActivity extends Activity {

  // load the library - name matches jni/Android.mk
  static {
    System.loadLibrary("ndk_test");
  }

  // declare the native code function - must match ndkfoo.c
  private native String invokeNativeFunction();

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // this is where we call the native code
        String hello = invokeNativeFunction();

        new AlertDialog.Builder(this).setMessage(hello).show();
    }
}
  

This code will invoke the NDK method that will return the string, that will be displayed as an alert on the screen. Here’s the result of running the ndk_test app

A successful run of ndk-build tool. Create an .so file in a new folder called libs
The .so file is the binary library that will be included into the application .apk package and will be available for the Java code of the app to link to.

Hope this was useful to the readers.

Happy NDK’ing. :)

FOUND THIS USEFUL? SHARE IT

Leave a comment -