0

I am having trouble on android ndk development. I am very new to ndk and following a tutorial. On compilation time no error comes but when I uploaded the application in device it gives me an error that Native Method Not Found. I have attached the code and snap shots of my package.

AT ndk-build I got this:

[arm64-v8a] Compile        : com_testing_ndk_FibLib <= com_testing_ndk_FibLib.c
[arm64-v8a] SharedLibrary  : libcom_testing_ndk_FibLib.so
[arm64-v8a] Install        : libcom_testing_ndk_FibLib.so => libs/arm64-v8a/libcom_testing_ndk_FibLib.so
[x86_64] Compile        : com_testing_ndk_FibLib <= com_testing_ndk_FibLib.c
[x86_64] SharedLibrary  : libcom_testing_ndk_FibLib.so
[x86_64] Install        : libcom_testing_ndk_FibLib.so => libs/x86_64/libcom_testing_ndk_FibLib.so
[mips64] Compile        : com_testing_ndk_FibLib <= com_testing_ndk_FibLib.c
[mips64] SharedLibrary  : libcom_testing_ndk_FibLib.so
[mips64] Install        : libcom_testing_ndk_FibLib.so => libs/mips64/libcom_testing_ndk_FibLib.so
[armeabi-v7a] Compile thumb  : com_testing_ndk_FibLib <= com_testing_ndk_FibLib.c
[armeabi-v7a] SharedLibrary  : libcom_testing_ndk_FibLib.so
[armeabi-v7a] Install        : libcom_testing_ndk_FibLib.so => libs/armeabi-v7a/libcom_testing_ndk_FibLib.so
[armeabi] Compile thumb  : com_testing_ndk_FibLib <= com_testing_ndk_FibLib.c
[armeabi] SharedLibrary  : libcom_testing_ndk_FibLib.so
[armeabi] Install        : libcom_testing_ndk_FibLib.so => libs/armeabi/libcom_testing_ndk_FibLib.so
[x86] Compile        : com_testing_ndk_FibLib <= com_testing_ndk_FibLib.c
[x86] SharedLibrary  : libcom_testing_ndk_FibLib.so
[x86] Install        : libcom_testing_ndk_FibLib.so => libs/x86/libcom_testing_ndk_FibLib.so
[mips] Compile        : com_testing_ndk_FibLib <= com_testing_ndk_FibLib.c
[mips] SharedLibrary  : libcom_testing_ndk_FibLib.so
[mips] Install        : libcom_testing_ndk_FibLib.so => libs/mips/libcom_testing_ndk_FibLib.so

FibLib.java

 package com.testing.ndk;

public class FibLib {
       static {
          System.loadLibrary("com_testing_ndk_FibLib"); // Load native library at runtime
                                       // hello.dll (Windows) or libhello.so (Unixes)
       }

       // Declare a native method sayHello() that receives nothing and returns void
       public static native String sayHello();

       // Test Driver
       public static void main(String[] args) {
          new FibLib().sayHello();  // invoke the native method
       }
    }

com_testing_ndk_FibLib.c

  #include <jni.h>
#include <stdio.h>
#include "com_testing_ndk_FibLib.h"
// Implementation of native method sayHello() of HelloJNI class

   JNIEXPORT void JNICALL Java_com_testing_ndk_FibLib_sayHello(JNIEnv *env, jobject thisObj) {
   printf("Hello World!\n");
   return;
}

com_testing_ndk_FibLib.h

    /* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_testing_ndk_FibLib */

#ifndef _Included_com_testing_ndk_FibLib
#define _Included_com_testing_ndk_FibLib
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_testing_ndk_FibLib
 * Method:    sayHello
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_testing_ndk_FibLib_sayHello
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

Application.mk

APP_PLATFORM := android-17
APP_ABI :=all

Android.mk

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

Error Log:

[arm64-v8a] Compile        : com_testing_ndk_FibLib <= com_testing_ndk_FibLib.c
jni/com_testing_ndk_FibLib.c:6:24: error: conflicting types for 'Java_com_testing_ndk_FibLib_sayHello'
 JNIEXPORT void JNICALL Java_com_testing_ndk_FibLib_sayHello(JNIEnv *env, jobject thisObj) {
                        ^
In file included from jni/com_testing_ndk_FibLib.c:3:0:
jni/com_testing_ndk_FibLib.h:15:27: note: previous declaration of 'Java_com_testing_ndk_FibLib_sayHello' was here
 JNIEXPORT jstring JNICALL Java_com_testing_ndk_FibLib_sayHello
                           ^
make: *** [obj/local/arm64-v8a/objs/com_testing_ndk_FibLib/com_testing_ndk_FibLib.o] Error 1

enter image description here

3
  • 2
    It is obvious - method is not found because you didn't implement method with signature JNIEXPORT jstring JNICALL Java_com_testing_ndk_FibLib_sayHello (JNIEnv *, jclass) as suggested in header file. Commented May 11, 2015 at 11:17
  • @Valentin thanks for your reply. Can you please right some code snippet here with your code, it will help me alot Commented May 11, 2015 at 11:43
  • Same issue as stackoverflow.com/questions/30152520/… ? Commented May 11, 2015 at 15:39

1 Answer 1

2

JNI peforms native method search using its name. To ensure that exported function it found is correct one, methods must be named according following scheme:

Java_package_name_className_methodName

For example, in you case name of native function must be Java_com_testing_ndk_FibLib_sayHello, but in your com_testing_ndk_FibLib.c there are no function with such name.

Following snippet should be fine if you place it in this file.

com_testing_ndk_FibLib.c

#include <jni.h>
#include <stdio.h>
#include "com_testing_ndk_FibLib.h"

// Implementation of native method sayHello() of HelloJNI class
JNIEXPORT void JNICALL Java_com_testing_ndk_FibLib_sayHello(JNIEnv *env, jobject thisObj) {
   printf("Hello World!\n");
   return;
}


To fix your problem change return type of signature in com_testing_ndk_FibLib.h at line 15

...
JNIEXPORT void JNICALL Java_com_testing_ndk_FibLib_sayHello
  (JNIEnv *, jclass);
...

and in FibLib.java at line 10:

...
public static native String sayHello();
...

Read about JNI here: http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for your reply. By doing this i am getting the following error, please see my updated Error log above
ok, why you declared "sayHello()" (in java) to return String object even if it doesn't return anything? Change return type to void, and generate header file again.
can you please give me here some code snippet. I am confused here
i edited answer and added solution and link to JNI specification
i have edit all of my above codes according to your instruction, but still i am getting the same error. Please guide me here.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.