Thursday 17 April 2014

NDKHello2 Tutorial

Hi,
This is my Second NDKHello2 Demo project for newbies

SourceCode Link:https://github.com/JohnnyIqbal/NDKHello2
Please add appcompat library to run this project

In this demo I added a folder src in jni folder and tried to compile.

I followed this process to make this project.

1. Create NativeLib.java file

2. Open terminal and head to project directory
  cd bin/classes   
and generate header files of NativeLib.java using this command
 javah -jni com.iqbal.ndkhello.ndk.NativeLib  
3. Copy NativeLib.h file in jni folder (Create if not exist in project root)
or you can use this command
 javah -jni -o ../../jni/NativeLib.h com.iqbal.ndkhello.ndk.NativeLib  

4. Create NativeLib.c file and copy method signature in NativeLib.c
and don't foreget to include NativeLib.h


 Method Signature eg:  
 JNIEXPORT jstring JNICALL Java_com_iqbal_ndkhello_ndk_NativeLib_getHello2  
this is the declared method in header files and change to method defination

 #include"NativeLib.h"  
 JNIEXPORT jstring JNICALL Java_com_iqbal_ndkhello_ndk_NativeLib_getHello (JNIEnv *env, jclass clz){ return (*env)->NewStringUTF(env,"Hello World2"); }  

Note:NewStringUTF sometimes this function shows error means red line to suppress this error please update your CDT.

5. Add new folder in jni called src add some c files and header files as I did main.c and main.h

6. Call main.h function in NativeLib.c by including main.h header files.
 #include"src/main.h"  
 JNIEXPORT jint JNICALL Java_com_iqbal_ndkhello2_ndk_NativeLib_getAdd(  
   JNIEnv *env, jclass clz, jint a, jint b) {  
 int c=add(a,b); //this function declared in main.h and defiened in main.c  
 return c;  
 }  

7.Now create Android.mk file and write these lines to build
 LOCAL_PATH:=$(call my-dir)  
 include $(CLEAR_VARS)   
 LOCAL_MODULE:=hello2   
 LOCAL_SRC_FILES:=NativeLib.c src/main.c  
 include $(BUILD_SHARED_LIBRARY)  

8.Create Application.mk to suppress the Warnning or error android-19 is larger than minsdkversion 8 and write this lines
 APP_PLATFORM:=android-8  
android-8 is my minsdkversion so be sure about this change according to minsdkversion in AndroidManifest.xml


9.Open terminal head to project root and fire this command
 ndk-build  

10. Now run your Android NDKHello Project.


No comments:

Post a Comment

Things after Kotlin Android Extensions

Things after Kotlin Android Extensions(KTX) I remembered in MVVM when I have to declare ViewModel, I initialize View Model like this. p...