, , ,

When I had started Android Native Development, I had to face some problems. So, I am writing this tutorial for the beginners who is just starting.

Environment Setup:
1. Eclipse 4.2
2. JDk7
3. Android SDK
4. CDT Plugin
5. ADT Plugin
6. NDK

Now Install JDK, Eclipse. Configure SDK location. Install CDT Plugin. Then Install ADT Plugin (If you don’t install CDT, you won’t install NDK plugin).
After you install NDK plugin, you will find a new sub-menu option of Android(Windows-> Preference->Android). Set NDK location (be sure the path can’t contain a space).

Note: In some tutorial you may find that, they mentioned about Cygwin or others to compile NDK. But latest, even I used android-ndk-r8, I didn’t need those.

Create Project
1. Create a new Android project.
2. Right click on project explorer. you find “Android tools” in the menu.
3. Select “Add Native Support…”. Write your library name. You will find a new folder in project explorer (jni). jni contains a cpp file and Android.mk.
4. Now write your method in cpp.
JNIEXPORT return_type JNICALL Java_yourPackage_YourJavaClass_MethodName(JNIEnv *env, jobject thiz)

JNIEnv *env, jobject thiz MUST be in parameters. If you need more, you can add after those. As it is cpp file, you need to use extern “C”.

Here I used.
extern “C” jint Java_com_example_ndktest_MainActivity_AddValue(JNIEnv *env,
jobject thiz,
jint nValue1,
jint nValue2)

In your MainActivity class, You should write.
System.loadLibrary(“MathSDK”); // libMathSDK.so is the lib name

public native int AddValue(int nValue1, int nValue2);

Now You can use AddValue Method as a member method of MainActivity class.
You don’t need to write anything till now in Android.mk.

Now build and run.