Monday, August 6, 2012

Android JNI simple example

Android JNI simple example.

In this post, I am trying to demonstrate Android JNI with an example. For those , who need to know more about JNI in detail, https://developer.android.com/guide/practices/jni.html

I am trying to discuss the JNI with an example. I am building on Windows OS. So, I used Cygwin to use the make build.

Step 1 :  Download Android NDK.

Step 2 : Install Cygwin ( Windows ).

Step 3:  Edit the Environment path for these two, like

C:\cygwin\bin;
F:\Softwares\android-ndk-r8-windows\android-ndk-r8

Step 4:

Create an Android Project, and create a native interface in java (as below) and compile it, to get the .class file.

package com.jni;
public class Library {

            // here “shared” is the shared object (.so file) created by NDK
          // Load your library inside static block
              static{
                        System.loadLibrary("shared");
              }

// Native method declaration with native keyword
// here method name is getNumber()

              Public native int getNumber(int num);
}

Step 5:
Create the Activity, which use this method.

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Library lib = new Library(); //my Native interface in Java
    int a = lib.getNumber(100); //Call to native method

    Toast.makeText(getApplicationContext(), "res = "+a,Toast.LENGTH_SHORT).show();

    }
    
}

Step 6 :
         Create the corresponding header file for your native interface.(follow below steps).
a.    Go to your project directory.
b.    Run the following command.
                                          i.    Javah –jni –classpath bin <fully qualified class name of interface>
(here class name is com.jni.Library)
c.    It will generate the header file (com_jni_Library.h) in project directory.


Step 7 : 

 Create a project directory inside NDK_HOME and create a directory named “jni” inside that. (e.g. NDK_HOME/sample/jni)

Step 8 :
   
                     Copy generated header file (at step 5.c) to NDK_HOME/<project_dir>/jni/


          Step 9:


  Create a C file inside NDK_HOME/<project_dir>/jni/, in which you will implement the native method definition.

        In this example: (Test.c)



//Include the header file generated in step 3.c

#include "com_jni_Library.h"

//Copy the method declaration/signature from the same header file and pass the //arguments appropriately. (Refer JNI documentation for more info)

JNIEXPORT jint JNICALL Java_com_jni_Library_getNumber
  (JNIEnv * env, jobjectobj, jint a){
          return (a+100);
}


Step 10 :
  Create a file named “Android.mk” inside NDK_HOME/<project_dir>/jni/ and write lines given below.
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

     # LOCAL_MODULE name should be the library name which you declared in
     # System.loadLibrary(“shared”) in step 2. Here lib name is “shared”

LOCAL_MODULE := shared

# LOCAL_SRC_FILES should be the C source file name created in step 6.
LOCAL_SRC_FILES := Test.c


include $(BUILD_SHARED_LIBRARY)

Step 11 : 

 Build your native code to generate shared object. (follow below steps)

a.    Start Cygwin and go to <NDK_HOME>/<project_dir>/

e.g. cd /cygdrive/d/android/android-ndk-r4b/sample

b.    Run the build command inside project directory

                                          i.    ndk-build

c.    If everything goes fine, it will generate a shared object file as

sample/libs/armeabi/libshared.so

Step 12 :
Now,   Copy the “libs” directory generated in step 8.c into your Android Project directory.


Step 13 : 

Build and run your Android application.

1 comment: