I’m new to Android and am trying to create a simple SDK+NDK concept. I followed the below steps:
- Download NDK
- Extract zip file
- Create new android project.
- Create new folder jni under project.
- Define UI as needed.
- Create a java file to call all the native methods. Declare all those methods with “native” prefix. Have static block to load library using
system.loadLibrary(""). - Create corresponding header file using
javah -jni filename - Move the generated filename.h file to jni folder.
- Write c file that includes the .h file and implements the methods in .h file and saved it.
-
Create mk file,with following content:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE :=
LOCAL_SRC_FILES := .c
include $(BUILD_SHARED_LIBRARY) - Go to the project folder in command prompt
- Give
<ndkfolder>/ndk-build - .so file will be generated
But I got stuck in the “12” point with the following error:
**"Compile thumb : com_cts_c2dmclient_NativeLib <= com_cts_c2dmclient_NativeLib.c
jni/com_cts_c2dmclient_NativeLib.c:3:40: fatal error: com_cts_c2dmclient_NativeL
ib: No such file or directory
compilation terminated.
make: *** [obj/local/armeabi/objs/com_cts_c2dmclient_NativeLib/com_cts_c2dmclien
t_NativeLib.o] Error 1**
Note: The .h file is created successfully.
My com_exampleservice_NativeLib.c file
#include "stdio.h"
#include "malloc.h"
#include <com_exampleservice_NativeLib.h>
JNIEXPORT jint JNICALL Java_com_exampleservice_NativeLib_loop
(JNIEnv * env,jobject obj,jint v1, jint v2){
int loop;
unsigned long int *array;
if(v2 == 0){
array = (unsigned long int *)malloc(v1 * sizeof(unsigned long int));}
else if(v2 == 1)
{
array = realloc(array,sizeof(array)+v1);
}
else{
}
array[0] = 1;
array[1] = 1;
for (loop = 2; loop < v1; loop++) {
array[loop] = array[loop - 1] + array[loop - 2];
}
for (loop = 0; loop < v1; loop++) {
}
if(v2 == 2)
{
free(array);
}
return 0;
}
Your
Android.mkfile does not look good in your question.Make sure that the
com_exampleservice_NativeLib.hfile does exist in yourjnidirectory near your c file.It may be necessary to add directories to the include path, like
Note that this
$(LOCAL_C_INCLUDES)must be a list of absolute paths, not like$(LOCAL_SRC_FILES)which is$(LOCAL_PATH)-based.Another note is that in your
com_exampleservice_NativeLib.cfile the use of#includedirective ornamenation is not correct. It should goSee What is the difference between #include <filename> and #include "filename"? for detailed explanation.