2

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?

haythem souissi
  • 3,263
  • 7
  • 50
  • 77

1 Answers1

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