Take the 2-minute tour ×

I need to use some native c/c++ in my project, so I need to use the NDK. Is there an easy way to set it up in eclipse?


share|improve this question
add comment

2 Answers

There are following steps

1 : Create a jni folder in your project directory

2 : Create a file name Android.mk in your newly created folder jni and create a new file of C or C++, lets we consider hear we use C file and name of file is MyNativeC.c

3: now type following code in Android.mk file

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)


LOCAL_MODULE    := local_module_ndk // this is the name of local module by which you can call the Local source file
LOCAL_SRC_FILES := MyNativeC.c// hear we can pass the name of your native file name hear we are use MyNativeC.c file so we pass the name of MyNativeC.c in local source file name


4 now open MyNativeC.c file and create two method which you want to call from your android code(from your Activity) hear we create following code

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


jstring Java_com_myNDKDemo_MainActivity_getString(JNIEnv * env, jobject this, jint value1, jint value2)
    char *szFormat = "Addition : %i";
    char *szResult;

    jlong sum = value1+value2;

    szResult = malloc(sizeof(szFormat) + 20);

    sprintf(szResult, szFormat, sum);

    jstring result = (*env)->NewStringUTF(env, szResult);


    return result;

5 now edit your activity where you want to call the native code,

first create a static block where we have to load the library of native code.

hear we show the code of my activity name is MainActivity.java

package com.myNDKDemo

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends Activity {
    private static final String DEBUG_TAG = "MainActivity";

    private native String getStringAdd(int fist, int second);

    static {

    public void onCreate(Bundle savedInstanceState) {

        button b = new Button(this);

        b.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

                            Toast.makeText(getApplicationContext(),""+getStringAdd(100,200), 2000).show();




6 Now first compile the c code, for compile c code first you have to NDK development kit,

now open run. and type cmd

now go to project path

In my case I create application in Destop, so hear we give the path of my app

after that typw the path of the my NDK's ndk-build file path

enter image description here

now we press enter the automatic it create libs directory in your project enter image description here

7 If you see in your project, there are libs and obj created automatically.

8.Refresh (right Click) the JNI folder (refresh it every time you build using teh ndk-build,this actually loads the newly built shared library in the libs folder.)

9.Now run your android project, when press the button the it will show you the addition


share|improve this answer
add comment
up vote 3 down vote accepted

Ok, after I spent some time experimenting, I can say that the best way for a beginner to start using NDK in eclipse is first to go over this tutorial: http://marakana.com/forums/android/examples/49.html and simply create the necessary files for the jni folder (but don't do anything else). Then you should read this http://mobilepearls.com/labs/ndk-builder-in-eclipse/ and follow the steps. Then you are ready.

share|improve this answer
The first link is out of date. The /app/ directory is no longer needed - you only need Android.mk and ndk-build will Just Work. –  richq Dec 6 '10 at 16:14
add comment

Your Answer


