Step by step for running android ndk project


Step by step for running android ndk project

  In our java class

    static
             { 
             System.loadLibrary("mylib");

              }


    This should be called above all activity calls.



Then call our native function  like 


    ------ public native String getMyData();-----





After that follow these steps:




Step 1:

   Create a folder"jni" in main project folder.

Step 2: 

   Inside "jni" folder,create a file "Android.mk".

Step 3:


     In Android.mk, put the below code

 

                      LOCAL_PATH := $(call my-dir)
                      MY_PATH := $(LOCAL_PATH)
                   include $(call all-subdir-makefiles)

                     include $(CLEAR_VARS)

                      LOCAL_PATH := $(MY_PATH)

                     LOCAL_LDLIBS := -llog -ldl
                         LOCAL_MODULE    := mylib
                    LOCAL_SRC_FILES := com_myproject_MainActivity.c

                    include $(BUILD_SHARED_LIBRARY)






Step 4:

       Create a header implementation using javah tool


     For that use  eclipse->run->enviormenttool

        In Main tab, there contains 3 variables


       ie Location,Working Directory,Arguements


         In Location ,enter the javah path without quotes ie ("E:\Program Files\Java\jdk1.6.0_23\bin\javah.exe")


         In Working Directory ,enter our project'bin path without quotes  ie("${workspace_loc:MyProject/bin/classes}");

         In Arguements ,enter this without quotes("-jni -verbose -d "${project_loc}${system_property:file.separator}jni" ${java_type_name}")


   After Run ,then a file is generated in jni folder which contains the function name like this 


     "JNIEXPORT jstring JNICALL Java_com_myproject_MainActivity_getMyData
  (JNIEnv *, jobject);"


Step 5: Create a C implementation of the generated file 

        Inside that import the header file

        ie

#include "com_myproject_MainActivity.h"


JNIEXPORT jstring Java_com_myproject_MainActivity_getMyData
(JNIEnv* pEnv, jobject pThis)
{
return (*pEnv)->NewStringUTF(pEnv,
"My native project talks C++");
}



Step 6: Run Cygwin 

Useful commands

1:  cd /cygdrive/c/projects/ndkfoo ---------------This corresponds the project's main folder

2: /cygdrive/c/android-ndk-r4/ndk-build  (no spaces in folders)-------This corresponds to the location where androidndk is saved.

After this step,inside the libs folder ,a folder named "armeabi" is created and some.so files are also created inside this.


Then Run this project -------------------------------------------Have a great day-------------------