I am trying to import an android project to my Eclipse. When i run this project, i got an error, "library is null". I figured out that this project was different, it contain a file named "jni". My library is in that file. I guess the library in this file was never compiled (i don't know why) I have seen in some topic that i need to use NDK? Did any one have a clear answer about this?
Asked
Active
Viewed 3,863 times
2
-
You have to compile jni folder using ndk-build command from cygwin terminal. This create ".so" file in libs folder. – AndroidLearner May 31 '12 at 04:36
-
i am using my mac book, no need to install cygwin. – haythem souissi May 31 '12 at 04:37
-
Do you have a link indicating what command i have to lunch – haythem souissi May 31 '12 at 04:38
1 Answers
1
Refer the below links. link to download ndk sample 1 sample 2
Step1: First create a project then create a folder named jni in your project directory
Step2: Create addition.c file in jni folder and add the below lines.
#include "com_ndkadd_Addition.h"
JNIEXPORT jint JNICALL Java_com_ndkadd_Addition_messageFromNativeCode
(JNIEnv * env, jobject jObj,jint value1, jint value2)
{
return (value1 + value2);
}
Step3: Create Android.mk file in jni folder and the below code
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := Addition
LOCAL_SRC_FILES := addition.c
include $(BUILD_SHARED_LIBRARY)
Step4: navigate to bin folder of your project from command prompt. type the below line and press enter.
javah -jni com.ndkadd.Addition
Step5: Move the created com_ndkadd_Addition.h file to jni folder.
Step6: Now Compiling the Native Code as below from command prompt.
location_of_ndk\project_name> location_of_ndk\ndk-build
Step7: below the code code for your activity and usage of created .so file in your libs folder.
public class Addition extends Activity {
TextView txtHello;
static
{
System.loadLibrary("Addition");
}
public native int messageFromNativeCode(int v1,int v2);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView txtHello = new TextView(this);
txtHello.setText(""+messageFromNativeCode(5,5));
setContentView(txtHello);
}
}
Note:: Better to have NDK in the place where you have SDK and the project containing JNI in android-ndk-r8 folder.

Shankar Agarwal
- 34,573
- 7
- 66
- 64