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?
Thanks.
See Question&Answers more detail:osI 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?
Thanks.
See Question&Answers more detail:osThere 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_LDLIBS := -llog
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
include $(BUILD_SHARED_LIBRA
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>
#define DEBUG_TAG "MY_NDK_DEMO"
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);
free(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 {
System.loadLibrary("local_module_ndk");
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
button b = new Button(this);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),""+getStringAdd(100,200), 2000).show();
}
});
setContentView(b);
}
}
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
after that typw the path of the my NDK's ndk-build file path
now we press enter the automatic it create libs directory in your project
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
thanks