0
votes

I'm new to Android and am trying to create a simple SDK+NDK concept. I followed the below steps:

  1. Download NDK
  2. Extract zip file
  3. Create new android project.
  4. Create new folder jni under project.
  5. Define UI as needed.
  6. 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("").
  7. Create corresponding header file using javah -jni filename
  8. Move the generated filename.h file to jni folder.
  9. Write c file that includes the .h file and implements the methods in .h file and saved it.
  10. Create mk file,with following content:

    LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)
    LOCAL_MODULE    :=
    LOCAL_SRC_FILES := .c
    include $(BUILD_SHARED_LIBRARY)

  11. Go to the project folder in command prompt
  12. Give <ndkfolder>/ndk-build
  13. .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;
}
2
Can you tell me your package name and the name of the class in which you are loading this lib?Dharmendra
i have three packages 1.com.cts.agentframework 2.com.cts.c2dmclient 3.com.exampleservice .. in com.exampleservice i created a class named NativeLib.java in this only loading the lib.. but iam calling the NDK fn from com.cts.c2dmclientSudarshan
you have specified a module name in your Android.mk right?bph
yes which is same as the .c file name i.e, com_exampleservice_NativeLibSudarshan

2 Answers

0
votes

Your Android.mk file does not look good in your question.

Make sure that the com_exampleservice_NativeLib.h file does exist in your jni directory near your c file.

It may be necessary to add directories to the include path, like

  LOCAL_C_INCLUDES += $(LOCAL_PATH)/include

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.c file the use of #include directive ornamenation is not correct. It should go

#include <stdio.h>
#include <malloc.h>
#include "com_exampleservice_NativeLib.h"

See What is the difference between #include <filename> and #include "filename"? for detailed explanation.

0
votes

I've just been using the NDK for the 1st time as well.

I used SWIG to avoid having to write the Java JNI code manually

I found this tutorial useful: