Starting with NDK for Android – A Simple example. OR How to run a C code in android?

Hello Friends….

Today I am going to talk about how to use ndk in android to run c code.

Note : These steps are for Mac and Linux Users not for Windows users.

Follow these step exactly to set up and run ndk in android.

1. I think that you are having your eclipse and android uptodate.
2. Go to this place “http://developer.android.com/sdk/ndk/index.html” and download the ndk for your operating system.


3. After downloading the zip, extract it and save it in your own location.

4. Now we are going to create the android Project.
I am naming it “NDKDemo” and the activity is named “NDKDemoActivity”.

Now create a folder named “jni” in the project.

5. Click on jni folder-> then go to “Run Menu” > external tools > external tools configuration.

6. Click on Program and click on the new icon (first icon) on the top. On the right side Give the NDK a name.

In the Next Location textbox Click on “Browse File System” and located the ndk-build that you just downloaded from “developer.android.com

7. Now in the Workspace location > Click on Browse Workspace and select your “jni” folder in the “NDKDemo” project directory.

This dialog comes when you click the Browse Workspace button and select the jni folder in your project and click OK.

8. Now create a file named “Android.mk” file inside the “jni” folder.

copy this code to “Android.mk” file.

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_LDLIBS := -llog

LOCAL_MODULE    := ndkdemo
LOCAL_SRC_FILES := native.c

include $(BUILD_SHARED_LIBRARY)

9. Now create another file named “native.c” inside the jni folder and copy this code into it.
This is our c code.
This don’t look purely like a c code because it has some java elements inside it.

#include <jni.h>
#include <string.h>
#include <android/log.h>

#define DEBUG_TAG "NDKDemoActivity"

void Java_com_coderzheaven_pack__NDKDemoActivity_printLog(JNIEnv * env, jobject this, jstring logString)
{
    jboolean isCopy;
    const char * szLogString = (*env)->GetStringUTFChars(env, logString, &isCopy);

    __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK: %s", szLogString);

    (*env)->ReleaseStringUTFChars(env, logString, szLogString);
}

Note :
Please take a look the function name

Java_com_coderzheaven_pack__NDKDemoActivity_printLog

This naming should be in this format
“Java_packagename_funtionname(arguments)”

Also one more thing the “dots” (.) in the package name should be replaced by underscore in the function name”. This is important.

OK Everything needed for NDK is done.
10. Now what we have to do is to compile the c code. For that go to “Run” > external tools > select NDK you created and run.

Now the c code has been compiled.

11. Now in the java Activity copy this code

package com.coderzheaven.pack;

import android.app.Activity;
import android.os.Bundle;

public class NDKDemoActivity extends Activity {
	  static {
	        System.loadLibrary("ndkdemo");
	    }

	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);
	        printLog("Hello Coderzheaven");
	    }

	    private native void printLog(String logThis);
}

12. Now run this project and check the Logcat for the message after calling the C function from android.

You can any number of c functions inside the native.c code and run it.
Make sure to put a declaration on top of the activity before calling.

Please leave your comments if this post was useful.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>