Reputation: 3973
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
Upvotes: 0
Views: 1042
Reputation: 1034
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;
}
...
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
Upvotes: 2